Skip to content

Commit

Permalink
Remove some status codes not supported by cowboy from response module
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardolins committed Feb 4, 2018
1 parent a799c85 commit ba7ad95
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 60 deletions.
42 changes: 1 addition & 41 deletions lib/fake_server/http/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,13 @@ defmodule FakeServer.HTTP.Response do
"""
def partial_content(body \\ "", headers \\ %{}), do: new(206, body, headers)

@doc """
Creates a new response with status code 207
"""
def multi_status(body \\ "", headers \\ %{}), do: new(207, body, headers)

@doc """
Creates a new response with status code 208
"""
def already_reported(body \\ "", headers \\ %{}), do: new(208, body, headers)

@doc """
Creates a new response with status code 226
"""
def im_used(body \\ "", headers \\ %{}), do: new(226, body, headers)

@doc """
Returns a list with all 4xx HTTP methods available
"""
def all_4xx do
[
bad_request(),
unauthorized(),
payment_required(),
forbidden(),
not_found(),
method_not_allowed(),
Expand All @@ -102,15 +86,13 @@ defmodule FakeServer.HTTP.Response do
unsupported_media_type(),
expectation_failed(),
im_a_teapot(),
misdirected_request(),
unprocessable_entity(),
locked(),
failed_dependency(),
upgrade_required(),
precondition_required(),
too_many_requests(),
request_header_fields_too_large(),
unavailable_for_legal_reasons()
request_header_fields_too_large()
]
end

Expand All @@ -124,11 +106,6 @@ defmodule FakeServer.HTTP.Response do
"""
def unauthorized(body \\ "", headers \\ %{}), do: new(401, body, headers)

@doc """
Creates a new response with status code 402
"""
def payment_required(body \\ "", headers \\ %{}), do: new(402, body, headers)

@doc """
Creates a new response with status code 403
"""
Expand Down Expand Up @@ -204,11 +181,6 @@ defmodule FakeServer.HTTP.Response do
"""
def im_a_teapot(body \\ "", headers \\ %{}), do: new(418, body, headers)

@doc """
Creates a new response with status code 421
"""
def misdirected_request(body \\ "", headers \\ %{}), do: new(421, body, headers)

@doc """
Creates a new response with status code 422
"""
Expand Down Expand Up @@ -244,12 +216,6 @@ defmodule FakeServer.HTTP.Response do
"""
def request_header_fields_too_large(body \\ "", headers \\ %{}), do: new(431, body, headers)

@doc """
Creates a new response with status code 451
"""
def unavailable_for_legal_reasons(body \\ "", headers \\ %{}), do: new(451, body, headers)


