2009-07-19 19:17:33 -04:00
require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , 'spec_helper' ) )
2008-07-27 11:52:18 -04:00
2008-07-28 10:49:53 -04:00
describe HTTParty do
2009-01-28 23:41:01 -05:00
before ( :each ) do
@klass = Class . new
@klass . instance_eval { include HTTParty }
end
2009-12-05 21:00:36 -05:00
describe " AllowedFormats deprecated " do
before do
Kernel . stub ( :warn )
end
2012-04-15 22:51:39 -04:00
2009-12-05 21:00:36 -05:00
it " warns with a deprecation message " do
Kernel . should_receive ( :warn ) . with ( " Deprecated: Use HTTParty::Parser::SupportedFormats " )
HTTParty :: AllowedFormats
end
it " returns HTTPart::Parser::SupportedFormats " do
HTTParty :: AllowedFormats . should == HTTParty :: Parser :: SupportedFormats
end
end
2012-02-15 14:46:01 -05:00
2010-11-24 12:27:45 -05:00
describe " pem " do
it 'should set the pem content' do
@klass . pem 'PEM-CONTENT'
@klass . default_options [ :pem ] . should == 'PEM-CONTENT'
end
it " should set the password to nil if it's not provided " do
@klass . pem 'PEM-CONTENT'
@klass . default_options [ :pem_password ] . should be_nil
end
it 'should set the password' do
@klass . pem 'PEM-CONTENT' , 'PASSWORD'
@klass . default_options [ :pem_password ] . should == 'PASSWORD'
end
end
2009-12-05 21:00:36 -05:00
2013-10-22 06:45:03 -04:00
describe " pkcs12 " do
it 'should set the p12 content' do
@klass . pkcs12 'P12-CONTENT' , 'PASSWORD'
@klass . default_options [ :p12 ] . should == 'P12-CONTENT'
end
it 'should set the password' do
@klass . pkcs12 'P12-CONTENT' , 'PASSWORD'
@klass . default_options [ :p12_password ] . should == 'PASSWORD'
end
end
2012-08-20 09:08:25 -04:00
describe 'ssl_version' do
it 'should set the ssl_version content' do
@klass . ssl_version :SSLv3
@klass . default_options [ :ssl_version ] . should == :SSLv3
end
end
2012-11-30 14:08:17 -05:00
describe 'ciphers' do
it 'should set the ciphers content' do
@klass . default_options [ :ciphers ] . should be_nil
@klass . ciphers 'RC4-SHA'
@klass . default_options [ :ciphers ] . should == 'RC4-SHA'
end
end
2012-03-28 11:51:31 -04:00
describe 'http_proxy' do
it 'should set the address' do
@klass . http_proxy 'proxy.foo.com' , 80
options = @klass . default_options
options [ :http_proxyaddr ] . should == 'proxy.foo.com'
options [ :http_proxyport ] . should == 80
end
it 'should set the proxy user and pass when they are provided' do
@klass . http_proxy 'proxy.foo.com' , 80 , 'user' , 'pass'
options = @klass . default_options
options [ :http_proxyuser ] . should == 'user'
options [ :http_proxypass ] . should == 'pass'
end
end
2008-07-28 12:08:21 -04:00
describe " base uri " do
2009-01-28 23:41:01 -05:00
before ( :each ) do
@klass . base_uri ( 'api.foo.com/v1' )
2008-11-08 13:59:57 -05:00
end
2008-11-08 11:18:25 -05:00
it " should have reader " do
2009-01-28 23:41:01 -05:00
@klass . base_uri . should == 'http://api.foo.com/v1'
2008-07-27 11:52:18 -04:00
end
2009-09-08 23:34:21 -04:00
2008-11-08 11:18:25 -05:00
it 'should have writer' do
2009-01-28 23:41:01 -05:00
@klass . base_uri ( 'http://api.foobar.com' )
@klass . base_uri . should == 'http://api.foobar.com'
2008-07-28 12:08:21 -04:00
end
2009-03-03 11:13:41 -05:00
it 'should not modify the parameter during assignment' do
uri = 'http://api.foobar.com'
@klass . base_uri ( uri )
uri . should == 'http://api.foobar.com'
end
2008-07-27 11:52:18 -04:00
end
2009-09-08 23:34:21 -04:00
2010-12-10 16:47:42 -05:00
describe " .disable_rails_query_string_format " do
it " sets the query string normalizer to HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER " do
@klass . disable_rails_query_string_format
@klass . default_options [ :query_string_normalizer ] . should == HTTParty :: Request :: NON_RAILS_QUERY_STRING_NORMALIZER
end
end
describe " .normalize_base_uri " do
2008-12-06 23:41:28 -05:00
it " should add http if not present for non ssl requests " do
uri = HTTParty . normalize_base_uri ( 'api.foobar.com' )
uri . should == 'http://api.foobar.com'
end
2009-09-08 23:34:21 -04:00
2008-12-06 23:41:28 -05:00
it " should add https if not present for ssl requests " do
uri = HTTParty . normalize_base_uri ( 'api.foo.com/v1:443' )
uri . should == 'https://api.foo.com/v1:443'
end
2009-09-08 23:34:21 -04:00
2008-12-06 23:41:28 -05:00
it " should not remove https for ssl requests " do
uri = HTTParty . normalize_base_uri ( 'https://api.foo.com/v1:443' )
uri . should == 'https://api.foo.com/v1:443'
end
2009-03-03 11:13:41 -05:00
it 'should not modify the parameter' do
uri = 'http://api.foobar.com'
HTTParty . normalize_base_uri ( uri )
uri . should == 'http://api.foobar.com'
end
2012-05-04 18:26:55 -04:00
it " should not treat uri's with a port of 4430 as ssl " do
uri = HTTParty . normalize_base_uri ( 'http://api.foo.com:4430/v1' )
uri . should == 'http://api.foo.com:4430/v1'
end
2008-12-06 23:41:28 -05:00
end
2009-09-08 23:34:21 -04:00
2008-07-28 12:08:21 -04:00
describe " headers " do
2009-09-08 19:02:09 -04:00
def expect_headers ( header = { } )
2009-08-21 20:32:15 -04:00
HTTParty :: Request . should_receive ( :new ) \
2009-09-08 19:02:09 -04:00
. with ( anything , anything , hash_including ( { :headers = > header } ) ) \
2009-08-21 20:32:15 -04:00
. and_return ( mock ( " mock response " , :perform = > nil ) )
end
2009-09-08 19:02:09 -04:00
2008-07-28 12:08:21 -04:00
it " should default to empty hash " do
2009-01-28 23:41:01 -05:00
@klass . headers . should == { }
2008-07-28 12:08:21 -04:00
end
2009-09-08 19:02:09 -04:00
2008-07-28 12:08:21 -04:00
it " should be able to be updated " do
init_headers = { :foo = > 'bar' , :baz = > 'spax' }
2009-01-28 23:41:01 -05:00
@klass . headers init_headers
@klass . headers . should == init_headers
2008-07-28 12:08:21 -04:00
end
2009-09-08 19:02:09 -04:00
it " uses the class headers when sending a request " do
expect_headers ( :foo = > 'bar' )
@klass . headers ( :foo = > 'bar' )
@klass . get ( '' )
end
it " overwrites class headers when passing in headers " do
expect_headers ( :baz = > 'spax' )
@klass . headers ( :foo = > 'bar' )
@klass . get ( '' , :headers = > { :baz = > 'spax' } )
end
context " with cookies " do
it 'utilizes the class-level cookies' do
expect_headers ( :foo = > 'bar' , 'cookie' = > 'type=snickerdoodle' )
@klass . headers ( :foo = > 'bar' )
@klass . cookies ( :type = > 'snickerdoodle' )
@klass . get ( '' )
2009-08-21 20:32:15 -04:00
end
2009-09-08 19:02:09 -04:00
it 'adds cookies to the headers' do
expect_headers ( :foo = > 'bar' , 'cookie' = > 'type=snickerdoodle' )
@klass . headers ( :foo = > 'bar' )
@klass . get ( '' , :cookies = > { :type = > 'snickerdoodle' } )
2009-08-21 20:32:15 -04:00
end
2009-09-08 19:02:09 -04:00
it 'adds optional cookies to the optional headers' do
expect_headers ( :baz = > 'spax' , 'cookie' = > 'type=snickerdoodle' )
@klass . get ( '' , :cookies = > { :type = > 'snickerdoodle' } , :headers = > { :baz = > 'spax' } )
2009-08-21 20:32:15 -04:00
end
end
2008-07-28 11:56:58 -04:00
end
2009-01-28 23:54:42 -05:00
describe " cookies " do
def expect_cookie_header ( s )
HTTParty :: Request . should_receive ( :new ) \
. with ( anything , anything , hash_including ( { :headers = > { " cookie " = > s } } ) ) \
. and_return ( mock ( " mock response " , :perform = > nil ) )
end
it " should not be in the headers by default " do
HTTParty :: Request . stub! ( :new ) . and_return ( stub ( nil , :perform = > nil ) )
@klass . get ( " " )
@klass . headers . keys . should_not include ( " cookie " )
end
it " should raise an ArgumentError if passed a non-Hash " do
lambda do
@klass . cookies ( " nonsense " )
end . should raise_error ( ArgumentError )
end
it " should allow a cookie to be specified with a one-off request " do
expect_cookie_header " type=snickerdoodle "
@klass . get ( " " , :cookies = > { :type = > " snickerdoodle " } )
end
describe " when a cookie is set at the class level " do
before ( :each ) do
@klass . cookies ( { :type = > " snickerdoodle " } )
end
it " should include that cookie in the request " do
expect_cookie_header " type=snickerdoodle "
@klass . get ( " " )
end
2009-06-23 20:28:42 -04:00
it " should pass the proper cookies when requested multiple times " do
2 . times do
expect_cookie_header " type=snickerdoodle "
@klass . get ( " " )
end
end
2009-01-28 23:54:42 -05:00
it " should allow the class defaults to be overridden " do
expect_cookie_header " type=chocolate_chip "
@klass . get ( " " , :cookies = > { :type = > " chocolate_chip " } )
end
end
describe " in a class with multiple methods that use different cookies " do
before ( :each ) do
@klass . instance_eval do
def first_method
get ( " first_method " , :cookies = > { :first_method_cookie = > " foo " } )
end
def second_method
get ( " second_method " , :cookies = > { :second_method_cookie = > " foo " } )
end
end
end
it " should not allow cookies used in one method to carry over into other methods " do
expect_cookie_header " first_method_cookie=foo "
@klass . first_method
expect_cookie_header " second_method_cookie=foo "
@klass . second_method
end
end
end
2009-09-08 23:34:21 -04:00
2008-07-28 12:40:40 -04:00
describe " default params " do
it " should default to empty hash " do
2009-01-28 23:41:01 -05:00
@klass . default_params . should == { }
2008-07-28 12:40:40 -04:00
end
2009-09-08 23:34:21 -04:00
2008-07-28 12:40:40 -04:00
it " should be able to be updated " do
new_defaults = { :foo = > 'bar' , :baz = > 'spax' }
2009-01-28 23:41:01 -05:00
@klass . default_params new_defaults
@klass . default_params . should == new_defaults
2008-07-28 12:40:40 -04:00
end
end
2009-09-08 23:34:21 -04:00
2010-04-12 04:45:27 -04:00
describe " default timeout " do
it " should default to nil " do
@klass . default_options [ :timeout ] . should == nil
end
it " should support updating " do
@klass . default_timeout 10
@klass . default_options [ :timeout ] . should == 10
end
2011-02-17 13:55:48 -05:00
it " should support floats " do
@klass . default_timeout 0 . 5
@klass . default_options [ :timeout ] . should == 0 . 5
end
2010-04-12 04:45:27 -04:00
end
2010-01-24 22:59:55 -05:00
describe " debug_output " do
it " stores the given stream as a default_option " do
@klass . debug_output $stdout
@klass . default_options [ :debug_output ] . should == $stdout
end
it " stores the $stderr stream by default " do
@klass . debug_output
@klass . default_options [ :debug_output ] . should == $stderr
end
end
2008-07-28 12:08:21 -04:00
describe " basic http authentication " do
it " should work " do
2009-01-28 23:41:01 -05:00
@klass . basic_auth 'foobar' , 'secret'
@klass . default_options [ :basic_auth ] . should == { :username = > 'foobar' , :password = > 'secret' }
2008-07-28 12:08:21 -04:00
end
2008-07-27 11:52:18 -04:00
end
2009-09-08 23:34:21 -04:00
2010-02-09 21:50:53 -05:00
describe " digest http authentication " do
it " should work " do
@klass . digest_auth 'foobar' , 'secret'
@klass . default_options [ :digest_auth ] . should == { :username = > 'foobar' , :password = > 'secret' }
end
end
2009-09-08 23:34:21 -04:00
describe " parser " do
2010-07-18 14:16:56 -04:00
class CustomParser
def self . parse ( body )
return { :sexy = > true }
end
end
2009-12-05 21:00:36 -05:00
let ( :parser ) do
Proc . new { | data , format | CustomParser . parse ( data ) }
2009-09-08 23:34:21 -04:00
end
it " should set parser options " do
2009-12-05 21:00:36 -05:00
@klass . parser parser
@klass . default_options [ :parser ] . should == parser
2009-09-08 23:34:21 -04:00
end
it " should be able parse response with custom parser " do
2009-12-05 21:00:36 -05:00
@klass . parser parser
2009-09-08 23:34:21 -04:00
FakeWeb . register_uri ( :get , 'http://twitter.com/statuses/public_timeline.xml' , :body = > 'tweets' )
custom_parsed_response = @klass . get ( 'http://twitter.com/statuses/public_timeline.xml' )
custom_parsed_response [ :sexy ] . should == true
end
2009-12-05 21:00:36 -05:00
it " raises UnsupportedFormat when the parser cannot handle the format " do
@klass . format :json
class MyParser < HTTParty :: Parser
SupportedFormats = { }
2010-05-04 23:09:28 -04:00
end unless defined? ( MyParser )
2009-12-05 21:00:36 -05:00
expect do
@klass . parser MyParser
end . to raise_error ( HTTParty :: UnsupportedFormat )
end
it 'does not validate format whe custom parser is a proc' do
expect do
@klass . format :json
@klass . parser lambda { | body , format | }
end . to_not raise_error ( HTTParty :: UnsupportedFormat )
end
2009-09-08 23:34:21 -04:00
end
2012-08-13 19:36:35 -04:00
describe " connection_adapter " do
2012-08-10 17:01:50 -04:00
let ( :uri ) { 'http://google.com/api.json' }
2012-08-13 19:36:35 -04:00
let ( :connection_adapter ) { mock ( 'CustomConnectionAdapter' ) }
2012-08-10 17:01:50 -04:00
2012-08-13 19:36:35 -04:00
it " should set the connection_adapter " do
@klass . connection_adapter connection_adapter
@klass . default_options [ :connection_adapter ] . should be connection_adapter
end
it " should set the connection_adapter_options when provided " do
options = { :foo = > :bar }
@klass . connection_adapter connection_adapter , options
@klass . default_options [ :connection_adapter_options ] . should be options
end
it " should not set the connection_adapter_options when not provided " do
@klass . connection_adapter connection_adapter
@klass . default_options [ :connection_adapter_options ] . should be_nil
2012-08-10 17:01:50 -04:00
end
2012-08-13 19:36:35 -04:00
it " should process a request with a connection from the adapter " do
connection_adapter_options = { :foo = > :bar }
connection_adapter . should_receive ( :call ) do | u , o |
o [ :connection_adapter_options ] . should == connection_adapter_options
HTTParty :: ConnectionAdapter . call ( u , o )
2012-08-10 17:01:50 -04:00
end . with ( URI . parse ( uri ) , kind_of ( Hash ) )
FakeWeb . register_uri ( :get , uri , :body = > 'stuff' )
2012-08-13 19:36:35 -04:00
@klass . connection_adapter connection_adapter , connection_adapter_options
2012-08-10 17:01:50 -04:00
@klass . get ( uri ) . should == 'stuff'
end
end
2008-07-28 11:56:58 -04:00
describe " format " do
it " should allow xml " do
2009-01-28 23:41:01 -05:00
@klass . format :xml
@klass . default_options [ :format ] . should == :xml
2008-07-28 11:56:58 -04:00
end
2009-09-08 23:34:21 -04:00
2014-02-08 12:32:43 -05:00
it " should allow csv " do
@klass . format :csv
@klass . default_options [ :format ] . should == :csv
end
2008-07-28 11:56:58 -04:00
it " should allow json " do
2009-01-28 23:41:01 -05:00
@klass . format :json
@klass . default_options [ :format ] . should == :json
2008-07-28 11:56:58 -04:00
end
2009-09-08 23:34:21 -04:00
2009-03-05 15:53:13 -05:00
it " should allow plain " do
@klass . format :plain
@klass . default_options [ :format ] . should == :plain
end
2009-09-08 23:34:21 -04:00
2008-07-28 11:56:58 -04:00
it 'should not allow funky format' do
lambda do
2009-01-28 23:41:01 -05:00
@klass . format :foobar
2008-07-28 11:56:58 -04:00
end . should raise_error ( HTTParty :: UnsupportedFormat )
end
2009-03-05 16:06:02 -05:00
it 'should only print each format once with an exception' do
lambda do
@klass . format :foobar
2014-02-08 12:32:43 -05:00
end . should raise_error ( HTTParty :: UnsupportedFormat , " ':foobar' Must be one of: csv, html, json, plain, xml " )
2009-03-05 16:06:02 -05:00
end
2009-12-05 21:00:36 -05:00
it 'sets the default parser' do
@klass . default_options [ :parser ] . should be_nil
@klass . format :json
@klass . default_options [ :parser ] . should == HTTParty :: Parser
end
it 'does not reset parser to the default parser' do
my_parser = lambda { }
@klass . parser my_parser
@klass . format :json
@klass . parser . should == my_parser
end
2008-07-28 11:56:58 -04:00
end
2008-08-27 16:38:19 -04:00
2010-01-29 16:57:35 -05:00
describe " # no_follow " do
it " sets no_follow to false by default " do
@klass . no_follow
@klass . default_options [ :no_follow ] . should be_false
end
it " sets the no_follow option to true " do
@klass . no_follow true
@klass . default_options [ :no_follow ] . should be_true
end
end
2010-02-09 22:02:15 -05:00
describe " # maintain_method_across_redirects " do
it " sets maintain_method_across_redirects to true by default " do
@klass . maintain_method_across_redirects
@klass . default_options [ :maintain_method_across_redirects ] . should be_true
end
it " sets the maintain_method_across_redirects option to false " do
@klass . maintain_method_across_redirects false
@klass . default_options [ :maintain_method_across_redirects ] . should be_false
end
2010-12-10 16:47:42 -05:00
end
2011-01-18 15:36:56 -05:00
describe " .follow_redirects " do
it " sets follow redirects to true by default " do
@klass . follow_redirects
@klass . default_options [ :follow_redirects ] . should be_true
end
it " sets the follow_redirects option to false " do
@klass . follow_redirects false
@klass . default_options [ :follow_redirects ] . should be_false
end
end
2010-12-10 16:47:42 -05:00
describe " .query_string_normalizer " do
it " sets the query_string_normalizer option " do
normalizer = proc { }
@klass . query_string_normalizer normalizer
@klass . default_options [ :query_string_normalizer ] . should == normalizer
end
2010-02-09 22:02:15 -05:00
end
2008-11-08 13:59:57 -05:00
describe " with explicit override of automatic redirect handling " do
2010-01-29 16:57:35 -05:00
before do
@request = HTTParty :: Request . new ( Net :: HTTP :: Get , 'http://api.foo.com/v1' , :format = > :xml , :no_follow = > true )
@redirect = stub_response 'first redirect' , 302
@redirect [ 'location' ] = 'http://foo.com/bar'
HTTParty :: Request . stub ( :new = > @request )
end
2008-08-27 16:38:19 -04:00
2008-11-08 13:59:57 -05:00
it " should fail with redirected GET " do
lambda do
2010-01-29 16:57:35 -05:00
@error = @klass . get ( '/foo' , :no_follow = > true )
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2008-08-27 16:38:19 -04:00
end
2008-09-19 19:31:06 -04:00
2008-11-08 13:59:57 -05:00
it " should fail with redirected POST " do
lambda do
2009-01-28 23:41:01 -05:00
@klass . post ( '/foo' , :no_follow = > true )
2010-01-29 16:57:35 -05:00
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2008-11-08 13:59:57 -05:00
end
2008-09-19 19:31:06 -04:00
2012-04-13 19:22:51 -04:00
it " should fail with redirected PATCH " do
lambda do
@klass . patch ( '/foo' , :no_follow = > true )
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
end
2008-11-08 13:59:57 -05:00
it " should fail with redirected DELETE " do
lambda do
2009-01-28 23:41:01 -05:00
@klass . delete ( '/foo' , :no_follow = > true )
2010-01-29 16:57:35 -05:00
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2008-11-08 13:59:57 -05:00
end
2008-09-19 19:31:06 -04:00
2013-01-17 00:01:17 -05:00
it " should fail with redirected MOVE " do
lambda do
@klass . move ( '/foo' , :no_follow = > true )
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
end
2013-02-07 09:34:32 -05:00
it " should fail with redirected COPY " do
lambda do
@klass . copy ( '/foo' , :no_follow = > true )
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
end
2008-11-08 13:59:57 -05:00
it " should fail with redirected PUT " do
lambda do
2009-01-28 23:41:01 -05:00
@klass . put ( '/foo' , :no_follow = > true )
2010-01-29 16:57:35 -05:00
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2008-09-19 19:31:06 -04:00
end
2009-11-22 23:18:29 -05:00
it " should fail with redirected HEAD " do
lambda do
@klass . head ( '/foo' , :no_follow = > true )
2010-01-29 16:57:35 -05:00
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2009-11-22 23:18:29 -05:00
end
it " should fail with redirected OPTIONS " do
lambda do
@klass . options ( '/foo' , :no_follow = > true )
2010-01-29 16:57:35 -05:00
end . should raise_error ( HTTParty :: RedirectionTooDeep ) { | e | e . response . body . should == 'first redirect' }
2009-11-22 23:18:29 -05:00
end
2008-07-28 13:32:35 -04:00
end
2009-09-08 23:34:21 -04:00
2008-11-30 23:58:06 -05:00
describe " with multiple class definitions " do
2009-01-28 23:41:01 -05:00
before ( :each ) do
@klass . instance_eval do
base_uri " http://first.com "
default_params :one = > 1
end
@additional_klass = Class . new
@additional_klass . instance_eval do
include HTTParty
base_uri " http://second.com "
default_params :two = > 2
end
end
2008-11-30 23:58:06 -05:00
it " should not run over each others options " do
2009-01-28 23:41:01 -05:00
@klass . default_options . should == { :base_uri = > 'http://first.com' , :default_params = > { :one = > 1 } }
@additional_klass . default_options . should == { :base_uri = > 'http://second.com' , :default_params = > { :two = > 2 } }
2008-11-30 23:58:06 -05:00
end
end
2009-09-08 23:34:21 -04:00
2009-11-24 17:22:45 -05:00
describe " two child classes inheriting from one parent " do
before ( :each ) do
@parent = Class . new do
include HTTParty
2010-07-07 08:55:51 -04:00
def self . name
" Parent "
end
2009-11-24 17:22:45 -05:00
end
@child1 = Class . new ( @parent )
@child2 = Class . new ( @parent )
end
it " does not modify each others inherited attributes " do
@child1 . default_params :joe = > " alive "
@child2 . default_params :joe = > " dead "
@child1 . default_options . should == { :default_params = > { :joe = > " alive " } }
@child2 . default_options . should == { :default_params = > { :joe = > " dead " } }
@parent . default_options . should == { }
end
2010-07-07 09:43:38 -04:00
it " inherits default_options from the superclass " do
@parent . basic_auth 'user' , 'password'
@child1 . default_options . should == { :basic_auth = > { :username = > 'user' , :password = > 'password' } }
@child1 . basic_auth 'u' , 'p' # modifying child1 has no effect on child2
@child2 . default_options . should == { :basic_auth = > { :username = > 'user' , :password = > 'password' } }
end
it " doesn't modify the parent's default options " do
@parent . basic_auth 'user' , 'password'
@child1 . basic_auth 'u' , 'p'
@child1 . default_options . should == { :basic_auth = > { :username = > 'u' , :password = > 'p' } }
@child1 . basic_auth 'email' , 'token'
@child1 . default_options . should == { :basic_auth = > { :username = > 'email' , :password = > 'token' } }
@parent . default_options . should == { :basic_auth = > { :username = > 'user' , :password = > 'password' } }
end
2012-04-21 18:45:03 -04:00
it " doesn't modify hashes in the parent's default options " do
@parent . headers 'Accept' = > 'application/json'
@child1 . headers 'Accept' = > 'application/xml'
@parent . default_options [ :headers ] . should == { 'Accept' = > 'application/json' }
@child1 . default_options [ :headers ] . should == { 'Accept' = > 'application/xml' }
end
2013-01-01 13:48:32 -05:00
it " works with lambda values " do
@child1 . default_options [ :imaginary_option ] = lambda { " This is a new lambda " }
@child1 . default_options [ :imaginary_option ] . should be_a Proc
end
it 'should dup the proc on the child class' do
2013-03-25 10:50:30 -04:00
imaginary_option = lambda { 2 * 3 . 14 }
2013-01-01 13:48:32 -05:00
@parent . default_options [ :imaginary_option ] = imaginary_option
2013-03-25 15:36:41 -04:00
@parent . default_options [ :imaginary_option ] . call . should == imaginary_option . call
2013-01-01 13:48:32 -05:00
@child1 . default_options [ :imaginary_option ]
2013-03-25 10:50:30 -04:00
@child1 . default_options [ :imaginary_option ] . call . should == imaginary_option . call
@child1 . default_options [ :imaginary_option ] . should_not be_equal imaginary_option
2013-01-01 13:48:32 -05:00
end
2010-07-07 09:43:38 -04:00
it " inherits default_cookies from the parent class " do
@parent . cookies 'type' = > 'chocolate_chip'
@child1 . default_cookies . should == { " type " = > " chocolate_chip " }
@child1 . cookies 'type' = > 'snickerdoodle'
@child1 . default_cookies . should == { " type " = > " snickerdoodle " }
@child2 . default_cookies . should == { " type " = > " chocolate_chip " }
end
it " doesn't modify the parent's default cookies " do
@parent . cookies 'type' = > 'chocolate_chip'
@child1 . cookies 'type' = > 'snickerdoodle'
@child1 . default_cookies . should == { " type " = > " snickerdoodle " }
@parent . default_cookies . should == { " type " = > " chocolate_chip " }
end
2009-11-24 17:22:45 -05:00
end
2010-07-07 11:26:20 -04:00
describe " grand parent with inherited callback " do
before do
@grand_parent = Class . new do
def self . inherited ( subclass )
subclass . instance_variable_set ( :@grand_parent , true )
end
end
@parent = Class . new ( @grand_parent ) do
include HTTParty
end
end
it " continues running the # inherited on the parent " do
child = Class . new ( @parent )
child . instance_variable_get ( :@grand_parent ) . should be_true
end
end
2008-12-06 19:41:23 -05:00
describe " # get " do
it " should be able to get html " do
stub_http_response_with ( 'google.html' )
HTTParty . get ( 'http://www.google.com' ) . should == file_fixture ( 'google.html' )
end
2009-09-08 23:34:21 -04:00
2012-02-15 14:46:01 -05:00
it " should be able to get chunked html " do
chunks = [ " Chunk1 " , " Chunk2 " , " Chunk3 " , " Chunk4 " ]
stub_chunked_http_response_with ( chunks )
HTTParty . get ( 'http://www.google.com' ) do | fragment |
chunks . should include ( fragment )
end . should == chunks . join
end
2008-12-06 19:41:23 -05:00
it " should be able parse response type json automatically " do
stub_http_response_with ( 'twitter.json' )
tweets = HTTParty . get ( 'http://twitter.com/statuses/public_timeline.json' )
tweets . size . should == 20
tweets . first [ 'user' ] . should == {
2009-09-08 23:34:21 -04:00
" name " = > " Pyk " ,
" url " = > nil ,
" id " = > " 7694602 " ,
" description " = > nil ,
" protected " = > false ,
" screen_name " = > " Pyk " ,
" followers_count " = > 1 ,
" location " = > " Opera Plaza, California " ,
2008-12-06 19:41:23 -05:00
" profile_image_url " = > " http://static.twitter.com/images/default_profile_normal.png "
}
end
2009-09-08 23:34:21 -04:00
2008-12-06 19:41:23 -05:00
it " should be able parse response type xml automatically " do
stub_http_response_with ( 'twitter.xml' )
tweets = HTTParty . get ( 'http://twitter.com/statuses/public_timeline.xml' )
tweets [ 'statuses' ] . size . should == 20
tweets [ 'statuses' ] . first [ 'user' ] . should == {
2009-09-08 23:34:21 -04:00
" name " = > " Magic 8 Bot " ,
" url " = > nil ,
" id " = > " 17656026 " ,
" description " = > " ask me a question " ,
" protected " = > " false " ,
" screen_name " = > " magic8bot " ,
" followers_count " = > " 90 " ,
" profile_image_url " = > " http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg " ,
2008-12-06 19:41:23 -05:00
" location " = > nil
}
end
2009-09-08 23:34:21 -04:00
2014-02-08 12:32:43 -05:00
it " should be able parse response type csv automatically " do
stub_http_response_with ( 'twitter.csv' )
profile = HTTParty . get ( 'http://twitter.com/statuses/profile.csv' )
profile . size . should == 2
profile [ 0 ] . should == [ " name " , " url " , " id " , " description " , " protected " , " screen_name " , " followers_count " , " profile_image_url " , " location " ]
profile [ 1 ] . should == [ " Magic 8 Bot " , nil , " 17656026 " , " ask me a question " , " false " , " magic8bot " , " 90 " , " http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg " , nil ]
end
2009-01-28 12:55:25 -05:00
it " should not get undefined method add_node for nil class for the following xml " do
stub_http_response_with ( 'undefined_method_add_node_for_nil.xml' )
result = HTTParty . get ( 'http://foobar.com' )
result . should == { " Entities " = > { " href " = > " https://s3-sandbox.parature.com/api/v1/5578/5633/Account " , " results " = > " 0 " , " total " = > " 0 " , " page_size " = > " 25 " , " page " = > " 1 " } }
end
2009-09-08 23:34:21 -04:00
2009-01-28 13:17:39 -05:00
it " should parse empty response fine " do
stub_http_response_with ( 'empty.xml' )
result = HTTParty . get ( 'http://foobar.com' )
2009-12-05 21:00:36 -05:00
result . should be_nil
2009-01-28 13:17:39 -05:00
end
2009-10-28 17:39:14 -04:00
it " should accept http URIs " do
stub_http_response_with ( 'google.html' )
lambda do
HTTParty . get ( 'http://google.com' )
2009-11-19 23:39:11 -05:00
end . should_not raise_error ( HTTParty :: UnsupportedURIScheme )
2009-10-28 17:39:14 -04:00
end
it " should accept https URIs " do
stub_http_response_with ( 'google.html' )
lambda do
HTTParty . get ( 'https://google.com' )
2009-11-19 23:39:11 -05:00
end . should_not raise_error ( HTTParty :: UnsupportedURIScheme )
2009-10-28 17:39:14 -04:00
end
2012-09-17 15:33:00 -04:00
it " should accept webcal URIs " do
stub_http_response_with ( 'google.html' )
2009-10-28 17:39:14 -04:00
lambda do
2012-09-17 15:33:00 -04:00
HTTParty . get ( 'webcal://google.com' )
end . should_not raise_error ( HTTParty :: UnsupportedURIScheme )
2009-10-28 17:39:14 -04:00
end
it " should raise an InvalidURIError on URIs that can't be parsed at all " do
lambda do
HTTParty . get ( " It's the one that says 'Bad URI' " )
end . should raise_error ( URI :: InvalidURIError )
end
2008-12-06 19:41:23 -05:00
end
2009-09-08 23:34:21 -04:00
end