Skip to content

Commit

Permalink
test: add test cases and improvements to benchmark test
Browse files Browse the repository at this point in the history
- Add a test case for a GET request with a specific header
- Add an assertion for the response code in the test case
- Add a test case for a GET request with a specific header and query parameter
- Add an assertion for the response code in the test case
- Add a test case for a GET request with a specific header and query parameter
- Add an assertion for the response code in the test case
- Update the benchmark test to use `http.NewRequestWithContext` instead of `http.NewRequest`
- Add error handling for `NewRequestWithContext` in the benchmark test
- Update the comment in the `WithSkipPath` function

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Nov 28, 2023
1 parent 52f4b6a commit 0755e6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -44,20 +45,23 @@ func TestLogger(t *testing.T) {
r.HEAD("/example", func(c *gin.Context) {})
r.OPTIONS("/example", func(c *gin.Context) {})

performRequest(r, "GET", "/example?a=100")
resp := performRequest(r, "GET", "/example?a=100", header{"X-Request-Id", "123"})
assert.Equal(t, 200, resp.Code)
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(r, "POST", "/example?a=100")
assert.Equal(t, 400, resp.Code)
assert.Contains(t, buffer.String(), "400")
assert.Contains(t, buffer.String(), "POST")
assert.Contains(t, buffer.String(), "/example")
assert.Contains(t, buffer.String(), "WRN")

buffer.Reset()
performRequest(r, "PUT", "/example?a=100")
assert.Equal(t, 502, resp.Code)
assert.Contains(t, buffer.String(), "502")
assert.Contains(t, buffer.String(), "PUT")
assert.Contains(t, buffer.String(), "/example")
Expand Down Expand Up @@ -195,7 +199,11 @@ func BenchmarkLogger(b *testing.B) {
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
req, _ := http.NewRequest("GET", "/", nil)
req, err := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
if err != nil {
b.Errorf("NewRequestWithContext() error = %v", err)
return
}
w := httptest.NewRecorder()

for pb.Next() {
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func WithUTC(s bool) Option {
})
}

// WithSkipPath skip URL path by specfic pattern
// WithSkipPath skip URL path by specific pattern
func WithSkipPath(s []string) Option {
return optionFunc(func(c *config) {
c.skipPath = s
Expand Down

0 comments on commit 0755e6f

Please sign in to comment.