@doc """
Returns a list with all 5xx HTTP methods available
"""
Expand All @@ -263,7 +229,6 @@ defmodule FakeServer.HTTP.Response do
http_version_not_supported(),
variant_also_negotiates(),
insufficient_storage(),
loop_detected(),
not_extended(),
network_authentication_required()
]
Expand Down Expand Up @@ -309,11 +274,6 @@ defmodule FakeServer.HTTP.Response do
"""
def insufficient_storage(body \\ "", headers \\ %{}), do: new(507, body, headers)

@doc """
Creates a new response with status code 508
"""
def loop_detected(body \\ "", headers \\ %{}), do: new(508, body, headers)

@doc """
Creates a new response with status code 510
"""
Expand Down
22 changes: 3 additions & 19 deletions test/fake_server/http/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ defmodule ResponseTest do
assert Response.no_content == %Response{code: 204, body: "", headers: %{}}
assert Response.reset_content == %Response{code: 205, body: "", headers: %{}}
assert Response.partial_content == %Response{code: 206, body: "", headers: %{}}
assert Response.multi_status == %Response{code: 207, body: "", headers: %{}}
assert Response.already_reported == %Response{code: 208, body: "", headers: %{}}
assert Response.im_used == %Response{code: 226, body: "", headers: %{}}
end

test "returns the correspondent status code with a json string as body" do
Expand All @@ -46,9 +43,6 @@ defmodule ResponseTest do
assert Response.no_content(~s<{"status_kind": "2xx"}>) == %Response{code: 204, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
assert Response.reset_content(~s<{"status_kind": "2xx"}>) == %Response{code: 205, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
assert Response.partial_content(~s<{"status_kind": "2xx"}>) == %Response{code: 206, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
assert Response.multi_status(~s<{"status_kind": "2xx"}>) == %Response{code: 207, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
assert Response.already_reported(~s<{"status_kind": "2xx"}>) == %Response{code: 208, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
assert Response.im_used(~s<{"status_kind": "2xx"}>) == %Response{code: 226, body: ~s<{"status_kind": "2xx"}>, headers: %{}}
end

test "returns the correspondent status code with a map as response body" do
Expand All @@ -59,24 +53,20 @@ defmodule ResponseTest do
assert Response.no_content(%{status_kind: "2xx"}) == %Response{code: 204, body: %{status_kind: "2xx"}, headers: %{}}
assert Response.reset_content(%{status_kind: "2xx"}) == %Response{code: 205, body: %{status_kind: "2xx"}, headers: %{}}
assert Response.partial_content(%{status_kind: "2xx"}) == %Response{code: 206, body: %{status_kind: "2xx"}, headers: %{}}
assert Response.multi_status(%{status_kind: "2xx"}) == %Response{code: 207, body: %{status_kind: "2xx"}, headers: %{}}
assert Response.already_reported(%{status_kind: "2xx"}) == %Response{code: 208, body: %{status_kind: "2xx"}, headers: %{}}
assert Response.im_used(%{status_kind: "2xx"}) == %Response{code: 226, body: %{status_kind: "2xx"}, headers: %{}}
end
end

describe "4XX" do
test "all_4xx responds a list with all 4xx status code" do
assert length(Response.all_4xx) == 27
assert length(Response.all_4xx) == 24
Enum.each(Response.all_4xx, fn(response) ->
assert response.code >= 400 && response.code <= 451
assert response.code >= 400 && response.code <= 431
end)
end

test "returns the correspondent status code with a map as response body" do
assert Response.bad_request(%{status_kind: "4xx"})== %Response{code: 400, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.unauthorized(%{status_kind: "4xx"})== %Response{code: 401, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.payment_required(%{status_kind: "4xx"})== %Response{code: 402, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.forbidden(%{status_kind: "4xx"})== %Response{code: 403, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.not_found(%{status_kind: "4xx"})== %Response{code: 404, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.method_not_allowed(%{status_kind: "4xx"})== %Response{code: 405, body: %{status_kind: "4xx"}, headers: %{}}
Expand All @@ -92,21 +82,18 @@ defmodule ResponseTest do
assert Response.unsupported_media_type(%{status_kind: "4xx"})== %Response{code: 415, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.expectation_failed(%{status_kind: "4xx"})== %Response{code: 417, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.im_a_teapot(%{status_kind: "4xx"})== %Response{code: 418, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.misdirected_request(%{status_kind: "4xx"})== %Response{code: 421, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.unprocessable_entity(%{status_kind: "4xx"})== %Response{code: 422, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.locked(%{status_kind: "4xx"})== %Response{code: 423, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.failed_dependency(%{status_kind: "4xx"})== %Response{code: 424, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.upgrade_required(%{status_kind: "4xx"})== %Response{code: 426, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.precondition_required(%{status_kind: "4xx"})== %Response{code: 428, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.too_many_requests(%{status_kind: "4xx"})== %Response{code: 429, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.request_header_fields_too_large(%{status_kind: "4xx"})== %Response{code: 431, body: %{status_kind: "4xx"}, headers: %{}}
assert Response.unavailable_for_legal_reasons(%{status_kind: "4xx"})== %Response{code: 451, body: %{status_kind: "4xx"}, headers: %{}}
end

test "returns the correspondent status code with a json string as response body" do
assert Response.bad_request(~s<{"status_kind":"4xx"}>)== %Response{code: 400, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.unauthorized(~s<{"status_kind":"4xx"}>)== %Response{code: 401, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.payment_required(~s<{"status_kind":"4xx"}>)== %Response{code: 402, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.forbidden(~s<{"status_kind":"4xx"}>)== %Response{code: 403, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.not_found(~s<{"status_kind":"4xx"}>)== %Response{code: 404, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.method_not_allowed(~s<{"status_kind":"4xx"}>)== %Response{code: 405, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
Expand All @@ -122,21 +109,19 @@ defmodule ResponseTest do
assert Response.unsupported_media_type(~s<{"status_kind":"4xx"}>)== %Response{code: 415, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.expectation_failed(~s<{"status_kind":"4xx"}>)== %Response{code: 417, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.im_a_teapot(~s<{"status_kind":"4xx"}>)== %Response{code: 418, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.misdirected_request(~s<{"status_kind":"4xx"}>)== %Response{code: 421, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.unprocessable_entity(~s<{"status_kind":"4xx"}>)== %Response{code: 422, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.locked(~s<{"status_kind":"4xx"}>)== %Response{code: 423, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.failed_dependency(~s<{"status_kind":"4xx"}>)== %Response{code: 424, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.upgrade_required(~s<{"status_kind":"4xx"}>)== %Response{code: 426, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.precondition_required(~s<{"status_kind":"4xx"}>)== %Response{code: 428, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.too_many_requests(~s<{"status_kind":"4xx"}>)== %Response{code: 429, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.request_header_fields_too_large(~s<{"status_kind":"4xx"}>)== %Response{code: 431, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
assert Response.unavailable_for_legal_reasons(~s<{"status_kind":"4xx"}>)== %Response{code: 451, body: ~s<{"status_kind":"4xx"}>, headers: %{}}
end
end

describe "5xx" do
test "all_5xx responds a list with all 5xx status code" do
assert length(Response.all_5xx) == 11
assert length(Response.all_5xx) == 10
Enum.each(Response.all_5xx, fn(response) ->
assert response.code >= 500 && response.code <= 511
end)
Expand All @@ -151,7 +136,6 @@ defmodule ResponseTest do
assert Response.http_version_not_supported == %Response{code: 505, body: "", headers: %{}}
assert Response.variant_also_negotiates == %Response{code: 506, body: "", headers: %{}}
assert Response.insufficient_storage == %Response{code: 507, body: "", headers: %{}}
assert Response.loop_detected == %Response{code: 508, body: "", headers: %{}}
assert Response.not_extended == %Response{code: 510, body: "", headers: %{}}
assert Response.network_authentication_required == %Response{code: 511, body: "", headers: %{}}
end
Expand Down
37 changes: 37 additions & 0 deletions test/integration/fake_server_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -468,5 +468,42 @@ defmodule FakeServer.FakeServerIntegrationTest do
assert response.body == ~s<{"test":false}>
end
end

test_with_server "can handle all 2xx status codes" do
response_list = [
Response.ok,
Response.created,
Response.accepted,
Response.non_authoritative_information,
Response.no_content,
Response.reset_content,
Response.partial_content
]

route "/", response_list

Enum.each(response_list, fn(response) ->
get_response = HTTPoison.get! FakeServer.address <> "/"
assert get_response.status_code == response.code
end)
end

test_with_server "can handle all 4xx status codes" do
route "/", Response.all_4xx

Enum.each(Response.all_4xx, fn(response) ->
get_response = HTTPoison.get! FakeServer.address <> "/"
assert get_response.status_code == response.code
end)
end

test_with_server "can handle all 5xx status codes" do
route "/", Response.all_5xx

Enum.each(Response.all_5xx, fn(response) ->
get_response = HTTPoison.get! FakeServer.address <> "/"
assert get_response.status_code == response.code
end)
end
end

0 comments on commit ba7ad95

Please sign in to comment.