Skip to content

Commit 72e4056

Browse files
ehutzelmanioquatix
authored andcommitted
Fix overriding content-type header for FileParts (#43)
1 parent c98ac86 commit 72e4056

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

lib/parts.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def initialize(boundary, name, io, headers = {})
6262
@io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new(@foot))
6363
end
6464

65-
def build_head(boundary, name, filename, type, content_len, opts = {}, headers = {})
65+
def build_head(boundary, name, filename, type, content_len, opts = {})
6666
opts = opts.clone
67-
67+
6868
trans_encoding = opts.delete("Content-Transfer-Encoding") || "binary"
6969
content_disposition = opts.delete("Content-Disposition") || "form-data"
7070

@@ -76,8 +76,8 @@ def build_head(boundary, name, filename, type, content_len, opts = {}, headers =
7676
part << "Content-ID: #{content_id}\r\n"
7777
end
7878

79-
if headers["Content-Type"] != nil
80-
part << "Content-Type: " + headers["Content-Type"] + "\r\n"
79+
if opts["Content-Type"] != nil
80+
part << "Content-Type: " + opts["Content-Type"] + "\r\n"
8181
else
8282
part << "Content-Type: #{type}\r\n"
8383
end

spec/parts_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Copyright, 2012, by Nick Sieger.
22
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
3-
#
3+
#
44
# Permission is hereby granted, free of charge, to any person obtaining a copy
55
# of this software and associated documentation files (the "Software"), to deal
66
# in the Software without restriction, including without limitation the rights
77
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
# copies of the Software, and to permit persons to whom the Software is
99
# furnished to do so, subject to the following conditions:
10-
#
10+
#
1111
# The above copyright notice and this permission notice shall be included in
1212
# all copies or substantial portions of the Software.
13-
#
13+
#
1414
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -41,7 +41,7 @@ def assert_part_length(part)
4141
def content_type; 'application/data'; end
4242
end
4343
end
44-
44+
4545
it "test_file_with_upload_io" do
4646
expect(Parts::Part.file?(UploadIO.new(__FILE__, "text/plain"))).to be true
4747
end
@@ -82,6 +82,11 @@ def content_type; 'application/data'; end
8282
assert_part_length Parts::FilePart.new("boundary", "multibyte", UploadIO.new(file, "text/plain"))
8383
file.close
8484
end
85+
86+
it "test_force_content_type_header" do
87+
part = Parts::FilePart.new("boundary", "afile", UploadIO.new(TEMP_FILE, "text/plain"), { "Content-Type" => "application/pdf" })
88+
expect(part.to_io.read).to match(/Content-Type: application\/pdf/)
89+
end
8590
end
8691

8792
RSpec.describe Parts::ParamPart do

0 commit comments

Comments
 (0)