webmock 1.24.0 → 1.24.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.24.1
4
+
5
+ * HTTPClient adapter supports reading basic authentication credentials directly from Authorization header.
6
+
7
+ Thanks to [Michiel Karnebeek](https://github.com/mkarnebeek)
8
+
3
9
  ## 1.24.0
4
10
 
5
11
  * Enabled support for Curb > 0.8.6
data/README.md CHANGED
@@ -1034,6 +1034,7 @@ People who submitted patches and new features or suggested improvements. Many th
1034
1034
  * Alexey Zapparov
1035
1035
  * Pablo Brasero
1036
1036
  * Cedric Pimenta
1037
+ * Michiel Karnebeek
1037
1038
 
1038
1039
  For a full list of contributors you can visit the
1039
1040
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -172,9 +172,6 @@ if defined?(::HTTPClient)
172
172
  uri.port = req.header.request_uri.port
173
173
  uri = uri.omit(:userinfo)
174
174
 
175
- auth = www_auth.basic_auth
176
- auth.challenge(req.header.request_uri, nil)
177
-
178
175
  @request_filter.each do |filter|
179
176
  filter.filter_request(req)
180
177
  end
@@ -186,11 +183,9 @@ if defined?(::HTTPClient)
186
183
  end
187
184
  headers = headers_from_session(uri).merge(headers)
188
185
 
189
- if (auth_cred = auth.get(req)) && auth.scheme == 'Basic'
190
- userinfo = WebMock::Util::Headers.decode_userinfo_from_header(auth_cred)
191
- userinfo = WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
192
- headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v =~ /^Basic / } #we added it to url userinfo
193
- uri.userinfo = userinfo
186
+ if auth_cred = auth_cred_from_www_auth(req) || auth_cred_from_headers(headers)
187
+ remove_authorization_header headers
188
+ uri.userinfo = userinfo_from_auth_cred auth_cred
194
189
  end
195
190
 
196
191
  signature = WebMock::RequestSignature.new(
@@ -208,6 +203,38 @@ if defined?(::HTTPClient)
208
203
  signature
209
204
  end
210
205
 
206
+ def userinfo_from_auth_cred auth_cred
207
+ userinfo = WebMock::Util::Headers.decode_userinfo_from_header(auth_cred)
208
+ WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
209
+ end
210
+
211
+ def remove_authorization_header headers
212
+ headers.reject! do |k, v|
213
+ next unless k =~ /[Aa]uthorization/
214
+ if v.is_a? Array
215
+ v.reject! { |v| v =~ /^Basic / }
216
+ v.length == 0
217
+ elsif v.is_a? String
218
+ v =~ /^Basic /
219
+ end
220
+ end
221
+ end
222
+
223
+ def auth_cred_from_www_auth(req)
224
+ auth = www_auth.basic_auth
225
+ auth.challenge(req.header.request_uri, nil)
226
+ auth.get(req) if auth.scheme == 'Basic'
227
+ end
228
+
229
+ def auth_cred_from_headers(headers)
230
+ headers.each do |k,v|
231
+ next unless k =~ /[Aa]uthorization/
232
+ return v if v.is_a?(String) && v =~ /^Basic /
233
+ v.each { |v| return v if v =~ /^Basic / } if v.is_a? Array
234
+ end
235
+ nil
236
+ end
237
+
211
238
  def webmock_responses
212
239
  @webmock_responses ||= Hash.new do |hash, request_signature|
213
240
  hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.24.0' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.24.1' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -186,4 +186,12 @@ describe "HTTPClient" do
186
186
  expect(@response.body).to eq body
187
187
  end
188
188
  end
189
+
190
+ context 'credentials' do
191
+ it 'are detected when manually specifying Authorization header' do
192
+ stub_request(:get, 'username:password@www.example.com').to_return(:status => 200)
193
+ headers = {'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='}
194
+ expect(http_request(:get, 'http://www.example.com/', {:headers => headers}).status).to eql('200')
195
+ end
196
+ end
189
197
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- hash: 119
4
+ hash: 117
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 24
9
- - 0
10
- version: 1.24.0
9
+ - 1
10
+ version: 1.24.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bartosz Blimke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2016-02-19 00:00:00 +01:00
18
+ date: 2016-02-23 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency