Skip to content

Commit

Permalink
add errro test
Browse files Browse the repository at this point in the history
  • Loading branch information
glossd committed Nov 13, 2024
1 parent f6921f3 commit 9a7a3be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ func TestRequest_ResponseEmpty(t *testing.T) {
}
}

func TestRequest_Error(t *testing.T) {
_, err := Request[string]("400.error")
if err == nil {
t.Fatal(err)
}

if err.Status != 400 {
t.Errorf("expected status 400")
}
if err.Headers["Content-type"] != "text/plain" {
t.Errorf("expected headers")
}
if err.Body != "Bad Request" {
t.Errorf("expected body")
}
}

func TestPostString(t *testing.T) {
j, err := Post[M]("echo.me", `{"hello":"whosthere"}`)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func mockDNS(url string, req *http.Request) *mockResponse {
case "my.ip":
return &mockResponse{Status: 200, Headers: map[string][]string{"Content-type": {"text/plain"}}, Body: `8.8.8.8`}
case "400.error":
return &mockResponse{Status: 400, Body: `Bad Request`}
return &mockResponse{Status: 400, Headers: map[string][]string{"Content-type": {"text/plain"}}, Body: `Bad Request`}
case "echo.me":
body, err := io.ReadAll(req.Body)
if err != nil {
Expand Down

0 comments on commit 9a7a3be

Please sign in to comment.