Skip to content

Commit d076e99

Browse files
committed
feat(ctx): enhance HTTP request handling and testing functionality
- Update HTTPResponse and HTTPRequest types to provide additional functionality and simplify response handling. - Modify the Run function to pass wrapped HTTPResponse and HTTPRequest to the response function. - Add a new test for setting context in HTTP requests. - Implement a test for setting context with a timeout, verifying the context's behavior upon expiration. Signed-off-by: appleboy <[email protected]>
1 parent 89bceeb commit d076e99

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

gofight.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,22 @@ const (
7272
ApplicationForm = "application/x-www-form-urlencoded"
7373
)
7474

75-
// HTTPResponse is basic HTTP response type
76-
type HTTPResponse *httptest.ResponseRecorder
75+
// HTTPResponse wraps the httptest.ResponseRecorder to provide additional
76+
// functionality or to simplify the response handling in tests.
77+
type HTTPResponse struct {
78+
*httptest.ResponseRecorder
79+
}
7780

78-
// HTTPRequest is basic HTTP request type
79-
type HTTPRequest *http.Request
81+
// HTTPRequest is a wrapper around the standard http.Request.
82+
// It embeds the http.Request struct, allowing you to use all the methods
83+
// and fields of http.Request while also providing the ability to extend
84+
// its functionality with additional methods or fields if needed.
85+
type HTTPRequest struct {
86+
*http.Request
87+
}
8088

81-
// ResponseFunc response handling func type
89+
// ResponseFunc is a type alias for a function that takes an HTTPResponse and an HTTPRequest as parameters.
90+
// It is used to define a callback function that can handle or process HTTP responses and requests.
8291
type ResponseFunc func(HTTPResponse, HTTPRequest)
8392

8493
// H is HTTP Header Type
@@ -407,9 +416,23 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
407416
return req, w
408417
}
409418

410-
// Run execute http request
419+
// Run executes the HTTP request using the provided http.Handler and processes
420+
// the response using the given ResponseFunc. It initializes the test request
421+
// and response writer, serves the HTTP request, and then passes the HTTP
422+
// response and request to the response function.
423+
//
424+
// Parameters:
425+
// - r: The http.Handler that will handle the HTTP request.
426+
// - response: A function that processes the HTTP response and request.
411427
func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
412428
req, w := rc.initTest()
413429
r.ServeHTTP(w, req)
414-
response(w, req)
430+
response(
431+
HTTPResponse{
432+
w,
433+
},
434+
HTTPRequest{
435+
req,
436+
},
437+
)
415438
}

gofight_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gofight
22

33
import (
4+
"context"
45
"io"
56
"net/http"
67
"testing"
@@ -53,3 +54,35 @@ func TestBasicHttpHelloWorld(t *testing.T) {
5354
assert.Equal(t, http.StatusOK, r.Code)
5455
})
5556
}
57+
58+
func TestSetContext(t *testing.T) {
59+
r := New()
60+
type contextKey string
61+
const key contextKey = "key"
62+
ctx := context.WithValue(context.Background(), key, "value")
63+
64+
r.GET("/").
65+
SetContext(ctx).
66+
Run(basicEngine(), func(r HTTPResponse, rq HTTPRequest) {
67+
assert.Equal(t, "value", rq.Context().Value(contextKey("key")))
68+
assert.Equal(t, "Hello World", r.Body.String())
69+
assert.Equal(t, http.StatusOK, r.Code)
70+
})
71+
}
72+
73+
func TestSetContextWithTimeout(t *testing.T) {
74+
r := New()
75+
ctx, cancel := context.WithTimeout(context.Background(), 0)
76+
defer cancel()
77+
78+
r.GET("/").
79+
SetContext(ctx).
80+
Run(basicEngine(), func(r HTTPResponse, rq HTTPRequest) {
81+
select {
82+
case <-rq.Context().Done():
83+
assert.Equal(t, context.DeadlineExceeded, rq.Context().Err())
84+
default:
85+
t.Error("expected context to be done")
86+
}
87+
})
88+
}

0 commit comments

Comments
 (0)