Skip to content

Commit 3e29092

Browse files
authored
Merge pull request #190 from uploadcare/fix-upload-api-user-agent
Fix Upload API sending Faraday's default User-Agent
2 parents e76dd6a + 3455456 commit 3e29092

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

lib/uploadcare/api/upload.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def build_request_uri(path, params, method)
159159
end
160160

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

@@ -243,6 +243,7 @@ def private_ip?(address)
243243
def upload_part_request(conn:, request_uri:, data:, timeout:, open_timeout:)
244244
conn.put(request_uri) do |req|
245245
req.headers['Content-Type'] = 'application/octet-stream'
246+
req.headers['User-Agent'] = Uploadcare::Internal::UserAgent.call(config: config)
246247
req.options.timeout = timeout if timeout
247248
req.options.open_timeout = open_timeout if open_timeout
248249
req.body = data

spec/uploadcare/api/rest_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@
3939
end
4040
end
4141

42+
describe 'User-Agent header' do
43+
it 'sends the SDK User-Agent on REST API requests, not the Faraday default' do
44+
expected_user_agent = Uploadcare::Internal::UserAgent.call(config: config)
45+
stub_request(:get, 'https://api.uploadcare.com/files/')
46+
.with(headers: { 'User-Agent' => expected_user_agent })
47+
.to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' })
48+
49+
rest.get(path: '/files/', params: {}, headers: {}, request_options: {})
50+
51+
expect(
52+
a_request(:get, 'https://api.uploadcare.com/files/')
53+
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
54+
).to have_been_made
55+
end
56+
end
57+
4258
describe '#get' do
4359
it 'returns a successful Result on 200' do
4460
stub_request(:get, 'https://api.uploadcare.com/files/')

spec/uploadcare/api/upload_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@
105105
end
106106
end
107107

108+
describe 'User-Agent header' do
109+
let(:expected_user_agent) { Uploadcare::Internal::UserAgent.call(config: config) }
110+
111+
it 'sends the SDK User-Agent on Upload API requests, not the Faraday default' do
112+
stub_request(:post, 'https://upload.uploadcare.com/base/')
113+
.with(headers: { 'User-Agent' => expected_user_agent })
114+
.to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' })
115+
116+
upload.post(path: 'base/', params: { 'UPLOADCARE_PUB_KEY' => 'demopublickey' })
117+
118+
expect(
119+
a_request(:post, 'https://upload.uploadcare.com/base/')
120+
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
121+
).to have_been_made
122+
end
123+
124+
it 'sends the SDK User-Agent when uploading parts to a presigned URL' do
125+
presigned_url = 'https://s3.amazonaws.com/bucket/part?signature=abc123'
126+
stub_request(:put, presigned_url)
127+
.with(headers: { 'User-Agent' => expected_user_agent })
128+
.to_return(status: 200, body: '', headers: {})
129+
130+
upload.upload_part_to_url(presigned_url, 'binary-data')
131+
132+
expect(
133+
a_request(:put, presigned_url)
134+
.with(headers: { 'User-Agent' => %r{\AUploadcareRuby/} })
135+
).to have_been_made
136+
end
137+
end
138+
108139
describe '#upload_part_to_url' do
109140
let(:presigned_url) { 'https://s3.amazonaws.com/bucket/part?signature=abc123' }
110141

0 commit comments

Comments
 (0)