Do a case-insensitive content-type check

This commit is contained in:
Adlan Razalan 2017-10-29 15:09:00 +08:00
parent 389483bae6
commit 49d21b71dc
2 changed files with 17 additions and 1 deletions

View File

@ -113,7 +113,7 @@ defmodule Readability do
headers headers
|> Enum.find( |> Enum.find(
{"Content-Type", "text/plain"}, # default {"Content-Type", "text/plain"}, # default
fn({key, _}) -> key == "Content-Type" end) fn({key, _}) -> String.downcase(key) == "content-type" end)
|> elem(1) |> elem(1)
end end

View File

@ -66,4 +66,20 @@ defmodule ReadabilityHttpTest do
assert result_html =~ ~r/connected computing devices\".<\/p><\/div><\/div>$/ assert result_html =~ ~r/connected computing devices\".<\/p><\/div><\/div>$/
end end
end end
test "response with content-type in different case is parsed correctly" do
# HTTP header keys are case insensitive (RFC2616 - Section 4.2)
url = "https://news.bbc.co.uk/test.html"
content = TestHelper.read_fixture("bbc.html")
response = %HTTPoison.Response{
status_code: 200,
headers: [{"content-Type", "text/html; charset=UTF-8"}],
body: content}
with_mock HTTPoison, [get!: fn(_url, _headers, _opts) -> response end] do
%Readability.Summary{article_html: result_html} = Readability.summarize(url)
assert result_html =~ ~r/connected computing devices\".<\/p><\/div><\/div>$/
end
end
end end