Skip to content

Commit

Permalink
💪 Add test pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Omochice committed Oct 1, 2021
1 parent 8e501c8 commit 70c896b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ func TestValidateResponse(t *testing.T) {
}
}
}
// TODO test when body is valid/invalid as json
// test when body is valid/invalid as json
invalidResp := http.Response{
Status: "444 not exists error",
StatusCode: 444,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte("test"))),
}
err = ValidateResponse(&invalidResp)
if err == nil {
t.Fatalf("If status code is invalid, should occur error\nActual: %d", invalidResp.StatusCode)
} else if !strings.HasSuffix(err.Error(), "]") {
t.Fatalf("If body is invalid as json, error is formated as %s",
"`Invalid response [statuscode statustext]`")
}

expectedMessage := "This is test"
validResp := http.Response{
Status: "444 not exists error",
StatusCode: 444,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fmt.Sprintf(`{"message": "%s"}`, expectedMessage)))),
}
err = ValidateResponse(&validResp)
if err == nil {
t.Fatalf("If is status code invalid, should occur error\nActual: %d", validResp.StatusCode)
} else if !strings.HasSuffix(err.Error(), expectedMessage) {
t.Fatalf("If body is valid as json, error suffix should have `%s`\nActual: %s",
expectedMessage, err.Error())
}
}

0 comments on commit 70c896b

Please sign in to comment.