From 9a7a3be662cf06ab4a59e0c7617aa7eb2f5b9521 Mon Sep 17 00:00:00 2001 From: Dennis Gloss Date: Wed, 13 Nov 2024 01:07:14 +0100 Subject: [PATCH] add errro test --- fetch_test.go | 17 +++++++++++++++++ mock.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fetch_test.go b/fetch_test.go index 3182811..a65ad49 100644 --- a/fetch_test.go +++ b/fetch_test.go @@ -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 { diff --git a/mock.go b/mock.go index 21233ee..46140c0 100644 --- a/mock.go +++ b/mock.go @@ -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 {