From 636c36323115f2f235cc71bbae8e9afa7f2158fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Tue, 31 May 2022 17:16:36 +0200 Subject: [PATCH] feat(client): add one more test for Client.GetAuthor (#4) --- internal/lubimyczytac/client_test.go | 23 +++++++++++++++++++++-- main.go | 4 ++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/lubimyczytac/client_test.go b/internal/lubimyczytac/client_test.go index 83ec801..df22522 100644 --- a/internal/lubimyczytac/client_test.go +++ b/internal/lubimyczytac/client_test.go @@ -27,8 +27,12 @@ func TestClient_GetAuthor(t *testing.T) { t.Parallel() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != fmt.Sprintf("/autor/%s/x", author.ID) || r.Method != http.MethodGet { - w.WriteHeader(http.StatusBadRequest) + if r.URL.Path != fmt.Sprintf("/autor/%s/x", author.ID) { + w.WriteHeader(http.StatusNotFound) + return + } + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) return } @@ -45,4 +49,19 @@ func TestClient_GetAuthor(t *testing.T) { }) } }) + + t.Run("ERR: author not found", func(t *testing.T) { + t.Parallel() + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer srv.Close() + + result, err := lubimyczytac. + NewClient(srv.Client(), lubimyczytac.WithBaseURL(srv.URL)). + GetAuthor(context.Background(), "123") + assert.ErrorIs(t, err, lubimyczytac.ErrAuthorNotFound) + assert.Zero(t, result) + }) } diff --git a/main.go b/main.go index 1a3f38b..d1360a0 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,12 @@ import ( ) const ( - defaultClientTimeout = 5 * time.Second + clientTimeout = 5 * time.Second ) func main() { httpSrv := newServer(newRouter(lubimyczytac.NewClient(&http.Client{ - Timeout: defaultClientTimeout, + Timeout: clientTimeout, }))) go func(httpSrv *http.Server) {