Skip to content

Commit

Permalink
chore: Handle invalid language in HelloHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarfelipe committed Aug 24, 2024
1 parent 07a6f86 commit 54b55eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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
19 changes: 19 additions & 0 deletions internal/httpserver/handler_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

0 comments on commit 54b55eb

Please sign in to comment.