aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/api.go8
-rw-r--r--api/api_test.go74
2 files changed, 41 insertions, 41 deletions
diff --git a/api/api.go b/api/api.go
index 2e6862e..ab3205e 100644
--- a/api/api.go
+++ b/api/api.go
@@ -82,10 +82,10 @@ func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux {
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
requestContext := makeRequestContext()
- (*requestContext.TemplateData)["Service"] = "something"
- templateFile := "hello.html"
- LogRequestContinuation(requestContext, r, w)(template.TemplateContinuation(templateFile, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
+ (*requestContext.TemplateData)["Service"] = "something"
+ templateFile := "hello.html"
+ LogRequestContinuation(requestContext, r, w)(template.TemplateContinuation(templateFile, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
})
- return mux
+ return mux
}
diff --git a/api/api_test.go b/api/api_test.go
index 5cd1280..6dbd44a 100644
--- a/api/api_test.go
+++ b/api/api_test.go
@@ -2,7 +2,7 @@ package api_test
import (
"database/sql"
- "io"
+ "io"
"net/http/httptest"
"os"
"strings"
@@ -20,70 +20,70 @@ func setup(t *testing.T) (*sql.DB, *httptest.Server) {
testDb := database.MakeConn(&randomDb)
database.Migrate(testDb)
- arguments := &args.Arguments{
- TemplatePath: "../templates",
- StaticPath: "../static",
- }
+ arguments := &args.Arguments{
+ TemplatePath: "../templates",
+ StaticPath: "../static",
+ }
- mux := api.MakeMux(arguments, testDb)
- testServer := httptest.NewServer(mux)
+ mux := api.MakeMux(arguments, testDb)
+ testServer := httptest.NewServer(mux)
- t.Cleanup(func() {
+ t.Cleanup(func() {
testServer.Close()
testDb.Close()
os.Remove(randomDb)
- })
+ })
return testDb, testServer
}
func assertResponseCode(t *testing.T, resp *httptest.ResponseRecorder, statusCode int) {
- if resp.Code != statusCode {
- t.Errorf("code is unexpected: %d, expected %d", resp.Code, statusCode)
- }
+ if resp.Code != statusCode {
+ t.Errorf("code is unexpected: %d, expected %d", resp.Code, statusCode)
+ }
}
func assertResponseBody(t *testing.T, resp *httptest.ResponseRecorder, body string) {
- buf := new(strings.Builder)
- _, err := io.Copy(buf, resp.Body)
- if err != nil {
- panic("could not read response body")
- }
- bodyStr := buf.String()
- if bodyStr != body {
- t.Errorf("body is unexpected: %s, expected %s", bodyStr, body)
- }
+ buf := new(strings.Builder)
+ _, err := io.Copy(buf, resp.Body)
+ if err != nil {
+ panic("could not read response body")
+ }
+ bodyStr := buf.String()
+ if bodyStr != body {
+ t.Errorf("body is unexpected: %s, expected %s", bodyStr, body)
+ }
}
func TestHealthcheck(t *testing.T) {
_, testServer := setup(t)
- req := httptest.NewRequest("GET", "/health", nil)
- resp := httptest.NewRecorder()
- testServer.Config.Handler.ServeHTTP(resp, req)
+ req := httptest.NewRequest("GET", "/health", nil)
+ resp := httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(resp, req)
- assertResponseCode(t, resp, 200)
- assertResponseBody(t, resp, "healthy")
+ assertResponseCode(t, resp, 200)
+ assertResponseBody(t, resp, "healthy")
}
func TestHello(t *testing.T) {
_, testServer := setup(t)
- req := httptest.NewRequest("GET", "/", nil)
- resp := httptest.NewRecorder()
- testServer.Config.Handler.ServeHTTP(resp, req)
+ req := httptest.NewRequest("GET", "/", nil)
+ resp := httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(resp, req)
- assertResponseCode(t, resp, 200)
+ assertResponseCode(t, resp, 200)
}
func TestCachingStaticFiles(t *testing.T) {
_, testServer := setup(t)
- req := httptest.NewRequest("GET", "/static/css/styles.css", nil)
- resp := httptest.NewRecorder()
- testServer.Config.Handler.ServeHTTP(resp, req)
+ req := httptest.NewRequest("GET", "/static/css/styles.css", nil)
+ resp := httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(resp, req)
- assertResponseCode(t, resp, 200)
- if resp.Header().Get("Cache-Control") != "public, max-age=3600" {
- t.Errorf("client cache will live indefinitely for static files, which is probably not great! %s", resp.Header().Get("Cache-Control"))
- }
+ assertResponseCode(t, resp, 200)
+ if resp.Header().Get("Cache-Control") != "public, max-age=3600" {
+ t.Errorf("client cache will live indefinitely for static files, which is probably not great! %s", resp.Header().Get("Cache-Control"))
+ }
}