Merge pull request #33 from adlan/case-insensitive-header-check

Do a case-insensitive content-type check
This commit is contained in:
Jaehyun Shin 2017-11-01 11:38:39 +09:00 committed by GitHub
commit 1a3928a6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

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

View File

@ -66,4 +66,20 @@ defmodule ReadabilityHttpTest do
assert result_html =~ ~r/connected computing devices\".<\/p><\/div><\/div>$/
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