Shit ton of tests

This commit is contained in:
Melody Becker 2025-04-24 15:59:15 +02:00
parent 99b00887a8
commit ccf98c2f6e
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
13 changed files with 1157 additions and 1 deletions

45
http/json_test.go Normal file
View file

@ -0,0 +1,45 @@
package webutils_test
import (
"io"
"net/http/httptest"
"testing"
webutils "git.mstar.dev/mstar/goutils/http"
)
func TestSendJsonOk(t *testing.T) {
recorder := httptest.NewRecorder()
err := webutils.SendJson(recorder, map[string]any{
"a": 1,
"b": "b",
"c": nil,
})
if err != nil {
t.Fatalf("error while sending json: %v", err)
}
res := recorder.Result()
if ctype := res.Header.Get("Content-Type"); ctype != "application/json" {
t.Fatalf("expected content type \"application/json\", got %v", ctype)
}
// httptest guarantees body to be functional and only returned error will be EOL
// so no need to watch for error
bodyBytes, _ := io.ReadAll(res.Body)
body := string(bodyBytes)
expected := `{"a":1,"b":"b","c":null}`
if body != expected {
t.Fatalf("Expected %v as body, got %v", expected, body)
}
}
func TestSendJsonNotMarshallable(t *testing.T) {
recorder := httptest.NewRecorder()
err := webutils.SendJson(recorder, map[string]any{
"a": 1,
"b": "b",
"c": make(chan any),
})
if err == nil {
t.Fatal("expected json marshalling error")
}
}