aboutsummaryrefslogtreecommitdiff
path: root/api/api_test.go
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2025-01-02 18:03:28 -0800
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2025-01-02 18:03:28 -0800
commitf1963beac75d13352a08d5918e596b781facbdf8 (patch)
tree5d5a1aa0e7a1bec9892813040df58c4c7415c040 /api/api_test.go
parent94dd1ca677c716975d43f323c97ea26f8fd4c962 (diff)
downloadsomething-main.tar.gz
something-main.zip
run formatterHEADmain
Diffstat (limited to 'api/api_test.go')
-rw-r--r--api/api_test.go74
1 files changed, 37 insertions, 37 deletions
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"))
+ }
}