From 83aed9af8fde7643a78c9eb01bfc786e61c8ec55 Mon Sep 17 00:00:00 2001 From: Edmar Felipe Date: Sat, 24 Aug 2024 16:50:45 -0300 Subject: [PATCH 1/4] chore: Add test for hello handler with language set to pt --- internal/httpserver/handler_hello_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/httpserver/handler_hello_test.go b/internal/httpserver/handler_hello_test.go index 07201dc..2bd8fc2 100644 --- a/internal/httpserver/handler_hello_test.go +++ b/internal/httpserver/handler_hello_test.go @@ -30,4 +30,23 @@ func TestHelloHandler(t *testing.T) { t.Fatalf("expected message %s, got %s", "Hello", hello.Message) } }) + + t.Run("Should return a hello response with language set to pt", func(t *testing.T) { + resp, err := http.Get(srv.URL + "?lang=pt") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.StatusCode) + } + defer resp.Body.Close() + + var hello httpserver.HelloResponse + json.NewDecoder(resp.Body).Decode(&hello) + + if hello.Message != "Olá" { + t.Fatalf("expected message %s, got %s", "Olá", hello.Message) + } + }) } From 18cccac3d516f027dc7ed876ed6b8db3e657d782 Mon Sep 17 00:00:00 2001 From: Edmar Felipe Date: Sat, 24 Aug 2024 16:58:32 -0300 Subject: [PATCH 2/4] chore: Update build workflow permissions --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9547dec..cb2b045 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -65,6 +65,8 @@ jobs: runs-on: ubuntu-latest needs: test permissions: - actions: read # to download + contents: read + actions: read # to download code coverage results from "test" job + pull-requests: write # write permission needed to comment on PR steps: - uses: fgrosse/go-coverage-report@v1.0.2 From 07a6f861e889d180a6e68ac203a46729864d77db Mon Sep 17 00:00:00 2001 From: Edmar Felipe Date: Sat, 24 Aug 2024 16:58:44 -0300 Subject: [PATCH 3/4] chore: Update build workflow permissions --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cb2b045..8187c0f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -65,8 +65,8 @@ jobs: runs-on: ubuntu-latest needs: test permissions: - contents: read - actions: read # to download code coverage results from "test" job + contents: read + actions: read # to download code coverage results from "test" job pull-requests: write # write permission needed to comment on PR steps: - uses: fgrosse/go-coverage-report@v1.0.2 From 54b55ebbe7a89edfda8f44120bc1293993a22461 Mon Sep 17 00:00:00 2001 From: Edmar Felipe Date: Sat, 24 Aug 2024 17:05:18 -0300 Subject: [PATCH 4/4] 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) + } + }) }