Shit ton of tests
This commit is contained in:
parent
99b00887a8
commit
ccf98c2f6e
13 changed files with 1157 additions and 1 deletions
33
http/chain_test.go
Normal file
33
http/chain_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package webutils_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
)
|
||||
|
||||
func TestChainMiddlewares(t *testing.T) {
|
||||
wrapper1 := webutils.HandlerBuilder(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Add("X-Foo", "bar")
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
wrapper2 := webutils.HandlerBuilder(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Add("X-Foos", r.Header.Get("X-Foo")+"baz")
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if x := r.Header.Get("X-Foos"); x != "barbaz" {
|
||||
t.Fatalf(`Expected header "X-Foos" to be "barbaz", got %v`, x)
|
||||
}
|
||||
})
|
||||
handler := webutils.ChainMiddlewares(baseHandler, wrapper1, wrapper2)
|
||||
recorder := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
handler.ServeHTTP(recorder, req)
|
||||
}
|
23
http/context_test.go
Normal file
23
http/context_test.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package webutils_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
)
|
||||
|
||||
func TestContextValsMiddleware(t *testing.T) {
|
||||
builder := webutils.ContextValsMiddleware(map[any]any{"foo": "bar"})
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if val, _ := r.Context().Value("foo").(string); val != "bar" {
|
||||
t.Fatalf(`Expected context value "foo" to be "bar", got %v`, val)
|
||||
}
|
||||
})
|
||||
wrapped := builder(baseHandler)
|
||||
recorder := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
wrapped.ServeHTTP(recorder, req)
|
||||
// At this point baseHandler should have been called and checked for the context value
|
||||
}
|
235
http/httpErr_test.go
Normal file
235
http/httpErr_test.go
Normal file
|
@ -0,0 +1,235 @@
|
|||
package webutils_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/maputils"
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
)
|
||||
|
||||
func TestHttpErr(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.HttpErr(recorder, 1, "some error", 500)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
res := recorder.Result()
|
||||
if res.StatusCode != 500 {
|
||||
t.Fatalf("Expected status code 500, got %v", res.StatusCode)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
expected := `{"id": 1, "message": "some error"}`
|
||||
if body != expected {
|
||||
t.Fatalf("Expected body %q, got %q", expected, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProblemDetailsStatusOnly(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.ProblemDetailsStatusOnly(recorder, 500)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
res := recorder.Result()
|
||||
if ct := res.Header.Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Fatalf("Expected content type to be \"application/problem+json\" got %v", ct)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
data := map[string]any{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
if err != nil {
|
||||
t.Fatalf("response body is not json: %v", err)
|
||||
}
|
||||
if dt, ok := data["type"].(string); !ok || dt != "about:blank" {
|
||||
t.Fatalf(`Expected key "type" as "about:blank", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["title"].(string); !ok || dt != http.StatusText(500) {
|
||||
t.Fatalf(`Expected key "title" as "%v", got %v`, http.StatusText(500), dt)
|
||||
}
|
||||
if dt, ok := data["status"].(float64); !ok || dt != 500 {
|
||||
t.Fatalf(`Expected key "status" as 500, got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["reference"].(string); !ok || dt != "RFC 9457" {
|
||||
t.Fatalf(`Expected key "reference" as "RFC 9457", got %v`, dt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProblemDetailsNoDetailsOrExtras(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.ProblemDetails(
|
||||
recorder,
|
||||
500,
|
||||
"error-type",
|
||||
"error-title",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
res := recorder.Result()
|
||||
if ct := res.Header.Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Fatalf("Expected content type to be \"application/problem+json\" got %v", ct)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
data := map[string]any{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
if err != nil {
|
||||
t.Fatalf("response body is not json: %v", err)
|
||||
}
|
||||
|
||||
expectedFields := []string{"type", "title", "status"}
|
||||
gotFields := maputils.KeysFromMap(data)
|
||||
if !sliceutils.CompareUnordered(gotFields, expectedFields) {
|
||||
t.Fatalf("Expected fields %v, got %v", expectedFields, gotFields)
|
||||
}
|
||||
if dt, ok := data["type"].(string); !ok || dt != "error-type" {
|
||||
t.Fatalf(`Expected key "type" as "error-type", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["title"].(string); !ok || dt != "error-title" {
|
||||
t.Fatalf(`Expected key "title" as "error-title", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["status"].(float64); !ok || dt != 500 {
|
||||
t.Fatalf(`Expected key "status" as 500, got %v`, dt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProblemDetailsNoDetailsWithExtras(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.ProblemDetails(
|
||||
recorder,
|
||||
500,
|
||||
"error-type",
|
||||
"error-title",
|
||||
nil,
|
||||
map[string]any{"foo": "bar"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
res := recorder.Result()
|
||||
if ct := res.Header.Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Fatalf("Expected content type to be \"application/problem+json\" got %v", ct)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
data := map[string]any{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
if err != nil {
|
||||
t.Fatalf("response body is not json: %v", err)
|
||||
}
|
||||
|
||||
expectedFields := []string{"type", "title", "status", "foo"}
|
||||
gotFields := maputils.KeysFromMap(data)
|
||||
if !sliceutils.CompareUnordered(gotFields, expectedFields) {
|
||||
t.Fatalf("Expected fields %v, got %v", expectedFields, gotFields)
|
||||
}
|
||||
if dt, ok := data["type"].(string); !ok || dt != "error-type" {
|
||||
t.Fatalf(`Expected key "type" as "error-type", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["title"].(string); !ok || dt != "error-title" {
|
||||
t.Fatalf(`Expected key "title" as "error-title", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["status"].(float64); !ok || dt != 500 {
|
||||
t.Fatalf(`Expected key "status" as 500, got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["foo"].(string); !ok || dt != "bar" {
|
||||
t.Fatalf(`Expected key "foo" as "bar", got %v`, dt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProblemDetailsWithDetailsNoExtras(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.ProblemDetails(
|
||||
recorder,
|
||||
500,
|
||||
"error-type",
|
||||
"error-title",
|
||||
other.IntoPointer("error-details"),
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
res := recorder.Result()
|
||||
if ct := res.Header.Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Fatalf("Expected content type to be \"application/problem+json\" got %v", ct)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
data := map[string]any{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
if err != nil {
|
||||
t.Fatalf("response body is not json: %v", err)
|
||||
}
|
||||
|
||||
expectedFields := []string{"type", "title", "status", "detail"}
|
||||
gotFields := maputils.KeysFromMap(data)
|
||||
if !sliceutils.CompareUnordered(gotFields, expectedFields) {
|
||||
t.Fatalf("Expected fields %v, got %v", expectedFields, gotFields)
|
||||
}
|
||||
if dt, ok := data["type"].(string); !ok || dt != "error-type" {
|
||||
t.Fatalf(`Expected key "type" as "error-type", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["title"].(string); !ok || dt != "error-title" {
|
||||
t.Fatalf(`Expected key "title" as "error-title", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["status"].(float64); !ok || dt != 500 {
|
||||
t.Fatalf(`Expected key "status" as 500, got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["detail"].(string); !ok || dt != "error-details" {
|
||||
t.Fatalf(`Expected key "detail" as "error-details", got %v`, dt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProblemDetailsWithDetailsWithExtras(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
err := webutils.ProblemDetails(
|
||||
recorder,
|
||||
500,
|
||||
"error-type",
|
||||
"error-title",
|
||||
other.IntoPointer("error-details"),
|
||||
map[string]any{"foo": "bar", "type": "should not be set in the end"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
res := recorder.Result()
|
||||
if ct := res.Header.Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Fatalf("Expected content type to be \"application/problem+json\" got %v", ct)
|
||||
}
|
||||
body := string(other.Must(io.ReadAll(res.Body)))
|
||||
data := map[string]any{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
if err != nil {
|
||||
t.Fatalf("response body is not json: %v", err)
|
||||
}
|
||||
|
||||
expectedFields := []string{"type", "title", "status", "detail", "foo"}
|
||||
gotFields := maputils.KeysFromMap(data)
|
||||
if !sliceutils.CompareUnordered(gotFields, expectedFields) {
|
||||
t.Fatalf("Expected fields %v, got %v", expectedFields, gotFields)
|
||||
}
|
||||
if dt, ok := data["type"].(string); !ok || dt != "error-type" {
|
||||
t.Fatalf(`Expected key "type" as "error-type", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["title"].(string); !ok || dt != "error-title" {
|
||||
t.Fatalf(`Expected key "title" as "error-title", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["status"].(float64); !ok || dt != 500 {
|
||||
t.Fatalf(`Expected key "status" as 500, got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["detail"].(string); !ok || dt != "error-details" {
|
||||
t.Fatalf(`Expected key "detail" as "error-details", got %v`, dt)
|
||||
}
|
||||
if dt, ok := data["foo"].(string); !ok || dt != "bar" {
|
||||
t.Fatalf(`Expected key "foo" as "bar", got %v`, dt)
|
||||
}
|
||||
}
|
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