Skip to content

Commit e1e3453

Browse files
Request validation middleware for go-chi server (#278)
* feat: request validator for go-chi or net/http * feat: add chi request validator in example
1 parent e523822 commit e1e3453

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

request_helpers.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ func (r *RequestBuilder) WithCookieNameValue(name, value string) *RequestBuilder
129129
return r.WithCookie(&http.Cookie{Name: name, Value: value})
130130
}
131131

132-
// This function performs the request, it takes a pointer to a testing context
133-
// to print messages, and a pointer to an echo context for request handling.
134-
func (r *RequestBuilder) Go(t *testing.T, e *echo.Echo) *CompletedRequest {
132+
// GoWithHTTPHandler performs the request, it takes a pointer to a testing context
133+
// to print messages, and a http handler for request handling.
134+
func (r *RequestBuilder) GoWithHTTPHandler(t *testing.T, handler http.Handler) *CompletedRequest {
135135
if r.Error != nil {
136136
// Fail the test if we had an error
137137
t.Errorf("error constructing request: %s", r.Error)
@@ -151,13 +151,19 @@ func (r *RequestBuilder) Go(t *testing.T, e *echo.Echo) *CompletedRequest {
151151
}
152152

153153
rec := httptest.NewRecorder()
154-
e.ServeHTTP(rec, req)
154+
handler.ServeHTTP(rec, req)
155155

156156
return &CompletedRequest{
157157
Recorder: rec,
158158
}
159159
}
160160

161+
// Go performs the request, it takes a pointer to a testing context
162+
// to print messages, and a pointer to an echo context for request handling.
163+
func (r *RequestBuilder) Go(t *testing.T, e *echo.Echo) *CompletedRequest {
164+
return r.GoWithHTTPHandler(t, e)
165+
}
166+
161167
// This is the result of calling Go() on the request builder. We're wrapping the
162168
// ResponseRecorder with some nice helper functions.
163169
type CompletedRequest struct {

0 commit comments

Comments
 (0)