add request body read tests

This commit is contained in:
Aneurin Barker Snook
2023-10-10 21:50:39 +01:00
parent ec541df476
commit a9ef6b837b
+61
View File
@@ -0,0 +1,61 @@
package rest
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestReadRequestJSON(t *testing.T) {
type TestInput struct {
Title string `json:"title"`
Serves int `json:"serves"`
Author string `json:"author,omitempty"`
}
type TestCase struct {
Req *http.Request
Input TestInput
Err error
}
testCases := []TestCase{
{
Req: httptest.NewRequest("POST", "/recipes", nil),
Err: errors.New("unexpected end of JSON input"),
},
{
Req: httptest.NewRequest("POST", "/recipes", strings.NewReader(`{"title":"Gnocchi","serves":2}`)),
Input: TestInput{Title: "Gnocchi", Serves: 2},
},
{
Req: httptest.NewRequest("POST", "/recipes", strings.NewReader(`{"title":"Spaghetti","serves":4,"author":"Mom"}`)),
Input: TestInput{Title: "Spaghetti", Serves: 4, Author: "Mom"},
},
}
for i, tc := range testCases {
t.Logf("(%d) Testing request body against %+v", i, tc.Input)
input := TestInput{}
err := ReadRequestJSON(tc.Req, &input)
if err != nil {
if tc.Err != nil {
// Compare error strings, as json.SyntaxError isn't directly comparable
if err.Error() != tc.Err.Error() {
t.Errorf("Expected error %v, got %v", tc.Err, err)
}
} else {
t.Errorf("Expected error %v, got %v", tc.Err, err)
}
continue
}
if input != tc.Input {
t.Errorf("Expected %v, got %v", tc.Input, input)
}
}
}