diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-02 18:03:28 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-02 18:03:28 -0800 |
| commit | f1963beac75d13352a08d5918e596b781facbdf8 (patch) | |
| tree | 5d5a1aa0e7a1bec9892813040df58c4c7415c040 /api | |
| parent | 94dd1ca677c716975d43f323c97ea26f8fd4c962 (diff) | |
| download | something-main.tar.gz something-main.zip | |
Diffstat (limited to 'api')
| -rw-r--r-- | api/api.go | 8 | ||||
| -rw-r--r-- | api/api_test.go | 74 |
2 files changed, 41 insertions, 41 deletions
@@ -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")) + } } |
