From 54b55ebbe7a89edfda8f44120bc1293993a22461 Mon Sep 17 00:00:00 2001 From: Edmar Felipe Date: Sat, 24 Aug 2024 17:05:18 -0300 Subject: [PATCH] chore: Handle invalid language in HelloHandler --- internal/httpserver/handler_hello.go | 2 +- internal/httpserver/handler_hello_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/httpserver/handler_hello.go b/internal/httpserver/handler_hello.go index 5096a45..98cb9c4 100644 --- a/internal/httpserver/handler_hello.go +++ b/internal/httpserver/handler_hello.go @@ -21,7 +21,7 @@ func HelloHandler(w http.ResponseWriter, r *http.Request) { } if _, ok := msgs[lang]; !ok { - WriteJSON(w, http.StatusBadRequest, ErrorResponse{Error: "invalid language"}) + WriteBadRequest(w, "invalid language") return } diff --git a/internal/httpserver/handler_hello_test.go b/internal/httpserver/handler_hello_test.go index 2bd8fc2..5e814bd 100644 --- a/internal/httpserver/handler_hello_test.go +++ b/internal/httpserver/handler_hello_test.go @@ -49,4 +49,23 @@ func TestHelloHandler(t *testing.T) { t.Fatalf("expected message %s, got %s", "Olá", hello.Message) } }) + + t.Run("Should return error response with invalid language", func(t *testing.T) { + resp, err := http.Get(srv.URL + "?lang=invalid") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if resp.StatusCode != http.StatusBadRequest { + t.Fatalf("expected status code %d, got %d", http.StatusBadRequest, resp.StatusCode) + } + defer resp.Body.Close() + + var errResp httpserver.ErrorResponse + json.NewDecoder(resp.Body).Decode(&errResp) + + if errResp.Error != "invalid language" { + t.Fatalf("expected error message %s, got %s", "invalid language", errResp.Error) + } + }) }