Skip to content

Commit 52b031a

Browse files
committed
test: improving test coverage
1 parent 971b5ee commit 52b031a

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

request_test.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,45 @@ func Test_ParseRequest(t *testing.T) {
7373
wantErr: assert.NoError,
7474
wantDetail: nil,
7575
},
76+
{
77+
name: "Body Only - No URL Params",
78+
args: args{
79+
w: httptest.NewRecorder(),
80+
r: func() *http.Request {
81+
body, _ := json.Marshal(TestRequest{ID: 42, Name: "OnlyBody"})
82+
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(body))
83+
return req
84+
}(),
85+
pathParams: []string{},
86+
},
87+
want: &TestRequest{ID: 42, Name: "OnlyBody"},
88+
wantErr: assert.NoError,
89+
},
90+
{
91+
name: "Body Only - Nil Path Params",
92+
args: args{
93+
w: httptest.NewRecorder(),
94+
r: func() *http.Request {
95+
body, _ := json.Marshal(TestRequest{ID: 7, Name: "NilPathParams"})
96+
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(body))
97+
return req
98+
}(),
99+
pathParams: nil,
100+
},
101+
want: &TestRequest{ID: 7, Name: "NilPathParams"},
102+
wantErr: assert.NoError,
103+
},
76104
{
77105
name: "Missing body",
78106
args: args{
79107
w: httptest.NewRecorder(),
80108
r: httptest.NewRequest("POST", "/test/123", nil),
81109
pathParams: []string{"ID"},
82110
},
83-
want: nil,
84-
wantErr: assert.Error,
85-
wantDetail: NewProblemDetails(http.StatusBadRequest, "about:blank", "Validation Error", "One or more fields failed validation."),
111+
want: nil,
112+
wantErr: assert.Error,
113+
wantDetail: NewProblemDetails(http.StatusBadRequest, "about:blank", "Validation Error",
114+
"One or more fields failed validation."),
86115
},
87116
{
88117
name: "Invalid JSON Body",
@@ -95,9 +124,10 @@ func Test_ParseRequest(t *testing.T) {
95124
}(),
96125
pathParams: []string{"ID"},
97126
},
98-
want: nil,
99-
wantErr: assert.Error,
100-
wantDetail: NewProblemDetails(http.StatusBadRequest, "about:blank", "Invalid Request", "invalid character 'i' looking for beginning of object key string"),
127+
want: nil,
128+
wantErr: assert.Error,
129+
wantDetail: NewProblemDetails(http.StatusBadRequest, "about:blank", "Invalid Request",
130+
"invalid character 'i' looking for beginning of object key string"),
101131
},
102132
}
103133

@@ -115,7 +145,9 @@ func Test_ParseRequest(t *testing.T) {
115145
// Check ProblemDetails if an error was expected.
116146
if tt.wantDetail != nil {
117147
rec := w.(*httptest.ResponseRecorder)
118-
assert.Equal(t, "application/problem+json; charset=utf-8", rec.Header().Get("Content-Type"), "Content-Type header mismatch")
148+
assert.Equal(t, "application/problem+json; charset=utf-8",
149+
rec.Header().Get("Content-Type"), "Content-Type header mismatch")
150+
119151
var pd ProblemDetails
120152
decodeErr := json.NewDecoder(rec.Body).Decode(&pd)
121153
assert.NoError(t, decodeErr, "Failed to decode problem details response")

0 commit comments

Comments
 (0)