From 4f22b1c59e6b688fb84b397b37742e234bc60e1b Mon Sep 17 00:00:00 2001 From: Andrei Neculau Date: Fri, 6 Jan 2017 00:07:44 +0100 Subject: [PATCH] introduce transform callback. fixes #53 --- README.md | 5 +++++ src/katt.erl | 51 ++++++++++++++++++++++++++++++++---------- src/katt.hrl | 1 + src/katt_callbacks.erl | 13 +++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f53377b..b12ae4a 100644 --- a/README.md +++ b/README.md @@ -176,12 +176,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 diff --git a/src/katt.erl b/src/katt.erl index bae35d9..9acef77 100644 --- a/src/katt.erl +++ b/src/katt.erl @@ -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). @@ -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 @@ -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 ) -> @@ -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 ) -> @@ -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() @@ -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. diff --git a/src/katt.hrl b/src/katt.hrl index d101f5e..aaed350 100644 --- a/src/katt.hrl +++ b/src/katt.hrl @@ -49,6 +49,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()}]. diff --git a/src/katt_callbacks.erl b/src/katt_callbacks.erl index c0dc1f2..b78fc5b 100644 --- a/src/katt_callbacks.erl +++ b/src/katt_callbacks.erl @@ -33,6 +33,7 @@ , validate_type/6 , progress/2 , text_diff/2 + , transform/4 ]). %%%_* Includes ================================================================= @@ -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