Permalink
Browse files

Merge pull request #392 from calonso/improve_em_http_request_cookies_…

…support

Improved cookies support for WebMockHttpClient
  • Loading branch information...
bblimke committed Jun 1, 2014
2 parents 16a7259 + 3866449 commit 03914e287743c903847049811557039c650732b0
@@ -133,6 +133,16 @@ def stubbed_webmock_response
@stubbed_webmock_response
end
+ def get_response_cookie(name)
+ name = name.to_s
+
+ raw_cookie = response_header.cookie
+ raw_cookie = [raw_cookie] if raw_cookie.is_a? String
+
+ cookie = raw_cookie.select { |c| c.start_with? name }.first
+ cookie and cookie.split('=', 2)[1]
+ end
+
private
def build_webmock_response
@@ -189,15 +199,21 @@ def make_raw_response(response)
headers["Content-Length"] = body.bytesize unless headers["Content-Length"]
headers.each do |header, value|
- value = value.join(", ") if value.is_a?(Array)
+ if header =~ /set-cookie/i
+ [value].flatten.each do |cookie|
+ response_string << "#{header}: #{cookie}"
+ end
+ else
+ value = value.join(", ") if value.is_a?(Array)
- # WebMock's internal processing will not handle the body
- # correctly if the header indicates that it is chunked, unless
- # we also create all the chunks.
- # It's far easier just to remove the header.
- next if header =~ /transfer-encoding/i && value =~/chunked/i
+ # WebMock's internal processing will not handle the body
+ # correctly if the header indicates that it is chunked, unless
+ # we also create all the chunks.
+ # It's far easier just to remove the header.
+ next if header =~ /transfer-encoding/i && value =~/chunked/i
- response_string << "#{header}: #{value}"
+ response_string << "#{header}: #{value}"
+ end
end if headers
response_string << "" << body
@@ -0,0 +1,77 @@
+require 'spec_helper'
+
+describe EventMachine::WebMockHttpClient do
+
+ describe 'get_response_cookie' do
+
+ before(:each) do
+ stub_request(:get, "http://example.org/").
+ to_return(
+ :status => 200,
+ :body => "",
+ :headers => { 'Set-Cookie' => cookie_string }
+ )
+ end
+
+ describe 'success' do
+
+ context 'with only one cookie' do
+
+ let(:cookie_name) { 'name_of_the_cookie' }
+ let(:cookie_value) { 'value_of_the_cookie' }
+ let(:cookie_string) { "#{cookie_name}=#{cookie_value}" }
+
+ it 'successfully gets the cookie' do
+ EM.run {
+ http = EventMachine::HttpRequest.new('http://example.org').get
+
+ http.errback { fail(http.error) }
+ http.callback {
+ http.get_response_cookie(cookie_name).should == cookie_value
+ EM.stop
+ }
+ }
+ end
+ end
+
+ context 'with several cookies' do
+
+ let(:cookie_name) { 'name_of_the_cookie' }
+ let(:cookie_value) { 'value_of_the_cookie' }
+ let(:cookie_2_name) { 'name_of_the_2nd_cookie' }
+ let(:cookie_2_value) { 'value_of_the_2nd_cookie' }
+ let(:cookie_string) { %W(#{cookie_name}=#{cookie_value} #{cookie_2_name}=#{cookie_2_value}) }
+
+ it 'successfully gets both cookies' do
+ EM.run {
+ http = EventMachine::HttpRequest.new('http://example.org').get
+
+ http.errback { fail(http.error) }
+ http.callback {
+ http.get_response_cookie(cookie_name).should == cookie_value
+ http.get_response_cookie(cookie_2_name).should == cookie_2_value
+ EM.stop
+ }
+ }
+ end
+ end
+ end
+
+ describe 'failure' do
+
+ let(:cookie_string) { 'a=b' }
+
+ it 'returns nil when no cookie is found' do
+ EM.run {
+ http = EventMachine::HttpRequest.new('http://example.org').get
+
+ http.errback { fail(http.error) }
+ http.callback {
+ http.get_response_cookie('not_found_cookie').should == nil
+ EM.stop
+ }
+ }
+ end
+ end
+ end
+end

0 comments on commit 03914e2

Please sign in to comment.