Skip to content

Commit

Permalink
Refactor. Add tests for the home & api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgelbg committed Jul 7, 2020
1 parent 81ae62d commit a4d911c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
w.Write(payload) // nolint: errcheck
}

// RegisterAppHandlers registers the app handlers with the given mux
func RegisterAppHandlers(mux *http.ServeMux) {
mux.Handle(StaticPath,
http.StripPrefix(StaticPath, http.FileServer(http.Dir("static"))),
)

mux.HandleFunc("/", indexHandler)
mux.HandleFunc(APIPath, apiHandler)

}

func main() {
config := zap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
Expand All @@ -120,15 +131,10 @@ func main() {
mux := http.NewServeMux()

RegisterDebugHandler(mux)
mux.Handle(StaticPath,
http.StripPrefix(StaticPath, http.FileServer(http.Dir("static"))),
)

mux.HandleFunc("/", indexHandler)
mux.HandleFunc(APIPath, apiHandler)
RegisterAppHandlers(mux)

server := http.Server{
Handler: http.TimeoutHandler(mux, procTimeout, "Processing your request took too long!"),
Handler: http.TimeoutHandler(mux, procTimeout, "Processing your request took too long."),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
Expand Down
54 changes: 54 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func TestAPIHandler(t *testing.T) {
response: "tokenizer parameter not found\n",
status: http.StatusBadRequest,
},
{
name: "Empty payload",
method: "GET",
payload: nil,
response: "str parameter not found\n",
status: http.StatusBadRequest,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -78,3 +85,50 @@ func TestAPIHandler(t *testing.T) {
})
}
}

func TestIndexRoute(t *testing.T) {
mux := http.NewServeMux()
RegisterAppHandlers(mux)

// The response recorder used to record HTTP responses
rr := httptest.NewRecorder()

req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal("Creating 'GET /' request failed,")
}

mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("'GET /' request should've a handler registered, got: %d, want: %d",
rr.Code, http.StatusOK,
)
}
}

func TestAPIRoute(t *testing.T) {
mux := http.NewServeMux()
RegisterAppHandlers(mux)

// The response recorder used to record HTTP responses
rr := httptest.NewRecorder()
payload := url.Values{
"tokenizer": {"%{key1} %{key2}"},
"str": {"a b"},
}

req, err := http.NewRequest("POST", APIPath, strings.NewReader(
payload.Encode()))
if err != nil {
t.Fatalf("Creating 'POST /api' request failed.")
}
req.Header.Add("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8")

mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("'POST /api' request should've a handler registered, got: %d, want: %d",
rr.Code, http.StatusOK,
)
}
}

0 comments on commit a4d911c

Please sign in to comment.