ch14.5
This commit is contained in:
@@ -18,3 +18,61 @@ func TestPing(t *testing.T) {
|
||||
assert.Equal(t, statusCode, http.StatusOK)
|
||||
assert.Equal(t, body, "OK")
|
||||
}
|
||||
|
||||
func TestSnippetView(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
|
||||
ts := newTestServer(t, app.routes())
|
||||
defer ts.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
urlPath string
|
||||
wantCode int
|
||||
wantBody string
|
||||
}{
|
||||
{
|
||||
name: "Valid ID",
|
||||
urlPath: "/snippet/view/1",
|
||||
wantCode: http.StatusOK,
|
||||
wantBody: "An old silent pond...",
|
||||
},
|
||||
{
|
||||
name: "Non-existend ID",
|
||||
urlPath: "/snippet/view/2",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Negative ID",
|
||||
urlPath: "/snippet/view/-1",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Decimal ID",
|
||||
urlPath: "/snippet/view/1.23",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "String ID",
|
||||
urlPath: "/snippet/view/foo",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Empty ID",
|
||||
urlPath: "/snippet/view/",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
code, _, body := ts.get(t, tt.urlPath)
|
||||
|
||||
assert.Equal(t, code, tt.wantCode)
|
||||
|
||||
if tt.wantBody != "" {
|
||||
assert.StringContains(t, body, tt.wantBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user