Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce transform callback. fixes #53 #57

Merged
merged 1 commit into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,17 @@ bin/katt --json base_url=http://httpbin.org my_name=Joe your_name=Mike -- doc/ex
* `validate` to be called with `expected`, `actual`, `params`, `callbacks`
* `progress` to be called with `transaction_result`
* `text_diff` to be called with `text`, `text`
* `transform` to be called with `id`, `katt_request` or `{katt_response, actual_response}`, `params`, `callbacks`

A request can also be configured via HTTP request headers:

* `x-katt-description` would take precedence over the transaction's description
* `x-katt-request-timeout` would take precedence over the `request_timeout` param
* `x-katt-request-sleep` would delay the request for a specific amount of milliseconds
* `x-katt-transform` would call the `tranform` callback with it's value as `id`

A response can also be configured via HTTP response headers:
* `x-katt-transform` would call the `tranform` callback with it's value as `id`

### If you would like to convert a HAR file to an APIB file

Expand Down
51 changes: 39 additions & 12 deletions src/katt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ make_callbacks(Callbacks) ->
, {recall, ?DEFAULT_RECALL_FUN}
, {request, ?DEFAULT_REQUEST_FUN}
, {text_diff, ?DEFAULT_TEXT_DIFF_FUN}
, {transform, ?DEFAULT_TRANSFORM_FUN}
, {validate, ?DEFAULT_VALIDATE_FUN}
], Callbacks).

Expand Down Expand Up @@ -234,10 +235,10 @@ run_transactions( From
Req = Req0#katt_request{headers = Hdrs},
From ! {progress, run_transaction, Description},
Request = make_katt_request(Req, Params, Callbacks),
ExpectedResponse = make_katt_response(Res, Params, Callbacks),
RequestFun = proplists:get_value(request, Callbacks),
ValidateFun = proplists:get_value(validate, Callbacks),
ActualResponse = RequestFun(Request, Params, Callbacks),
ExpectedResponse = make_katt_response(ActualResponse, Res, Params, Callbacks),
ValidationResult = ValidateFun( ExpectedResponse
, ActualResponse
, Params
Expand Down Expand Up @@ -270,7 +271,7 @@ run_transactions( From
{Params, {Count + 1, [Result|Results]}}
end.

make_katt_request( #katt_request{headers=Hdrs0, url=Url0, body=Body0} = Req
make_katt_request( #katt_request{url=Url0, headers=Hdrs0, body=Body0} = Req0
, Params
, Callbacks
) ->
Expand All @@ -284,12 +285,14 @@ make_katt_request( #katt_request{headers=Hdrs0, url=Url0, body=Body0} = Req
Url = make_request_url(Url1, Params),
Hdrs = RecallFun(headers, Hdrs0, Params, Callbacks),
[Hdrs, Body] = RecallFun(body, [Hdrs, Body0], Params, Callbacks),
Req#katt_request{ url = Url
, headers = Hdrs
, body = Body
}.
Req = Req0#katt_request{ url = Url
, headers = Hdrs
, body = Body
},
maybe_transform_request(Req, Params, Callbacks).

make_katt_response( #katt_response{headers=Hdrs0, body=Body0} = Res
make_katt_response( ActualResponse
, #katt_response{headers=Hdrs0, body=Body0} = Res0
, Params
, Callbacks
) ->
Expand All @@ -298,10 +301,11 @@ make_katt_response( #katt_response{headers=Hdrs0, body=Body0} = Res
Hdrs = RecallFun(headers, Hdrs0, Params, Callbacks),
[Hdrs, Body] = RecallFun(body, [Hdrs, Body0], Params, Callbacks),
ParsedBody = ParseFun(Hdrs, Body, Params, Callbacks),
Res#katt_response{ headers = Hdrs
, body = Body
, parsed_body = ParsedBody
}.
Res = Res0#katt_response{ headers = Hdrs
, body = Body
, parsed_body = ParsedBody
},
maybe_transform_response(Res, Params, Callbacks, ActualResponse).

-spec make_request_url( string()
, params()
Expand All @@ -320,8 +324,31 @@ make_request_url(Path, Params) ->
, Path
], "").


make_host(?PROTOCOL_HTTP, Hostname, 80) -> Hostname;
make_host(?PROTOCOL_HTTPS, Hostname, 443) -> Hostname;
make_host(_Proto, Hostname, Port) ->
Hostname ++ ":" ++ katt_util:to_list(Port).

maybe_transform_request(Req0, Params, Callbacks) ->
Hdrs0 = Req0#katt_request.headers,
case proplists:get_value("x-katt-transform", Hdrs0) of
undefined ->
Req0;
TransformId ->
TransformFun = proplists:get_value(transform, Callbacks),
Hdrs = proplists:delete("x-katt-transform", Hdrs0),
Req = Req0#katt_request{headers = Hdrs},
TransformFun(TransformId, Req, Params, Callbacks)
end.

maybe_transform_response(Res0, Params, Callbacks, ActualResponse) ->
Hdrs0 = Res0#katt_response.headers,
case proplists:get_value("x-katt-transform", Hdrs0) of
undefined ->
Res0;
TransformId ->
TransformFun = proplists:get_value(transform, Callbacks),
Hdrs = proplists:delete("x-katt-transform", Hdrs0),
Res = Res0#katt_response{headers = Hdrs},
TransformFun(TransformId, {Res, ActualResponse}, Params, Callbacks)
end.
1 change: 1 addition & 0 deletions src/katt.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
-define(DEFAULT_RECALL_FUN, fun katt_callbacks:recall/4).
-define(DEFAULT_REQUEST_FUN, fun katt_callbacks:request/3).
-define(DEFAULT_TEXT_DIFF_FUN, fun katt_callbacks:text_diff/2).
-define(DEFAULT_TRANSFORM_FUN, fun katt_callbacks:transform/4).
-define(DEFAULT_VALIDATE_FUN, fun katt_callbacks:validate/4).

-type params() :: [{param_name(), param_value()}].
Expand Down
13 changes: 13 additions & 0 deletions src/katt_callbacks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
, validate_type/6
, progress/2
, text_diff/2
, transform/4
]).

%%%_* Includes =================================================================
Expand Down Expand Up @@ -223,6 +224,18 @@ text_diff(A, B) ->
[{text_diff, tdiff:diff(A, B)}].
-endif.

%% @doc Transform a request or a response
%% @end
-spec transform( list()
, request() | {response(), list()}
, params()
, callbacks()
) -> request() | response().
transform(_Id, #katt_request{} = Req, _Params, _Callbacks) ->
Req;
transform(_Id, {#katt_response{} = Res, _Actual}, _Params, _Callbacks) ->
Res.

%%%_* Internal =================================================================

http_request( #katt_request{ method = Method
Expand Down