<commit_before>package server

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/albertyw/reaction-pics/tumblr"
)

func TestReadFile(t *testing.T) {
	request, err := http.NewRequest("GET", "/app.js", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	indexHandler(response, request)
	assert.Equal(t, response.Code, 200)

	cacheString := appCacheString()
	assert.Contains(t, response.Body.String(), cacheString)
}

func TestIndexFile(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.True(t, len(response.Body.String()) > 100)
}

func TestExactURL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestNoExactURL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestRedirectS3URL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/static/data/asdf.gif", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 307)
	url, err := response.Result().Location()
	assert.NoError(t, err)
	assert.Equal(t, url.String(), "http://static.reaction.pics/img/asdf.gif")
}

func TestSearchHandler(t *testing.T) {
	request, err := http.NewRequest("GET", "/search", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":0,\"totalResults\":0}")
}

func TestSearchHandlerOffset(t *testing.T) {
	request, err := http.NewRequest("GET", "/search?offset=1", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":1,\"totalResults\":0}")
}

func TestSearchHandlerMalformedOffset(t *testing.T) {
	request, err := http.NewRequest("GET", "/search?offset=asdf", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":0,\"totalResults\":0}")
}

func TestPostHandlerMalformed(t *testing.T) {
	request, err := http.NewRequest("GET", "/post/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostHandlerNotFound(t *testing.T) {
	request, err := http.NewRequest("GET", "/post/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostHandler(t *testing.T) {
	post := tumblr.Post{ID: 1234}
	board.AddPost(post)
	defer func() { board.Reset() }()
	request, err := http.NewRequest("GET", "/post/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.NotEqual(t, len(response.Body.String()), 0)
}

func TestPostDataHandler(t *testing.T) {
	post := tumblr.Post{ID: 1234}
	board.AddPost(post)
	defer func() { board.Reset() }()
	request, err := http.NewRequest("GET", "/postdata/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.NotEqual(t, len(response.Body.String()), 0)
}

func TestPostDataHandlerMalformed(t *testing.T) {
	request, err := http.NewRequest("GET", "/postdata/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostDataHandlerUnknown(t *testing.T) {
	request, err := http.NewRequest("GET", "/postdata/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestStatsHandler(t *testing.T) {
	request, err := http.NewRequest("GET", "/stats.json", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	statsHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"keywords\":[],\"postCount\":\"0\"}")
}

func TestAddPost(t *testing.T) {
	oldLength := len(board.Posts)
	post1 := tumblr.Post{ID: 1, Title: "1"}
	post2 := tumblr.Post{ID: 2, Title: "2"}
	post3 := tumblr.Post{ID: 3, Title: "3"}
	postChan := make(chan tumblr.Post, 1)
	defer func() { close(postChan) }()
	go loadPosts(postChan)
	postChan <- post1
	postChan <- post2
	postChan <- post3
	assert.NotEqual(t, len(board.Posts), oldLength)
	assert.True(t, board.Posts[0].ID < board.Posts[1].ID)
}

func TestCacheString(t *testing.T) {
	tempStaticPath := staticPath
	defer func() { staticPath = tempStaticPath }()
	cacheString := appCacheString()
	assert.NotEqual(t, cacheString, "")

	staticPath = ""
	cacheString = appCacheString()
	assert.Equal(t, cacheString, "")
}
<commit_msg>Add test for formatting percent symbols<commit_after>package server

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/albertyw/reaction-pics/tumblr"
)

func TestReadFile(t *testing.T) {
	request, err := http.NewRequest("GET", "/app.js", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	indexHandler(response, request)
	assert.Equal(t, response.Code, 200)

	cacheString := appCacheString()
	assert.Contains(t, response.Body.String(), cacheString)
}

func TestIndexFile(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.True(t, len(response.Body.String()) > 100)
}

func TestExactURL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestNoExactURL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestRedirectS3URL(t *testing.T) {
	handler := rewriteFS(http.FileServer(http.Dir(staticPath)).ServeHTTP)
	request, err := http.NewRequest("GET", "/static/data/asdf.gif", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	handler(response, request)
	assert.Equal(t, response.Code, 307)
	url, err := response.Result().Location()
	assert.NoError(t, err)
	assert.Equal(t, url.String(), "http://static.reaction.pics/img/asdf.gif")
}

func TestSearchHandler(t *testing.T) {
	request, err := http.NewRequest("GET", "/search", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":0,\"totalResults\":0}")
}

func TestSearchHandlerOffset(t *testing.T) {
	request, err := http.NewRequest("GET", "/search?offset=1", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":1,\"totalResults\":0}")
}

func TestSearchHandlerMalformedOffset(t *testing.T) {
	request, err := http.NewRequest("GET", "/search?offset=asdf", nil)
	assert.NoError(t, err)

	q := request.URL.Query()
	q.Add("query", "searchTerm")
	response := httptest.NewRecorder()
	searchHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"data\":[],\"offset\":0,\"totalResults\":0}")
}

func TestPostHandlerMalformed(t *testing.T) {
	request, err := http.NewRequest("GET", "/post/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostHandlerNotFound(t *testing.T) {
	request, err := http.NewRequest("GET", "/post/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostHandler(t *testing.T) {
	post := tumblr.Post{ID: 1234}
	board.AddPost(post)
	defer func() { board.Reset() }()
	request, err := http.NewRequest("GET", "/post/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.NotEqual(t, len(response.Body.String()), 0)
}

func TestPostDataHandler(t *testing.T) {
	post := tumblr.Post{ID: 1234}
	board.AddPost(post)
	defer func() { board.Reset() }()
	request, err := http.NewRequest("GET", "/postdata/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.NotEqual(t, len(response.Body.String()), 0)
}

func TestPostDataPercentHandler(t *testing.T) {
	post := tumblr.Post{ID: 1234, Title: `asdf% qwer`}
	board.AddPost(post)
	defer func() { board.Reset() }()
	request, err := http.NewRequest("GET", "/postdata/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	var data map[string][]map[string]interface{}
	json.Unmarshal(response.Body.Bytes(), &data)
	title := data["data"][0]["title"].(string)
	assert.Equal(t, `asdf% qwer`, title)
}

func TestPostDataHandlerMalformed(t *testing.T) {
	request, err := http.NewRequest("GET", "/postdata/asdf", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestPostDataHandlerUnknown(t *testing.T) {
	request, err := http.NewRequest("GET", "/postdata/1234", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	postDataHandler(response, request)
	assert.Equal(t, response.Code, 404)
}

func TestStatsHandler(t *testing.T) {
	request, err := http.NewRequest("GET", "/stats.json", nil)
	assert.NoError(t, err)

	response := httptest.NewRecorder()
	statsHandler(response, request)
	assert.Equal(t, response.Code, 200)
	assert.Equal(t, response.Body.String(), "{\"keywords\":[],\"postCount\":\"0\"}")
}

func TestAddPost(t *testing.T) {
	oldLength := len(board.Posts)
	post1 := tumblr.Post{ID: 1, Title: "1"}
	post2 := tumblr.Post{ID: 2, Title: "2"}
	post3 := tumblr.Post{ID: 3, Title: "3"}
	postChan := make(chan tumblr.Post, 1)
	defer func() { close(postChan) }()
	go loadPosts(postChan)
	postChan <- post1
	postChan <- post2
	postChan <- post3
	assert.NotEqual(t, len(board.Posts), oldLength)
	assert.True(t, board.Posts[0].ID < board.Posts[1].ID)
}

func TestCacheString(t *testing.T) {
	tempStaticPath := staticPath
	defer func() { staticPath = tempStaticPath }()
	cacheString := appCacheString()
	assert.NotEqual(t, cacheString, "")

	staticPath = ""
	cacheString = appCacheString()
	assert.Equal(t, cacheString, "")
}

{'fraction_non_alphanumeric': 0.18072189642355663, 'fraction_numerical': 0.016932353184108368, 'mean_word_length': 5.506179473401397, 'pattern_counts': {'":': 22, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2928717', 'n_tokens_mistral': 4614, 'n_tokens_neox': 4381, 'n_words': 819}