Skip to content

Commit

Permalink
Merge pull request #31 from Jesus/fix-content-length-error
Browse files Browse the repository at this point in the history
Fix Content-Length error
  • Loading branch information
Jesus authored Sep 18, 2017
2 parents 3240a1a + 62c70ed commit 973b41f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/dropbox_api/endpoints/content_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def build_request(params, content)
'Dropbox-API-Arg' => JSON.dump(params),
'Content-Type' => 'application/octet-stream'
}
if body.respond_to?(:length)
headers['Content-Length'] = body.length.to_s
elsif body.respond_to?(:stat)
headers['Content-Length'] = body.stat.size.to_s
end

return body, headers
end
Expand Down
41 changes: 41 additions & 0 deletions spec/endpoints/content_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe DropboxApi::Endpoints::ContentUpload do
subject { described_class.new(DropboxApi::ConnectionBuilder.new("bearer")) }

context 'Content-Length header' do
context 'for string body' do
it "is calculated and appended to headers" do
body = "tested content"

_, headers = subject.build_request({}, body)

expect(headers['Content-Length']).to eq(body.length.to_s)
end
end

context 'for File body' do
it "is calculated and appended to headers" do
begin
content = "tested content"
file = Tempfile.new('dropbox_api')
file << content

_, headers = subject.build_request({}, file)

expect(headers['Content-Length']).to eq(content.length.to_s)
ensure
file.close
end
end
end

context 'for nil' do
it 'is not in headers' do
body = nil

_, headers = subject.build_request({}, body)

expect(headers).to_not have_key("Content-Length")
end
end
end
end

0 comments on commit 973b41f

Please sign in to comment.