Skip to content

Commit

Permalink
feat: introduce response transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
areknoster committed Oct 11, 2024
1 parent 1394a4a commit 730f0ba
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ type TransformRespMode int
const (
// TransformRespModeNone. No transformations are applied to the response. Default value.
TransformRespModeNone TransformRespMode = iota
// TransformRespModeOnlyOnRecord will apply transform only in record mode, so the transformed response would be visible in stored files.
// TransformRespModeOnRecord will apply transform only in record mode, so the transformed response would be visible in stored files.
// In replay mode, whatever is stored in the file will be used, without any transformations.
TransformRespModeOnlyOnRecord
TransformRespModeOnRecord
// TransformRespModeAlways will apply transformation to the response in both record and replay modes.
// When in replay mode, the file is not modified, so the response is not transformed.
TransformRespModeAlways
// TransformModeRuntime will apply transformation only in runtime. This means, that the files would always contain untransformed responses,
// but the response will be transformed on the fly during the test execution.
TransformRespModeRuntime

// TransformRespModeReplay will apply transformation only in replay mode.
// TransformRespModeOnReplay will apply transformation only in replay mode.
// This is useful when there is some other action (e.g. oauth flow) that needs to be performed in record mode,
// but then the response is not feasible in replay mode. (e.g. we want to override the redirect url in oauth responses)
TransformRespModeReplay
TransformRespModeOnReplay
)

func WithResponseTransform(mode TransformRespMode, transform ResponseTransform) Option {
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (n noopRequestSanitizer) SanitizeRequest(req *http.Request) *http.Request {

type noopRequestValidator struct{}

func (n noopRequestValidator) Validate(t T, recorded RequestData, got RequestData) {}
func (n noopRequestValidator) Validate(_ T, recorded RequestData, got RequestData) {}

func Test_configWithDefaults(t *testing.T) {
t.Run("should return default config", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion record_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *recordTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
if d.transformMode == TransformRespModeOnlyOnRecord || d.transformMode == TransformRespModeAlways {
if d.transformMode == TransformRespModeOnRecord || d.transformMode == TransformRespModeAlways {
resp = d.transform.TransformResponse(resp)
}
resp, err = d.dumpRespToFile(respFile, req, resp)
Expand Down
18 changes: 11 additions & 7 deletions record_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestRecordTransport_RoundTrip(t *testing.T) {
expectedBody: "original response body",
},
{
name: "TransformRespModeOnlyOnRecord",
transformMode: TransformRespModeOnlyOnRecord,
name: "TransformRespModeOnRecord",
transformMode: TransformRespModeOnRecord,
applyTransform: true,
expectedBody: "transformed response body",
},
Expand All @@ -62,8 +62,8 @@ func TestRecordTransport_RoundTrip(t *testing.T) {
expectedBody: "transformed response body",
},
{
name: "TransformRespModeReplay",
transformMode: TransformRespModeReplay,
name: "TransformRespModeOnReplay",
transformMode: TransformRespModeOnReplay,
applyTransform: true,
expectedBody: "original response body",
},
Expand All @@ -85,10 +85,12 @@ func TestRecordTransport_RoundTrip(t *testing.T) {
}

sampleResp := func() *http.Response {
return &http.Response{
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("original response body")),
}
defer resp.Body.Close()
return resp
}

transform := ResponseTransformFunc(func(r *http.Response) *http.Response {
Expand Down Expand Up @@ -165,7 +167,7 @@ func TestRecordTransport_RoundTrip(t *testing.T) {
}

expectedStoredBody := "original response body"
if tc.transformMode == TransformRespModeOnlyOnRecord || tc.transformMode == TransformRespModeAlways {
if tc.transformMode == TransformRespModeOnRecord || tc.transformMode == TransformRespModeAlways {
expectedStoredBody = "transformed response body"
}

Expand All @@ -191,10 +193,12 @@ func TestRecordTransport_NilBody(t *testing.T) {
}

sampleResp := func() *http.Response {
return &http.Response{
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(bytes.NewBufferString("response body")),
}
defer resp.Body.Close()
return resp
}

mockRT := &mockRoundTripper{
Expand Down
2 changes: 1 addition & 1 deletion replay_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (d *replayTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Apply transformation based on the transform mode
if d.transform != nil {
switch d.transformMode {
case TransformRespModeAlways, TransformRespModeRuntime, TransformRespModeReplay:
case TransformRespModeAlways, TransformRespModeRuntime, TransformRespModeOnReplay:
respFromFile = d.transform.TransformResponse(respFromFile)
}
}
Expand Down
8 changes: 4 additions & 4 deletions replay_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ func TestReplayTransport_TransformModes(t *testing.T) {
applyTransform: true, // Changed to true to test that transform is not applied
},
{
name: "TransformRespModeOnlyOnRecord",
transformMode: TransformRespModeOnlyOnRecord,
name: "TransformRespModeOnRecord",
transformMode: TransformRespModeOnRecord,
expectedBody: "Wassup, world?",
applyTransform: true, // Changed to true to test that transform is not applied
},
Expand All @@ -195,8 +195,8 @@ func TestReplayTransport_TransformModes(t *testing.T) {
applyTransform: true,
},
{
name: "TransformRespModeReplay",
transformMode: TransformRespModeReplay,
name: "TransformRespModeOnReplay",
transformMode: TransformRespModeOnReplay,
expectedBody: "transformed response body",
applyTransform: true,
},
Expand Down
2 changes: 1 addition & 1 deletion request_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func SchemeValidator() RequestValidator {

// HeadersValidator validates headers of the request.
// It is not sensitive to the order of headers.
// User-Agent and Content-Lenght are removed from the comparison, because it is added deeper in the http client call.
// User-Agent and Content-Length are removed from the comparison, because it is added deeper in the http client call.
func HeadersValidator() RequestValidator {
return RequestValidatorFunc(func(t T, recorded RequestData, got RequestData) {
recordedHeaders := recorded.Headers.Clone()
Expand Down
1 change: 0 additions & 1 deletion request_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func TestRequestValidators(t *testing.T) {

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 {
Expand Down

0 comments on commit 730f0ba

Please sign in to comment.