Skip to content

Commit

Permalink
fix: start testing request validator properly and fix arised issues
Browse files Browse the repository at this point in the history
  • Loading branch information
areknoster committed Sep 24, 2024
1 parent 40189ac commit 31bca7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions request_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ func HeadersValidator() RequestValidator {
for key := range recordedHeaders {
recordedHeader, gotHeader := recordedHeaders.Get(key), got.Headers.Get(key)
if recordedHeader == "SANITIZED" {
got.Headers.Del(key)
continue
}
if recordedHeader != gotHeader {
t.Errorf("expected header '%s' to be '%s', got '%s'", key, recordedHeader, gotHeader)
}
got.Headers.Del(key)

}
for key := range got.Headers {
t.Errorf("unexpected header '%s' with value '%s'", key, got.Headers.Get(key))
Expand Down
18 changes: 16 additions & 2 deletions request_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,29 @@ func TestRequestValidators(t *testing.T) {
got: RequestData{Headers: http.Header{"Key1": []string{"Value1"}}},
expectErr: false,
},
{
name: "HeadersValidator_Sanitized_Both",
validator: HeadersValidator(),
recorded: RequestData{Headers: http.Header{"Key1": []string{"SANITIZED"}}},
got: RequestData{Headers: http.Header{"Key1": []string{"SANITIZED"}}},
expectErr: false,
},
{
name: "HeadersValidator_SanitizedOneAnotherEmpty",
validator: HeadersValidator(),
recorded: RequestData{Headers: http.Header{"Key1": []string{"SANITIZED"}}},
got: RequestData{Headers: http.Header{"Key1": []string{}}},
expectErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

mT := &mockT{}
tc.validator.Validate(mT, tc.recorded, tc.got)
if tc.expectErr && !mT.failed {
t.Error("expected error, got none")
if tc.expectErr != mT.failed {
t.Errorf("expected error value mismatch. Expected %v, got %v", tc.expectErr, mT.failed)
}
})
}
Expand Down

0 comments on commit 31bca7e

Please sign in to comment.