Skip to content

Commit

Permalink
http client: Fallback to default mime type 'application/octet-stream' (
Browse files Browse the repository at this point in the history
  • Loading branch information
bew authored and bcardiff committed Feb 4, 2019
1 parent a230a55 commit 3e5ba44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 9 additions & 2 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ require "openssl"
require "http/client"
require "http/server"

private def test_server(host, port, read_time = 0)
private def test_server(host, port, read_time = 0, content_type = "text/plain")
server = TCPServer.new(host, port)
begin
spawn do
io = server.accept
sleep read_time
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => "text/plain"}, body: "OK")
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
response.to_io(io)
io.flush
end
Expand Down Expand Up @@ -213,6 +213,13 @@ module HTTP
end
end

it "tests empty Content-Type" do
test_server("localhost", 0, content_type: "") do |server|
client = Client.new("localhost", server.local_address.port)
client.get("/")
end
end

describe "#set_defaults" do
it "sets default Host header" do
client = TestClient.new "www.example.com"
Expand Down
16 changes: 10 additions & 6 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ module HTTP
private def self.check_content_type_charset(body, headers)
return unless body

if content_type = headers["Content-Type"]?
mime_type = MIME::MediaType.parse(content_type)
if charset = mime_type["charset"]?
body.set_encoding(charset, invalid: :skip)
end
end
content_type = headers["Content-Type"]?
return unless content_type

mime_type = MIME::MediaType.parse?(content_type)
return unless mime_type

charset = mime_type["charset"]?
return unless charset

body.set_encoding(charset, invalid: :skip)
end

# :nodoc:
Expand Down

0 comments on commit 3e5ba44

Please sign in to comment.