GithubHelp home page GithubHelp logo

etest_http's Introduction

etest_http

etest_http is a supplementary library for etest that makes it easy to test http APIs by providing useful assertions and helper methods.

Example Test Case

-module (my_api_test).
-compile (export_all).

% etest macros
-include_lib ("etest/include/etest.hrl").
% etest_http macros
-include_lib ("etest_http/include/etest_http.hrl").

test_hello_world() ->
    Response = ?perform_get("http://localhost:3000/hello/world"),
    ?assert_status(200, Response),
    ?assert_body_contains("Hello", Response),
    ?assert_body("Hello World", Response).

Installation

Install etest_http by adding it as a rebar dependency:

% rebar.config:
{deps, [
    {etest_http, "", {git, "git://github.com/wooga/etest_http.git"}},
    % ...
]}.

Then run rebar get-deps to synchronize your dependencies.

Usage

There are two kinds of macros which etest_http provides:

The request macros provide a simple API to perform HTTP request. They return a response record on which subsequent assertions are performed.

The assertion macros provide useful http API related assertions.

Request Macros

perform_get

Perform a GET request, returns a response record.

Res = ?perform_get(Url).
Res = ?perform_get(Url, Headers).
Res = ?perform_get(Url, Headers, Queries).

% Url = string()
% header() :: {string() | binary() | atom(), string() | binary()}
% Headers = [header()]
% query() :: [{string(), string()}]
% Queries = [query()]

perform_post

Perform a POST request, returns a response record.

Res = ?perform_post(Url).
Res = ?perform_post(Url, Headers).
Res = ?perform_post(Url, Headers, Body).
Res = ?perform_post(Url, Headers, Body, Queries).

% Url = string()
% header() :: {string() | binary() | atom(), string() | binary()}
% Headers = [header()]
% query() :: [{string(), string()}]
% Queries = [query()]
% Body = binary()

Assertion Macros

assert_contains

Assert that the Haystack contains the Needle, fail with assert_contains otherwise.

% Test Macro.
?assert_contains(Needle, Haystack).

% Test Generator Macro.
?assert_contains(Needle, Haystack).

% Needle = string()
% Haystack = string()

assert_body_contains

Assert that the body received with the response Res contains the string Needle, fail with assert_contains otherwise too.

% Test Macro.
?assert_body_contains(Needle, Res).

% Test Generator Macro.
?assert_body_contains(Needle, Res).

% Needle = string()

assert_body

Assert that the body received with the response Res is exactly Body, fail with assert_equal otherwise.

?assert_body(Body, Res).

% Test Generator Macro.
?assert_body(Body, Res).

% Body = binary()

Planned for future versions:

  • Support for regular expressions

assert_header

Assert that the presence of a header HeaderName in the headers received with the response Res, fail with assert_header otherwise.

% Test Macro.
?assert_header(HeaderName, Res).

% Test Generator Macro.
?assert_header(HeaderName, Res).

% HeaderName = string()

% Examples:
?assert_header_val("X-Signature", Res).

assert_header_value

Assert that the headers received with the response Res has a header HeaderName with value HeaderValue, fail with assert_header_value otherwise.

% Test Macro.
?assert_header_val(HeaderName, HeaderValue, Res).

% Test Generator Macro.
?assert_header_val(HeaderName, HeaderValue, Res).

% HeaderName = string()
% HeaderVal = string()

% Examples:
?assert_header_val(
    "X-Signature", "42UVoTWYp9I-wdWJsQYUyEXRoCI1wCXmOVPqwdV8LU0=", Res).

assert_status

Assert that the response's HTTP status code is StatusCode, fail with assert_status otherwise.

% Test Macro.
?assert_status(StatusCode, Res).

% Test Generator Macro.
?assert_status(StatusCode, Res).

% StatusCode = integer()

% Example:
?assert_status(200, Res).

assert_json

Assert that the body received with the response Res contains a JSON structure equal to JsonStruct, fail with assert_equal otherwise.

% Test Macro.
?assert_json(JsonStruct, Res).

% Test Generator Macro.
?assert_json(JsonStruct, Res).

% JsonStruct = orddict()

% Example:
?assert_json([{message, "Hello World"}], Res).

assert_json_key

Assert that the body received with the response Res contains a JSON object, which has a key Key with arbitrary contents, fail with assert_json_key otherwise.

% Test Macro.
?assert_json_key(Key, Res).

% Test Generator Macro.
?assert_json_key(Key, Res).

% Key = binary() | [binary()]

% Examples:
?assert_json_key(<<"message">>, Res).
?assert_json_key([<<"messages">>, <<"en">>], Res).

assert_json_value

Assert that the body received with the response Res contains a JSON object, which under the key Key contains exactly Val, fail with assert_json_value otherwise.

% Test Macro.
?assert_json_value(Key, Val, Res).

% Test Generator Macro.
?assert_json_value(Key, Val, Res).

% Key = binary() | [binary()]
% Val = atom() | binary() | list() | integer() | float() | {list()}

% Examples:
?assert_json_value(<<"message">>, <<"Hello World">>, Res).
?assert_json_value(<<"should_reload">>, true, Res).
?assert_json_value([<<"messages">>, <<"de">>], <<"Hallo Welt">>, Res).

etest_http's People

Contributors

dry avatar hukl avatar kachayev avatar lpgauth avatar phuesler avatar puzza007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

etest_http's Issues

perform_request follows redirects by default

Hi,
I've noticed that perform_request/5 always follows redirects. Is this a wanted behavior?

I added the HttpOptions {autoredirect, false} to be able to properly test 302s...

lpgauth@abf23d0

from http://www.erlang.org/doc/man/httpc.html

autoredirect

Should the client automatically retrieve the information from the new URI and return that as the result     
instead of a 30X-result code.

Note that for some 30X-result codes automatic redirect is not allowed. In these cases the 30X-result 
will always be returned.

Defaults to true

perform_request ignores content-type header

For other requests than get and delete, perform_request always sets an empty string as the content-type. The httpc api is a bit weird but this needs to be fixed.

https://github.com/wooga/etest_http/blob/master/src/etest_http.erl#L19

probably by adding a perform_post marco which accepts and additional content-type argument and defaulting the others to url-form-encoded

OR

look in the headers dict for /content-type/i field and pass that into the content-type parameter for httpc:request

Create new relaes

Hi,
can you create new release from latest code form master.
Best Regards,
Pawel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.