Created
August 4, 2018 12:54
-
-
Save KamilLelonek/c8ce93ad5945509ecb0a707660b88156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Http.WrapperTest do | |
use ExUnit.Case, async: true | |
alias Http.{Wrapper, Mock} | |
import Mox | |
setup :verify_on_exit! | |
@headers [] | |
test "should pass a proper arguments" do | |
url = "url" | |
payload = %{} | |
opts = [] | |
expect(Mock, :post, fn ^url, ^payload, @headers, ^opts -> | |
{:ok, %HTTPoison.Response{status_code: 200, body: ""}} | |
end) | |
Wrapper.post(url, payload, opts) | |
end | |
test "should return a custom error" do | |
error = {:error, :nxdomain} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert ^error = Wrapper.post("url", "payload", []) | |
end | |
test "should return simple HTTPoison.Error" do | |
reason = "reason" | |
error = {:error, %HTTPoison.Error{reason: reason}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert {:error, ^reason} = Wrapper.post("url", "payload", []) | |
end | |
test "should return complex HTTPoison.Error" do | |
reason = "reason" | |
error = {:error, %HTTPoison.Error{reason: {:error, reason}}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert {:error, ^reason} = Wrapper.post("url", "payload", []) | |
end | |
test "should return 404 response" do | |
body = "body" | |
url = "url" | |
code = 404 | |
error = {:ok, %HTTPoison.Response{status_code: code, body: body}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert { | |
:error, | |
%{ | |
body: ^body, | |
headers: "[]", | |
status_code: ^code, | |
url: ^url | |
} | |
} = Wrapper.post(url, payload, []) | |
end | |
test "should return 400 form response" do | |
body = "body" | |
url = "url" | |
code = 400 | |
error = {:ok, %HTTPoison.Response{status_code: code, body: body}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert { | |
:error, | |
%{ | |
body: ^body, | |
headers: "[]", | |
status_code: ^code, | |
url: ^url | |
} | |
} = Wrapper.post(url, payload, []) | |
end | |
test "should return 422 json response" do | |
body = "body" | |
url = "url" | |
code = 400 | |
error = {:ok, %HTTPoison.Response{status_code: code, body: body}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert { | |
:error, | |
%{ | |
body: ^body, | |
headers: "[]", | |
status_code: ^code, | |
url: ^url | |
} | |
} = Wrapper.post(url, payload, []) | |
end | |
test "should return 200 response" do | |
body = "body" | |
error = {:ok, %HTTPoison.Response{status_code: 200, body: body}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert { | |
:ok, | |
^body | |
} = Wrapper.post("url", "payload", []) | |
end | |
test "should parse 204 response" do | |
body = %{key: "value", valid: true, age: 10} | |
json = Poison.encode!(body) | |
error = {:ok, %HTTPoison.Response{status_code: 200, body: json}} | |
expect(Mock, :post, fn _, _, _, _ -> error end) | |
assert { | |
:ok, | |
^body | |
} = Wrapper.post("url", "payload", []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment