Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/uploadcare/api/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def build_request_uri(path, params, method)
end

def prepare_headers(req, _method, _uri, headers)
req.headers['User-Agent'] ||= Uploadcare::Internal::UserAgent.call(config: config)
req.headers['User-Agent'] = Uploadcare::Internal::UserAgent.call(config: config)
req.headers.merge!(headers)
end

Expand Down Expand Up @@ -243,6 +243,7 @@ def private_ip?(address)
def upload_part_request(conn:, request_uri:, data:, timeout:, open_timeout:)
conn.put(request_uri) do |req|
req.headers['Content-Type'] = 'application/octet-stream'
req.headers['User-Agent'] = Uploadcare::Internal::UserAgent.call(config: config)
req.options.timeout = timeout if timeout
req.options.open_timeout = open_timeout if open_timeout
req.body = data
Expand Down
16 changes: 16 additions & 0 deletions spec/uploadcare/api/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
end
end

describe 'User-Agent header' do
it 'sends the SDK User-Agent on REST API requests, not the Faraday default' do
expected_user_agent = Uploadcare::Internal::UserAgent.call(config: config)
stub_request(:get, 'https://api.uploadcare.com/files/')
.with(headers: { 'User-Agent' => expected_user_agent })
.to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' })

rest.get(path: '/files/', params: {}, headers: {}, request_options: {})

expect(
a_request(:get, 'https://api.uploadcare.com/files/')
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
).to have_been_made
end
end

describe '#get' do
it 'returns a successful Result on 200' do
stub_request(:get, 'https://api.uploadcare.com/files/')
Expand Down
31 changes: 31 additions & 0 deletions spec/uploadcare/api/upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,37 @@
end
end

describe 'User-Agent header' do
let(:expected_user_agent) { Uploadcare::Internal::UserAgent.call(config: config) }

it 'sends the SDK User-Agent on Upload API requests, not the Faraday default' do
stub_request(:post, 'https://upload.uploadcare.com/base/')
.with(headers: { 'User-Agent' => expected_user_agent })
.to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' })

upload.post(path: 'base/', params: { 'UPLOADCARE_PUB_KEY' => 'demopublickey' })

expect(
a_request(:post, 'https://upload.uploadcare.com/base/')
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
).to have_been_made
end

it 'sends the SDK User-Agent when uploading parts to a presigned URL' do
presigned_url = 'https://s3.amazonaws.com/bucket/part?signature=abc123'
stub_request(:put, presigned_url)
.with(headers: { 'User-Agent' => expected_user_agent })
.to_return(status: 200, body: '', headers: {})

upload.upload_part_to_url(presigned_url, 'binary-data')

expect(
a_request(:put, presigned_url)
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
).to have_been_made
end
end

describe '#upload_part_to_url' do
let(:presigned_url) { 'https://s3.amazonaws.com/bucket/part?signature=abc123' }

Expand Down