Skip to content

Commit

Permalink
Add TestSetResultStateCheckFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jan 24, 2023
1 parent f5594c5 commit c15612a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,28 @@ func TestSetResponseBodyTransformer(t *testing.T) {
tests.AssertEqual(t, user.Username, "我是roc")
tests.AssertEqual(t, user.Email, "[email protected]")
}

func TestSetResultStateCheckFunc(t *testing.T) {
c := tc().SetResultStateCheckFunc(func(resp *Response) ResultState {
if resp.StatusCode == http.StatusOK {
return SuccessState
} else {
return ErrorState
}
})
resp, err := c.R().Get("/status?code=200")
tests.AssertNoError(t, err)
tests.AssertEqual(t, SuccessState, resp.ResultState())

resp, err = c.R().Get("/status?code=201")
tests.AssertNoError(t, err)
tests.AssertEqual(t, ErrorState, resp.ResultState())

resp, err = c.R().Get("/status?code=301")
tests.AssertNoError(t, err)
tests.AssertEqual(t, ErrorState, resp.ResultState())

resp, err = c.R().Get("/status?code=404")
tests.AssertNoError(t, err)
tests.AssertEqual(t, ErrorState, resp.ResultState())
}
10 changes: 10 additions & 0 deletions req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ func handleGet(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
w.Write([]byte("TestGet: text response"))
case "/status":
r.ParseForm()
code := r.FormValue("code")
codeInt, err := strconv.Atoi(code)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(codeInt)
case "/urlencode":
info := &UserInfo{
Username: "我是roc",
Expand Down

0 comments on commit c15612a

Please sign in to comment.