-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: draft implementation of request validation
- Loading branch information
1 parent
d020eec
commit d1c0e09
Showing
7 changed files
with
101 additions
and
43 deletions.
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
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,50 @@ | ||
package hypert | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
// RequestData is some data related to the request, that can be used to create filename in the NamingScheme's FileNames method implementations or during request validation. | ||
// The fields are cloned from request's fields and their modification will not affect actual request's values. | ||
type RequestData struct { | ||
Header http.Header | ||
URL *url.URL | ||
BodyBytes []byte | ||
} | ||
|
||
func cloneURL(u *url.URL) *url.URL { | ||
if u == nil { // this shouldn't actually happen, unless there is very weird injected clients' transport setup | ||
return nil | ||
} | ||
var userInfo *url.Userinfo | ||
if u.User != nil { | ||
userInfoCopy := *u.User | ||
userInfo = &userInfoCopy | ||
} | ||
uCopy := *u | ||
uCopy.User = userInfo | ||
return &uCopy | ||
} | ||
|
||
func requestDataFromRequest(t *testing.T, req *http.Request) RequestData { | ||
if req.Body == nil { | ||
req.Body = http.NoBody | ||
} | ||
var originalReqBody bytes.Buffer | ||
teeReader := io.TeeReader(req.Body, &originalReqBody) | ||
req.Body = io.NopCloser(&originalReqBody) | ||
gotBodyBytes, err := io.ReadAll(teeReader) | ||
if err != nil { | ||
t.Fatal("hypert: got error when reading request body") | ||
} | ||
|
||
return RequestData{ | ||
Header: req.Header.Clone(), | ||
URL: cloneURL(req.URL), | ||
BodyBytes: gotBodyBytes, | ||
} | ||
} |
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,24 @@ | ||
package hypert | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// RequestValidator does assertions, that allows to make assertions on request that was caught by TestClient in the replay mode. | ||
type RequestValidator interface { | ||
Validate(t *testing.T, recorded RequestData, got RequestData) | ||
} | ||
|
||
type RequestValidatorFunc func(t *testing.T, recorded RequestData, got RequestData) | ||
|
||
func (f RequestValidatorFunc) Validate(t *testing.T, recorded RequestData, got RequestData) { | ||
f(t, recorded, got) | ||
} | ||
|
||
func ComposedRequestValidator(validators ...RequestValidator) RequestValidator { | ||
return RequestValidatorFunc(func(t *testing.T, recorded RequestData, got RequestData) { | ||
for _, validator := range validators { | ||
validator.Validate(t, recorded, got) | ||
} | ||
}) | ||
} |