Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong request arguments handling for httpc adapter #38

Merged
merged 1 commit into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions fixture/vcr_cassettes/example_httpc_request_4.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
"httpc_options": [],
"http_options": []
},
"request_body": "",
"url": "http://example.com"
},
"response": {
"body": "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
"headers": {
"cache-control": "max-age=604800",
"date": "Sun, 30 Nov 2014 11:15:00 GMT",
"date": "Tue, 12 Jan 2016 13:12:40 GMT",
"accept-ranges": "bytes",
"etag": "\"359670651\"",
"server": "ECS (cpm/F9FC)",
"server": "ECS (ewr/15BD)",
"vary": "Accept-Encoding",
"content-length": "1270",
"content-type": "text/html",
"expires": "Sun, 07 Dec 2014 11:15:00 GMT",
"expires": "Tue, 19 Jan 2016 13:12:40 GMT",
"last-modified": "Fri, 09 Aug 2013 23:54:35 GMT",
"x-cache": "HIT",
"x-ec-custom-error": "1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"request": {
"body": "",
"headers": {
"Content-Type": "text/html"
},
"method": "get",
"options": {
"httpc_options": {
"body_format": "binary"
},
"http_options": {
"connect_timeout": "3000",
"timeout": "5000"
}
},
"request_body": "",
"url": "http://example.com"
},
"response": {
"body": "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
"headers": {
"cache-control": "max-age=604800",
"date": "Tue, 12 Jan 2016 13:12:40 GMT",
"accept-ranges": "bytes",
"etag": "\"359670651\"",
"server": "ECS (ewr/15BD)",
"vary": "Accept-Encoding",
"content-length": "1270",
"content-type": "text/html",
"expires": "Tue, 19 Jan 2016 13:12:40 GMT",
"last-modified": "Fri, 09 Aug 2013 23:54:35 GMT",
"x-cache": "HIT",
"x-ec-custom-error": "1"
},
"status_code": [
"HTTP/1.1",
200,
"OK"
],
"type": "ok"
}
}
]
15 changes: 7 additions & 8 deletions lib/exvcr/adapter/httpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ defmodule ExVCR.Adapter.Httpc do
Generate key for searching response.
"""
def generate_keys_for_request(request) do
if Enum.count(request) <= 2 do
[url: Enum.fetch!(request, 0), method: :get]
else
url = Enum.fetch!(request, 1) |> elem(0)
method = Enum.fetch!(request, 0)
request_body = Enum.fetch(request, 3) |> parse_request_body

[url: url, method: method, request_body: request_body]
case request do
[method, {url, _} | _] ->
[url: url, method: method, request_body: nil]
[method, {url, _, _, body} | _] ->
[url: url, method: method, request_body: body]
[url | _] ->
[url: url, method: :get, request_body: nil]
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/adapter_httpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ defmodule ExVCR.Adapter.HttpcTest do
end
end

test "example httpc request/4 with additional options" do
use_cassette "example_httpc_request_4_additional_options" do
{:ok, {{_, 200, _reason_phrase}, _headers, body}} = :httpc.request(
:get,
{'http://example.com', [{'Content-Type', 'text/html'}]},
[connect_timeout: 3000, timeout: 5000],
body_format: :binary)
assert to_string(body) =~ ~r/Example Domain/
end
end

test "example httpc request error" do
use_cassette "example_httpc_request_error" do
{:error, {reason, _detail}} = :httpc.request('http://invalidurl')
Expand Down