ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - FabioBatSilva/efrisby: A REST API testing framework for erlang
A REST API testing framework for erlang. Contribute to FabioBatSilva/efrisby development by creating an account on GitHub.
Visit Site

GitHub - FabioBatSilva/efrisby: A REST API testing framework for erlang

GitHub - FabioBatSilva/efrisby: A REST API testing framework for erlang

efrisby

Build Status

A REST API testing framework for erlang inspired by frisby-js

Installation

By adding the following dependency to your rebar.config file :


%% Rebar3 test profiles
{profiles, [
    {test, [
        {deps, [
            {efrisby, "0.2.0"}
        ]}
    ]}
]}.

Basic Usage.

You have to start all efrisby dependencies before using any of the functions

To start in the console run:

$ rebar3 shell

And run the following command to start all of the application it depends on:

   > application:ensure_all_started(efrisby).
%% > ok,[idna,mimerl,certifi,hackney,efrisby]}
   > efrisby:get("http://localhost/api/1.0/users/3.json", [
        {status, 200},
        {content_type, "application/json"},
        {json_types, [
            {<<"id">>, integer},
            {<<"is_admin">>, boolean},
            {<<"username">>, bitstring}
        ]},
        {json, [
            {<<"id">>, 3},
            {<<"is_admin">>, false},
            {<<"username">>, <<"johndoe">>}
        ]}
    ]).
%% > {ok, Response}

Write Tests

efrisby tests start with one by calling one of get, post, put, options, delete, or head to generate an HTTP request and assert the response.

efrisby has many built-in test assertions like :

  • status to easily test HTTP status codes
  • content_type to test content type header
  • headers to test expected HTTP headers
  • json to test expected JSON keys/values
  • json_types to test JSON value types

eq :


{ok, Response} = efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {status, 200},
    {content_type, "application/json; charset=utf-8"},
    {json_types, [
        {<<"id">>, integer},
        {<<"url">>, bitstring},
        {<<"login">>, bitstring}
    ]},
    {json, [
        {<<"id">>, 588172},
        {<<"login">>, <<"FabioBatSilva">>},
        {<<"url">>, <<"https://api.github.com/users/FabioBatSilva">>}
    ]}
]).

> efrisby_resp:json(Response).
[
    {<<"id">>, 588172},
    {<<"login">>, <<"FabioBatSilva">>},
    {<<"url">>, <<"https://api.github.com/users/FabioBatSilva">>}
]

Expectations

Expectation are used to generate assertions on the response. These helpers make testing API response bodies and headers easy with minimal time and effort.

{status, integer()}

Assert that HTTP Status code equals expectation.

efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {status, 200}
]).
%% > {ok, Response}

{headers, list()}

Assert the HTTP response headers.

efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {headers, [
        {<<"content-type">>, <<"application/json; charset=utf-8">>}
    ]}
]).
%% > {ok, Response}

{json, bitstring() | none(), list() | integer() | atom() | bitstring()}

Tests that response JSON body contains the provided keys/values in the response.

efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {json, [
        {<<"id">>, 588172},
        {<<"login">>, <<"FabioBatSilva">>}
    ]}
]).
%% > {ok, Response}

efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {json, ".id", 588172},
    {json, ".login", <<"FabioBatSilva">>}
]).
%% > {ok, Response}

{json_types, bitstring() | none(), list()}

Tests that response JSON body contains the provided keys/values types.

efrisby:get("https://api.github.com/users/FabioBatSilva", [
    {json_types, [
        {<<"id">>, integer},
        {<<"login">>, bitstring}
    ]}
]).
%% > {ok, Response}

Using Paths with json and json_types

Both json and json_types accept a tuple containing a path. The path value can be a nested path separated by periods, like args.foo.mypath, a simple path like .results or . to test the whole JSON value.

efrisby:get("http://httpbin.org/get?foo=bar&bar=baz", [
    {json_types, ".args", [
        {<<"bar">>, bitstring},
        {<<"foo">>, bitstring}
    ]},
    {json, ".args", [
        {<<"foo">>, <<"bar">>},
        {<<"bar">>, <<"baz">>}
    ]}
]).
%% > {ok, Response}

Request Methods

efrisby support your basic HTTP verbs..

  • efrisby:get(url(), expectations(), options())
  • efrisby:head(url(), expectations(), options())
  • efrisby:options(url(), expectations(), options())
  • efrisby:put(url(), body(), expectations(), options())
  • efrisby:post(url(), body(), expectations(), options())
  • efrisby:patch(url(), body(), expectations(), options())
  • efrisby:delete(url(), body(), expectations(), options())

Every request method accepts an optional list of parameters as its last argument, Request options control various aspects of a request including, headers, timeout settings and more.

eq :

-define(OPTIONS, [
    {failure_callback, fun erlang:display/1},
    {http_options, [{timeout, 150000}]},
    {base_url, "https://myapi.local"},
    {headers, [
        {"Accept", "application/json"}
    ]}
]).
  • http_options Options for hackney http client (see https://github.com/benoitc/hackney)
  • failure_callback Assertion failure callback function
  • base_url Base request url
  • headers Http headers

efrisby:get(url(), expectations(), options())

GET request.

efrisby:get("/users/FabioBatSilva", [
    {status, 200}
], ?OPTIONS).
%% > {ok, Response}

efrisby:post(url(), body(), expectations(), options())

POST request.


Body = [
    {<<"login">>, <<"FabioBatSilva">>},
    {<<"name">>, <<"Fabio B. Silva">>}
],

Expectations = [
    {status, 200},
    {content_type, "application/json"},
    {json_type, [
        {id, integer}
    ]}
],

efrisby:post("/users", Body, Expectations, ?OPTIONS).
%% > {ok, Response}

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory