Shit ton of tests
This commit is contained in:
parent
99b00887a8
commit
ccf98c2f6e
13 changed files with 1157 additions and 1 deletions
45
http/json_test.go
Normal file
45
http/json_test.go
Normal 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")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue