httsoiree 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +14 -0
  5. data/Guardfile +16 -0
  6. data/History +303 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +77 -0
  9. data/Rakefile +12 -0
  10. data/bin/httparty +117 -0
  11. data/cucumber.yml +1 -0
  12. data/examples/aaws.rb +32 -0
  13. data/examples/basic.rb +28 -0
  14. data/examples/crack.rb +19 -0
  15. data/examples/custom_parsers.rb +67 -0
  16. data/examples/delicious.rb +37 -0
  17. data/examples/google.rb +16 -0
  18. data/examples/headers_and_user_agents.rb +6 -0
  19. data/examples/logging.rb +38 -0
  20. data/examples/nokogiri_html_parser.rb +22 -0
  21. data/examples/rubyurl.rb +14 -0
  22. data/examples/stackexchange.rb +24 -0
  23. data/examples/tripit_sign_in.rb +33 -0
  24. data/examples/twitter.rb +31 -0
  25. data/examples/whoismyrep.rb +10 -0
  26. data/features/basic_authentication.feature +20 -0
  27. data/features/command_line.feature +7 -0
  28. data/features/deals_with_http_error_codes.feature +26 -0
  29. data/features/digest_authentication.feature +20 -0
  30. data/features/handles_compressed_responses.feature +27 -0
  31. data/features/handles_multiple_formats.feature +57 -0
  32. data/features/steps/env.rb +22 -0
  33. data/features/steps/httparty_response_steps.rb +52 -0
  34. data/features/steps/httparty_steps.rb +43 -0
  35. data/features/steps/mongrel_helper.rb +94 -0
  36. data/features/steps/remote_service_steps.rb +74 -0
  37. data/features/supports_read_timeout_option.feature +13 -0
  38. data/features/supports_redirection.feature +22 -0
  39. data/features/supports_timeout_option.feature +13 -0
  40. data/httparty.gemspec +25 -0
  41. data/lib/httparty/connection_adapter.rb +188 -0
  42. data/lib/httparty/cookie_hash.rb +22 -0
  43. data/lib/httparty/core_extensions.rb +32 -0
  44. data/lib/httparty/exceptions.rb +29 -0
  45. data/lib/httparty/hash_conversions.rb +51 -0
  46. data/lib/httparty/logger/apache_logger.rb +22 -0
  47. data/lib/httparty/logger/curl_logger.rb +48 -0
  48. data/lib/httparty/logger/logger.rb +18 -0
  49. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  50. data/lib/httparty/net_digest_auth.rb +84 -0
  51. data/lib/httparty/parser.rb +141 -0
  52. data/lib/httparty/request.rb +339 -0
  53. data/lib/httparty/response/headers.rb +31 -0
  54. data/lib/httparty/response.rb +72 -0
  55. data/lib/httparty/version.rb +3 -0
  56. data/lib/httparty.rb +618 -0
  57. data/lib/httsoiree.rb +3 -0
  58. data/script/release +42 -0
  59. data/spec/fixtures/delicious.xml +23 -0
  60. data/spec/fixtures/empty.xml +0 -0
  61. data/spec/fixtures/google.html +3 -0
  62. data/spec/fixtures/ssl/generate.sh +29 -0
  63. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  64. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  65. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  66. data/spec/fixtures/ssl/generated/ca.key +15 -0
  67. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  68. data/spec/fixtures/ssl/generated/server.crt +13 -0
  69. data/spec/fixtures/ssl/generated/server.key +15 -0
  70. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  71. data/spec/fixtures/twitter.csv +2 -0
  72. data/spec/fixtures/twitter.json +1 -0
  73. data/spec/fixtures/twitter.xml +403 -0
  74. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  75. data/spec/httparty/connection_adapter_spec.rb +370 -0
  76. data/spec/httparty/cookie_hash_spec.rb +83 -0
  77. data/spec/httparty/exception_spec.rb +23 -0
  78. data/spec/httparty/logger/apache_logger_spec.rb +41 -0
  79. data/spec/httparty/logger/curl_logger_spec.rb +18 -0
  80. data/spec/httparty/logger/logger_spec.rb +22 -0
  81. data/spec/httparty/net_digest_auth_spec.rb +152 -0
  82. data/spec/httparty/parser_spec.rb +165 -0
  83. data/spec/httparty/request_spec.rb +774 -0
  84. data/spec/httparty/response_spec.rb +221 -0
  85. data/spec/httparty/ssl_spec.rb +74 -0
  86. data/spec/httparty_spec.rb +783 -0
  87. data/spec/spec.opts +2 -0
  88. data/spec/spec_helper.rb +37 -0
  89. data/spec/support/ssl_test_helper.rb +47 -0
  90. data/spec/support/ssl_test_server.rb +80 -0
  91. data/spec/support/stub_response.rb +43 -0
  92. data/website/css/common.css +47 -0
  93. data/website/index.html +73 -0
  94. metadata +215 -0
@@ -0,0 +1,221 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe HTTParty::Response do
4
+ before do
5
+ @last_modified = Date.new(2010, 1, 15).to_s
6
+ @content_length = '1024'
7
+ @request_object = HTTParty::Request.new Net::HTTP::Get, '/'
8
+ @response_object = Net::HTTPOK.new('1.1', 200, 'OK')
9
+ @response_object.stub(body: "{foo:'bar'}")
10
+ @response_object['last-modified'] = @last_modified
11
+ @response_object['content-length'] = @content_length
12
+ @parsed_response = lambda { {"foo" => "bar"} }
13
+ @response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
14
+ end
15
+
16
+ describe ".underscore" do
17
+ it "works with one capitalized word" do
18
+ HTTParty::Response.underscore("Accepted").should == "accepted"
19
+ end
20
+
21
+ it "works with titlecase" do
22
+ HTTParty::Response.underscore("BadGateway").should == "bad_gateway"
23
+ end
24
+
25
+ it "works with all caps" do
26
+ HTTParty::Response.underscore("OK").should == "ok"
27
+ end
28
+ end
29
+
30
+ describe "initialization" do
31
+ it "should set the Net::HTTP Response" do
32
+ @response.response.should == @response_object
33
+ end
34
+
35
+ it "should set body" do
36
+ @response.body.should == @response_object.body
37
+ end
38
+
39
+ it "should set code" do
40
+ @response.code.should.to_s == @response_object.code
41
+ end
42
+
43
+ it "should set code as a Fixnum" do
44
+ @response.code.should be_an_instance_of(Fixnum)
45
+ end
46
+ end
47
+
48
+ it "returns response headers" do
49
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
50
+ response.headers.should == {'last-modified' => [@last_modified], 'content-length' => [@content_length]}
51
+ end
52
+
53
+ it "should send missing methods to delegate" do
54
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
55
+ response['foo'].should == 'bar'
56
+ end
57
+
58
+ it "response to request" do
59
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
60
+ response.respond_to?(:request).should be_true
61
+ end
62
+
63
+ it "responds to response" do
64
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
65
+ response.respond_to?(:response).should be_true
66
+ end
67
+
68
+ it "responds to body" do
69
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
70
+ response.respond_to?(:body).should be_true
71
+ end
72
+
73
+ it "responds to headers" do
74
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
75
+ response.respond_to?(:headers).should be_true
76
+ end
77
+
78
+ it "responds to parsed_response" do
79
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
80
+ response.respond_to?(:parsed_response).should be_true
81
+ end
82
+
83
+ it "responds to anything parsed_response responds to" do
84
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
85
+ response.respond_to?(:[]).should be_true
86
+ end
87
+
88
+ it "should be able to iterate if it is array" do
89
+ response = HTTParty::Response.new(@request_object, @response_object, lambda { [{'foo' => 'bar'}, {'foo' => 'baz'}] })
90
+ response.size.should == 2
91
+ expect {
92
+ response.each { |item| }
93
+ }.to_not raise_error
94
+ end
95
+
96
+ it "allows headers to be accessed by mixed-case names in hash notation" do
97
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
98
+ response.headers['Content-LENGTH'].should == @content_length
99
+ end
100
+
101
+ it "returns a comma-delimited value when multiple values exist" do
102
+ @response_object.add_field 'set-cookie', 'csrf_id=12345; path=/'
103
+ @response_object.add_field 'set-cookie', '_github_ses=A123CdE; path=/'
104
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
105
+ response.headers['set-cookie'].should == "csrf_id=12345; path=/, _github_ses=A123CdE; path=/"
106
+ end
107
+
108
+ # Backwards-compatibility - previously, #headers returned a Hash
109
+ it "responds to hash methods" do
110
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
111
+ hash_methods = {}.methods - response.headers.methods
112
+ hash_methods.each do |method_name|
113
+ response.headers.respond_to?(method_name).should be_true
114
+ end
115
+ end
116
+
117
+ describe "semantic methods for response codes" do
118
+ def response_mock(klass)
119
+ response = klass.new('', '', '')
120
+ response.stub(:body)
121
+ response
122
+ end
123
+
124
+ context "major codes" do
125
+ it "is information" do
126
+ net_response = response_mock(Net::HTTPInformation)
127
+ response = HTTParty::Response.new(@request_object, net_response, '')
128
+ response.information?.should be_true
129
+ end
130
+
131
+ it "is success" do
132
+ net_response = response_mock(Net::HTTPSuccess)
133
+ response = HTTParty::Response.new(@request_object, net_response, '')
134
+ response.success?.should be_true
135
+ end
136
+
137
+ it "is redirection" do
138
+ net_response = response_mock(Net::HTTPRedirection)
139
+ response = HTTParty::Response.new(@request_object, net_response, '')
140
+ response.redirection?.should be_true
141
+ end
142
+
143
+ it "is client error" do
144
+ net_response = response_mock(Net::HTTPClientError)
145
+ response = HTTParty::Response.new(@request_object, net_response, '')
146
+ response.client_error?.should be_true
147
+ end
148
+
149
+ it "is server error" do
150
+ net_response = response_mock(Net::HTTPServerError)
151
+ response = HTTParty::Response.new(@request_object, net_response, '')
152
+ response.server_error?.should be_true
153
+ end
154
+ end
155
+
156
+ context "for specific codes" do
157
+ SPECIFIC_CODES = {
158
+ accepted?: Net::HTTPAccepted,
159
+ bad_gateway?: Net::HTTPBadGateway,
160
+ bad_request?: Net::HTTPBadRequest,
161
+ conflict?: Net::HTTPConflict,
162
+ continue?: Net::HTTPContinue,
163
+ created?: Net::HTTPCreated,
164
+ expectation_failed?: Net::HTTPExpectationFailed,
165
+ forbidden?: Net::HTTPForbidden,
166
+ found?: Net::HTTPFound,
167
+ gateway_time_out?: Net::HTTPGatewayTimeOut,
168
+ gone?: Net::HTTPGone,
169
+ internal_server_error?: Net::HTTPInternalServerError,
170
+ length_required?: Net::HTTPLengthRequired,
171
+ method_not_allowed?: Net::HTTPMethodNotAllowed,
172
+ moved_permanently?: Net::HTTPMovedPermanently,
173
+ multiple_choice?: Net::HTTPMultipleChoice,
174
+ no_content?: Net::HTTPNoContent,
175
+ non_authoritative_information?: Net::HTTPNonAuthoritativeInformation,
176
+ not_acceptable?: Net::HTTPNotAcceptable,
177
+ not_found?: Net::HTTPNotFound,
178
+ not_implemented?: Net::HTTPNotImplemented,
179
+ not_modified?: Net::HTTPNotModified,
180
+ ok?: Net::HTTPOK,
181
+ partial_content?: Net::HTTPPartialContent,
182
+ payment_required?: Net::HTTPPaymentRequired,
183
+ precondition_failed?: Net::HTTPPreconditionFailed,
184
+ proxy_authentication_required?: Net::HTTPProxyAuthenticationRequired,
185
+ request_entity_too_large?: Net::HTTPRequestEntityTooLarge,
186
+ request_time_out?: Net::HTTPRequestTimeOut,
187
+ request_uri_too_long?: Net::HTTPRequestURITooLong,
188
+ requested_range_not_satisfiable?: Net::HTTPRequestedRangeNotSatisfiable,
189
+ reset_content?: Net::HTTPResetContent,
190
+ see_other?: Net::HTTPSeeOther,
191
+ service_unavailable?: Net::HTTPServiceUnavailable,
192
+ switch_protocol?: Net::HTTPSwitchProtocol,
193
+ temporary_redirect?: Net::HTTPTemporaryRedirect,
194
+ unauthorized?: Net::HTTPUnauthorized,
195
+ unsupported_media_type?: Net::HTTPUnsupportedMediaType,
196
+ use_proxy?: Net::HTTPUseProxy,
197
+ version_not_supported?: Net::HTTPVersionNotSupported
198
+ }
199
+
200
+ # Ruby 2.0, new name for this response.
201
+ if RUBY_VERSION >= "2.0.0" && ::RUBY_PLATFORM != "java"
202
+ SPECIFIC_CODES[:multiple_choices?] = Net::HTTPMultipleChoices
203
+ end
204
+
205
+ SPECIFIC_CODES.each do |method, klass|
206
+ it "responds to #{method}" do
207
+ net_response = response_mock(klass)
208
+ response = HTTParty::Response.new(@request_object, net_response, '')
209
+ response.__send__(method).should be_true
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ describe "headers" do
216
+ it "can initialize without headers" do
217
+ headers = HTTParty::Response::Headers.new
218
+ headers.should == {}
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe HTTParty::Request do
4
+ context "SSL certificate verification" do
5
+ before do
6
+ FakeWeb.allow_net_connect = true
7
+ end
8
+
9
+ after do
10
+ FakeWeb.allow_net_connect = false
11
+ end
12
+
13
+ it "should fail when no trusted CA list is specified, by default" do
14
+ lambda do
15
+ ssl_verify_test(nil, nil, "selfsigned.crt")
16
+ end.should raise_error OpenSSL::SSL::SSLError
17
+ end
18
+
19
+ it "should work when no trusted CA list is specified, when the verify option is set to false" do
20
+ ssl_verify_test(nil, nil, "selfsigned.crt", verify: false).should == {'success' => true}
21
+ end
22
+
23
+ it "should fail when no trusted CA list is specified, with a bogus hostname, by default" do
24
+ lambda do
25
+ ssl_verify_test(nil, nil, "bogushost.crt")
26
+ end.should raise_error OpenSSL::SSL::SSLError
27
+ end
28
+
29
+ it "should work when no trusted CA list is specified, even with a bogus hostname, when the verify option is set to true" do
30
+ ssl_verify_test(nil, nil, "bogushost.crt", verify: false).should == {'success' => true}
31
+ end
32
+
33
+ it "should work when using ssl_ca_file with a self-signed CA" do
34
+ ssl_verify_test(:ssl_ca_file, "selfsigned.crt", "selfsigned.crt").should == {'success' => true}
35
+ end
36
+
37
+ it "should work when using ssl_ca_file with a certificate authority" do
38
+ ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").should == {'success' => true}
39
+ end
40
+
41
+ it "should work when using ssl_ca_path with a certificate authority" do
42
+ http = Net::HTTP.new('www.google.com', 443)
43
+ response = stub(Net::HTTPResponse, :[] => '', body: '', to_hash: {})
44
+ http.stub(:request).and_return(response)
45
+ Net::HTTP.should_receive(:new).with('www.google.com', 443).and_return(http)
46
+ http.should_receive(:ca_path=).with('/foo/bar')
47
+ HTTParty.get('https://www.google.com', ssl_ca_path: '/foo/bar')
48
+ end
49
+
50
+ it "should fail when using ssl_ca_file and the server uses an unrecognized certificate authority" do
51
+ lambda do
52
+ ssl_verify_test(:ssl_ca_file, "ca.crt", "selfsigned.crt")
53
+ end.should raise_error(OpenSSL::SSL::SSLError)
54
+ end
55
+
56
+ it "should fail when using ssl_ca_path and the server uses an unrecognized certificate authority" do
57
+ lambda do
58
+ ssl_verify_test(:ssl_ca_path, ".", "selfsigned.crt")
59
+ end.should raise_error(OpenSSL::SSL::SSLError)
60
+ end
61
+
62
+ it "should fail when using ssl_ca_file and the server uses a bogus hostname" do
63
+ lambda do
64
+ ssl_verify_test(:ssl_ca_file, "ca.crt", "bogushost.crt")
65
+ end.should raise_error(OpenSSL::SSL::SSLError)
66
+ end
67
+
68
+ it "should fail when using ssl_ca_path and the server uses a bogus hostname" do
69
+ lambda do
70
+ ssl_verify_test(:ssl_ca_path, ".", "bogushost.crt")
71
+ end.should raise_error(OpenSSL::SSL::SSLError)
72
+ end
73
+ end
74
+ end