Skip to content

Commit

Permalink
feat(ctx): enhance HTTP request handling and testing functionality
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Nov 2, 2024
1 parent 89bceeb commit d076e99
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
37 changes: 30 additions & 7 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ const (
ApplicationForm = "application/x-www-form-urlencoded"
)

// HTTPResponse is basic HTTP response type
type HTTPResponse *httptest.ResponseRecorder
// HTTPResponse wraps the httptest.ResponseRecorder to provide additional
// functionality or to simplify the response handling in tests.
type HTTPResponse struct {
*httptest.ResponseRecorder
}

// HTTPRequest is basic HTTP request type
type HTTPRequest *http.Request
// HTTPRequest is a wrapper around the standard http.Request.
// It embeds the http.Request struct, allowing you to use all the methods
// and fields of http.Request while also providing the ability to extend
// its functionality with additional methods or fields if needed.
type HTTPRequest struct {
*http.Request
}

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

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

// Run execute http request
// Run executes the HTTP request using the provided http.Handler and processes
// the response using the given ResponseFunc. It initializes the test request
// and response writer, serves the HTTP request, and then passes the HTTP
// response and request to the response function.
//
// Parameters:
// - r: The http.Handler that will handle the HTTP request.
// - response: A function that processes the HTTP response and request.
func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
req, w := rc.initTest()
r.ServeHTTP(w, req)
response(w, req)
response(
HTTPResponse{
w,
},
HTTPRequest{
req,
},
)
}
33 changes: 33 additions & 0 deletions gofight_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gofight

import (
"context"
"io"
"net/http"
"testing"
Expand Down Expand Up @@ -53,3 +54,35 @@ func TestBasicHttpHelloWorld(t *testing.T) {
assert.Equal(t, http.StatusOK, r.Code)
})
}

func TestSetContext(t *testing.T) {
r := New()
type contextKey string
const key contextKey = "key"
ctx := context.WithValue(context.Background(), key, "value")

r.GET("/").
SetContext(ctx).
Run(basicEngine(), func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, "value", rq.Context().Value(contextKey("key")))
assert.Equal(t, "Hello World", r.Body.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}

func TestSetContextWithTimeout(t *testing.T) {
r := New()
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()

r.GET("/").
SetContext(ctx).
Run(basicEngine(), func(r HTTPResponse, rq HTTPRequest) {
select {
case <-rq.Context().Done():
assert.Equal(t, context.DeadlineExceeded, rq.Context().Err())
default:
t.Error("expected context to be done")
}
})
}

0 comments on commit d076e99

Please sign in to comment.