-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add coverage for healthcheck server #37
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
573013d
test: Add coverage for healthcheck server
ivov c873399
Merge branch 'main' into add-coverage-for-healthcheck-server
ivov 4cc37c8
Refactor to use new config style
ivov 217f578
Merge branch 'main' into add-coverage-for-healthcheck-server
ivov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,114 @@ | ||||||||||||
package http | ||||||||||||
|
||||||||||||
import ( | ||||||||||||
"encoding/json" | ||||||||||||
"fmt" | ||||||||||||
"net/http" | ||||||||||||
"net/http/httptest" | ||||||||||||
"testing" | ||||||||||||
) | ||||||||||||
|
||||||||||||
func TestHealthCheckHandler(t *testing.T) { | ||||||||||||
tests := []struct { | ||||||||||||
name string | ||||||||||||
method string | ||||||||||||
expectedStatus int | ||||||||||||
wantBody bool | ||||||||||||
}{ | ||||||||||||
{ | ||||||||||||
name: "GET request returns 200 and status ok", | ||||||||||||
method: http.MethodGet, | ||||||||||||
expectedStatus: http.StatusOK, | ||||||||||||
wantBody: true, | ||||||||||||
}, | ||||||||||||
{ | ||||||||||||
name: "POST request returns 405 and status not allowed", | ||||||||||||
method: http.MethodPost, | ||||||||||||
expectedStatus: http.StatusMethodNotAllowed, | ||||||||||||
wantBody: false, | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
|
||||||||||||
for _, tt := range tests { | ||||||||||||
t.Run(tt.name, func(t *testing.T) { | ||||||||||||
req := httptest.NewRequest(tt.method, "/healthz", nil) | ||||||||||||
w := httptest.NewRecorder() | ||||||||||||
|
||||||||||||
handleHealthCheck(w, req) | ||||||||||||
|
||||||||||||
if got := w.Code; got != tt.expectedStatus { | ||||||||||||
t.Errorf("handleHealthCheck() status = %v, want %v", got, tt.expectedStatus) | ||||||||||||
} | ||||||||||||
|
||||||||||||
if tt.wantBody { | ||||||||||||
var response struct { | ||||||||||||
Status string `json:"status"` | ||||||||||||
} | ||||||||||||
|
||||||||||||
if err := json.NewDecoder(w.Body).Decode(&response); err != nil { | ||||||||||||
t.Errorf("failed to decode response body: %v", err) | ||||||||||||
} | ||||||||||||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this would be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
|
||||||||||||
if response.Status != "ok" { | ||||||||||||
t.Errorf("handleHealthCheck() status = %v, want %v", response.Status, "ok") | ||||||||||||
} | ||||||||||||
|
||||||||||||
if contentType := w.Header().Get("Content-Type"); contentType != "application/json" { | ||||||||||||
t.Errorf("handleHealthCheck() Content-Type = %v, want %v", contentType, "application/json") | ||||||||||||
} | ||||||||||||
} | ||||||||||||
}) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
func TestHealthCheckHandlerEncodingError(t *testing.T) { | ||||||||||||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil) | ||||||||||||
|
||||||||||||
failingWriter := &failingWriter{ | ||||||||||||
headers: http.Header{}, | ||||||||||||
} | ||||||||||||
handleHealthCheck(failingWriter, req) | ||||||||||||
|
||||||||||||
if failingWriter.statusCode != http.StatusInternalServerError { | ||||||||||||
t.Errorf("handleHealthCheck() with encoding error, status = %v, want %v", | ||||||||||||
failingWriter.statusCode, http.StatusInternalServerError) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
type failingWriter struct { | ||||||||||||
statusCode int | ||||||||||||
headers http.Header | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (w *failingWriter) Header() http.Header { | ||||||||||||
return w.headers | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (w *failingWriter) Write([]byte) (int, error) { | ||||||||||||
return 0, fmt.Errorf("encoding error") | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (w *failingWriter) WriteHeader(statusCode int) { | ||||||||||||
w.statusCode = statusCode | ||||||||||||
} | ||||||||||||
|
||||||||||||
func TestNewHealthCheckServer(t *testing.T) { | ||||||||||||
server := NewHealthCheckServer("5680") | ||||||||||||
|
||||||||||||
if server == nil { | ||||||||||||
t.Fatal("NewHealthCheckServer() returned nil") | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
if server.Addr != ":5680" { | ||||||||||||
t.Errorf("NewHealthCheckServer() addr = %v, want %v", server.Addr, ":5680") | ||||||||||||
} | ||||||||||||
|
||||||||||||
if server.ReadTimeout != readTimeout { | ||||||||||||
t.Errorf("NewHealthCheckServer() readTimeout = %v, want %v", server.ReadTimeout, readTimeout) | ||||||||||||
} | ||||||||||||
|
||||||||||||
if server.WriteTimeout != writeTimeout { | ||||||||||||
t.Errorf("NewHealthCheckServer() writeTimeout = %v, want %v", server.WriteTimeout, writeTimeout) | ||||||||||||
} | ||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes a bit late to the party since most of the tests have already been written, but maybe we should use a package like
github.com/stretchr/testify/assert
to make these assertions clearer? E.g. this would then beThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I'll start using
testify
from now on and maybe later we can revisit older tests!