Skip to content

Commit

Permalink
Merge pull request #3 from edmarfelipe/feat/code_coverage
Browse files Browse the repository at this point in the history
chore: Add test for hello handler with language set to pt
  • Loading branch information
edmarfelipe authored Aug 24, 2024
2 parents 20026cf + 54b55eb commit 1e3cbfc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion internal/httpserver/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
38 changes: 38 additions & 0 deletions internal/httpserver/handler_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,42 @@ 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)
}
})

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)
}
})
}

0 comments on commit 1e3cbfc

Please sign in to comment.