benschwarz-smoke 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (245) hide show
  1. data/Rakefile +47 -0
  2. data/VERSION.yml +2 -2
  3. data/lib/core_ext/string.rb +6 -0
  4. data/lib/smoke/cache.rb +38 -23
  5. data/lib/smoke/request.rb +4 -8
  6. data/lib/smoke.rb +7 -2
  7. data/rdoc/classes/Smoke/Origin.html +340 -0
  8. data/rdoc/classes/Smoke/Source/Data.html +126 -0
  9. data/rdoc/classes/Smoke/Source/Feed.html +117 -0
  10. data/rdoc/classes/Smoke/Source/YQL.html +223 -0
  11. data/rdoc/classes/Smoke.html +260 -0
  12. data/rdoc/created.rid +1 -0
  13. data/rdoc/files/README_markdown.html +180 -0
  14. data/rdoc/files/lib/core_ext/hash_rb.html +49 -0
  15. data/rdoc/files/lib/smoke/origin_rb.html +49 -0
  16. data/rdoc/files/lib/smoke/request_rb.html +49 -0
  17. data/rdoc/files/lib/smoke/source/data_rb.html +49 -0
  18. data/rdoc/files/lib/smoke/source/feed_rb.html +49 -0
  19. data/rdoc/files/lib/smoke/source/join_rb.html +49 -0
  20. data/rdoc/files/lib/smoke/source/yql_rb.html +49 -0
  21. data/rdoc/files/lib/smoke_rb.html +65 -0
  22. data/rdoc/fr_class_index.html +21 -0
  23. data/rdoc/fr_file_index.html +28 -0
  24. data/rdoc/fr_method_index.html +4459 -0
  25. data/rdoc/index.html +15 -0
  26. data/rdoc/rdoc-style.css +319 -0
  27. data/spec/smoke/cache_spec.rb +28 -6
  28. data/vendor/crack/History +15 -0
  29. data/vendor/crack/LICENSE +20 -0
  30. data/vendor/crack/README.rdoc +42 -0
  31. data/vendor/crack/Rakefile +49 -0
  32. data/vendor/crack/VERSION.yml +4 -0
  33. data/vendor/crack/crack.gemspec +61 -0
  34. data/vendor/crack/lib/crack/core_extensions.rb +128 -0
  35. data/vendor/crack/lib/crack/json.rb +68 -0
  36. data/vendor/crack/lib/crack/xml.rb +214 -0
  37. data/vendor/crack/lib/crack.rb +7 -0
  38. data/vendor/crack/test/crack_test.rb +4 -0
  39. data/vendor/crack/test/data/twittersearch-firefox.json +1 -0
  40. data/vendor/crack/test/data/twittersearch-ie.json +1 -0
  41. data/vendor/crack/test/hash_test.rb +56 -0
  42. data/vendor/crack/test/json_test.rb +66 -0
  43. data/vendor/crack/test/string_test.rb +31 -0
  44. data/vendor/crack/test/test_helper.rb +12 -0
  45. data/vendor/crack/test/xml_test.rb +489 -0
  46. data/vendor/dependencies/README.markdown +113 -0
  47. data/vendor/dependencies/Rakefile +5 -0
  48. data/vendor/dependencies/bin/dep +83 -0
  49. data/vendor/dependencies/dependencies.gemspec +15 -0
  50. data/vendor/dependencies/dependencies.gemspec.erb +27 -0
  51. data/vendor/dependencies/lib/dependencies/dep.rb +99 -0
  52. data/vendor/dependencies/lib/dependencies.rb +5 -0
  53. data/vendor/dependencies/test/dependencies_test.rb +228 -0
  54. data/vendor/dependencies/test/foobaz-0.3.gem +0 -0
  55. data/vendor/fakeweb/CHANGELOG +163 -0
  56. data/vendor/fakeweb/LICENSE.txt +281 -0
  57. data/vendor/fakeweb/README.rdoc +193 -0
  58. data/vendor/fakeweb/Rakefile +76 -0
  59. data/vendor/fakeweb/fakeweb.gemspec +21 -0
  60. data/vendor/fakeweb/lib/fake_web/ext/net_http.rb +71 -0
  61. data/vendor/fakeweb/lib/fake_web/registry.rb +103 -0
  62. data/vendor/fakeweb/lib/fake_web/responder.rb +113 -0
  63. data/vendor/fakeweb/lib/fake_web/response.rb +10 -0
  64. data/vendor/fakeweb/lib/fake_web/stub_socket.rb +15 -0
  65. data/vendor/fakeweb/lib/fake_web/utility.rb +22 -0
  66. data/vendor/fakeweb/lib/fake_web.rb +172 -0
  67. data/vendor/fakeweb/lib/fakeweb.rb +2 -0
  68. data/vendor/fakeweb/test/fixtures/google_response_from_curl +12 -0
  69. data/vendor/fakeweb/test/fixtures/google_response_with_transfer_encoding +17 -0
  70. data/vendor/fakeweb/test/fixtures/google_response_without_transfer_encoding +11 -0
  71. data/vendor/fakeweb/test/fixtures/test_example.txt +1 -0
  72. data/vendor/fakeweb/test/fixtures/test_txt_file +3 -0
  73. data/vendor/fakeweb/test/test_allow_net_connect.rb +85 -0
  74. data/vendor/fakeweb/test/test_deprecations.rb +54 -0
  75. data/vendor/fakeweb/test/test_fake_authentication.rb +92 -0
  76. data/vendor/fakeweb/test/test_fake_web.rb +535 -0
  77. data/vendor/fakeweb/test/test_fake_web_open_uri.rb +58 -0
  78. data/vendor/fakeweb/test/test_helper.rb +74 -0
  79. data/vendor/fakeweb/test/test_missing_open_uri.rb +25 -0
  80. data/vendor/fakeweb/test/test_precedence.rb +51 -0
  81. data/vendor/fakeweb/test/test_query_string.rb +45 -0
  82. data/vendor/fakeweb/test/test_regexes.rb +103 -0
  83. data/vendor/fakeweb/test/test_response_headers.rb +73 -0
  84. data/vendor/fakeweb/test/test_trailing_slashes.rb +53 -0
  85. data/vendor/fakeweb/test/test_utility.rb +70 -0
  86. data/vendor/json-1.1.3/CHANGES +93 -0
  87. data/vendor/json-1.1.3/GPL +340 -0
  88. data/vendor/json-1.1.3/README +78 -0
  89. data/vendor/json-1.1.3/RUBY +58 -0
  90. data/vendor/json-1.1.3/Rakefile +309 -0
  91. data/vendor/json-1.1.3/TODO +1 -0
  92. data/vendor/json-1.1.3/VERSION +1 -0
  93. data/vendor/json-1.1.3/benchmarks/benchmark.txt +133 -0
  94. data/vendor/json-1.1.3/benchmarks/benchmark_generator.rb +48 -0
  95. data/vendor/json-1.1.3/benchmarks/benchmark_parser.rb +26 -0
  96. data/vendor/json-1.1.3/benchmarks/benchmark_rails.rb +26 -0
  97. data/vendor/json-1.1.3/bin/edit_json.rb +10 -0
  98. data/vendor/json-1.1.3/bin/prettify_json.rb +76 -0
  99. data/vendor/json-1.1.3/data/example.json +1 -0
  100. data/vendor/json-1.1.3/data/index.html +38 -0
  101. data/vendor/json-1.1.3/data/prototype.js +4184 -0
  102. data/vendor/json-1.1.3/ext/json/ext/generator/extconf.rb +9 -0
  103. data/vendor/json-1.1.3/ext/json/ext/generator/generator.c +875 -0
  104. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.c +182 -0
  105. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.h +53 -0
  106. data/vendor/json-1.1.3/ext/json/ext/parser/extconf.rb +9 -0
  107. data/vendor/json-1.1.3/ext/json/ext/parser/parser.c +1758 -0
  108. data/vendor/json-1.1.3/ext/json/ext/parser/parser.rl +638 -0
  109. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.c +154 -0
  110. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.h +58 -0
  111. data/vendor/json-1.1.3/install.rb +26 -0
  112. data/vendor/json-1.1.3/lib/json/Array.xpm +21 -0
  113. data/vendor/json-1.1.3/lib/json/FalseClass.xpm +21 -0
  114. data/vendor/json-1.1.3/lib/json/Hash.xpm +21 -0
  115. data/vendor/json-1.1.3/lib/json/Key.xpm +73 -0
  116. data/vendor/json-1.1.3/lib/json/NilClass.xpm +21 -0
  117. data/vendor/json-1.1.3/lib/json/Numeric.xpm +28 -0
  118. data/vendor/json-1.1.3/lib/json/String.xpm +96 -0
  119. data/vendor/json-1.1.3/lib/json/TrueClass.xpm +21 -0
  120. data/vendor/json-1.1.3/lib/json/add/core.rb +135 -0
  121. data/vendor/json-1.1.3/lib/json/add/rails.rb +58 -0
  122. data/vendor/json-1.1.3/lib/json/common.rb +354 -0
  123. data/vendor/json-1.1.3/lib/json/editor.rb +1362 -0
  124. data/vendor/json-1.1.3/lib/json/ext.rb +13 -0
  125. data/vendor/json-1.1.3/lib/json/json.xpm +1499 -0
  126. data/vendor/json-1.1.3/lib/json/pure/generator.rb +394 -0
  127. data/vendor/json-1.1.3/lib/json/pure/parser.rb +259 -0
  128. data/vendor/json-1.1.3/lib/json/pure.rb +75 -0
  129. data/vendor/json-1.1.3/lib/json/version.rb +9 -0
  130. data/vendor/json-1.1.3/lib/json.rb +235 -0
  131. data/vendor/json-1.1.3/tests/fixtures/fail1.json +1 -0
  132. data/vendor/json-1.1.3/tests/fixtures/fail10.json +1 -0
  133. data/vendor/json-1.1.3/tests/fixtures/fail11.json +1 -0
  134. data/vendor/json-1.1.3/tests/fixtures/fail12.json +1 -0
  135. data/vendor/json-1.1.3/tests/fixtures/fail13.json +1 -0
  136. data/vendor/json-1.1.3/tests/fixtures/fail14.json +1 -0
  137. data/vendor/json-1.1.3/tests/fixtures/fail18.json +1 -0
  138. data/vendor/json-1.1.3/tests/fixtures/fail19.json +1 -0
  139. data/vendor/json-1.1.3/tests/fixtures/fail2.json +1 -0
  140. data/vendor/json-1.1.3/tests/fixtures/fail20.json +1 -0
  141. data/vendor/json-1.1.3/tests/fixtures/fail21.json +1 -0
  142. data/vendor/json-1.1.3/tests/fixtures/fail22.json +1 -0
  143. data/vendor/json-1.1.3/tests/fixtures/fail23.json +1 -0
  144. data/vendor/json-1.1.3/tests/fixtures/fail24.json +1 -0
  145. data/vendor/json-1.1.3/tests/fixtures/fail25.json +1 -0
  146. data/vendor/json-1.1.3/tests/fixtures/fail27.json +2 -0
  147. data/vendor/json-1.1.3/tests/fixtures/fail28.json +2 -0
  148. data/vendor/json-1.1.3/tests/fixtures/fail3.json +1 -0
  149. data/vendor/json-1.1.3/tests/fixtures/fail4.json +1 -0
  150. data/vendor/json-1.1.3/tests/fixtures/fail5.json +1 -0
  151. data/vendor/json-1.1.3/tests/fixtures/fail6.json +1 -0
  152. data/vendor/json-1.1.3/tests/fixtures/fail7.json +1 -0
  153. data/vendor/json-1.1.3/tests/fixtures/fail8.json +1 -0
  154. data/vendor/json-1.1.3/tests/fixtures/fail9.json +1 -0
  155. data/vendor/json-1.1.3/tests/fixtures/pass1.json +56 -0
  156. data/vendor/json-1.1.3/tests/fixtures/pass15.json +1 -0
  157. data/vendor/json-1.1.3/tests/fixtures/pass16.json +1 -0
  158. data/vendor/json-1.1.3/tests/fixtures/pass17.json +1 -0
  159. data/vendor/json-1.1.3/tests/fixtures/pass2.json +1 -0
  160. data/vendor/json-1.1.3/tests/fixtures/pass26.json +1 -0
  161. data/vendor/json-1.1.3/tests/fixtures/pass3.json +6 -0
  162. data/vendor/json-1.1.3/tests/runner.rb +25 -0
  163. data/vendor/json-1.1.3/tests/test_json.rb +293 -0
  164. data/vendor/json-1.1.3/tests/test_json_addition.rb +161 -0
  165. data/vendor/json-1.1.3/tests/test_json_fixtures.rb +30 -0
  166. data/vendor/json-1.1.3/tests/test_json_generate.rb +100 -0
  167. data/vendor/json-1.1.3/tests/test_json_rails.rb +118 -0
  168. data/vendor/json-1.1.3/tests/test_json_unicode.rb +61 -0
  169. data/vendor/json-1.1.3/tools/fuzz.rb +140 -0
  170. data/vendor/json-1.1.3/tools/server.rb +62 -0
  171. data/vendor/moneta/LICENSE +20 -0
  172. data/vendor/moneta/README +51 -0
  173. data/vendor/moneta/Rakefile +60 -0
  174. data/vendor/moneta/TODO +4 -0
  175. data/vendor/moneta/benchmarks/various.rb +234 -0
  176. data/vendor/moneta/lib/moneta/basic_file.rb +111 -0
  177. data/vendor/moneta/lib/moneta/berkeley.rb +53 -0
  178. data/vendor/moneta/lib/moneta/couch.rb +63 -0
  179. data/vendor/moneta/lib/moneta/datamapper.rb +117 -0
  180. data/vendor/moneta/lib/moneta/file.rb +91 -0
  181. data/vendor/moneta/lib/moneta/lmc.rb +52 -0
  182. data/vendor/moneta/lib/moneta/memcache.rb +53 -0
  183. data/vendor/moneta/lib/moneta/memory.rb +11 -0
  184. data/vendor/moneta/lib/moneta/mongodb.rb +58 -0
  185. data/vendor/moneta/lib/moneta/redis.rb +49 -0
  186. data/vendor/moneta/lib/moneta/rufus.rb +41 -0
  187. data/vendor/moneta/lib/moneta/s3.rb +162 -0
  188. data/vendor/moneta/lib/moneta/sdbm.rb +33 -0
  189. data/vendor/moneta/lib/moneta/tyrant.rb +58 -0
  190. data/vendor/moneta/lib/moneta/xattr.rb +58 -0
  191. data/vendor/moneta/lib/moneta.rb +76 -0
  192. data/vendor/moneta/moneta.gemspec +32 -0
  193. data/vendor/moneta/script/destroy +14 -0
  194. data/vendor/moneta/script/generate +14 -0
  195. data/vendor/moneta/spec/moneta_basic_file_spec.rb +50 -0
  196. data/vendor/moneta/spec/moneta_berkeley_spec.rb +20 -0
  197. data/vendor/moneta/spec/moneta_couch_spec.rb +22 -0
  198. data/vendor/moneta/spec/moneta_datamapper_spec.rb +79 -0
  199. data/vendor/moneta/spec/moneta_file_spec.rb +21 -0
  200. data/vendor/moneta/spec/moneta_lmc_spec.rb +24 -0
  201. data/vendor/moneta/spec/moneta_memcache_spec.rb +16 -0
  202. data/vendor/moneta/spec/moneta_memory_spec.rb +12 -0
  203. data/vendor/moneta/spec/moneta_mongodb_spec.rb +16 -0
  204. data/vendor/moneta/spec/moneta_redis_spec.rb +16 -0
  205. data/vendor/moneta/spec/moneta_rufus_spec.rb +15 -0
  206. data/vendor/moneta/spec/moneta_s3_spec.rb +19 -0
  207. data/vendor/moneta/spec/moneta_sdbm_spec.rb +21 -0
  208. data/vendor/moneta/spec/moneta_tyrant_spec.rb +15 -0
  209. data/vendor/moneta/spec/moneta_xattr_spec.rb +21 -0
  210. data/vendor/moneta/spec/shared.rb +122 -0
  211. data/vendor/moneta/spec/spec_helper.rb +7 -0
  212. data/vendor/rest-client/README.rdoc +151 -0
  213. data/vendor/rest-client/Rakefile +58 -0
  214. data/vendor/rest-client/VERSION +1 -0
  215. data/vendor/rest-client/bin/restclient +87 -0
  216. data/vendor/rest-client/lib/rest_client.rb +2 -0
  217. data/vendor/rest-client/lib/restclient/exceptions.rb +88 -0
  218. data/vendor/rest-client/lib/restclient/mixin/response.rb +43 -0
  219. data/vendor/rest-client/lib/restclient/raw_response.rb +30 -0
  220. data/vendor/rest-client/lib/restclient/request.rb +238 -0
  221. data/vendor/rest-client/lib/restclient/resource.rb +146 -0
  222. data/vendor/rest-client/lib/restclient/response.rb +20 -0
  223. data/vendor/rest-client/lib/restclient.rb +99 -0
  224. data/vendor/rest-client/rest-client.gemspec +66 -0
  225. data/vendor/rest-client/spec/base.rb +4 -0
  226. data/vendor/rest-client/spec/exceptions_spec.rb +65 -0
  227. data/vendor/rest-client/spec/mixin/response_spec.rb +46 -0
  228. data/vendor/rest-client/spec/raw_response_spec.rb +17 -0
  229. data/vendor/rest-client/spec/request_spec.rb +476 -0
  230. data/vendor/rest-client/spec/resource_spec.rb +75 -0
  231. data/vendor/rest-client/spec/response_spec.rb +16 -0
  232. data/vendor/rest-client/spec/restclient_spec.rb +53 -0
  233. data/vendor/simple-rss/LICENSE +429 -0
  234. data/vendor/simple-rss/README +43 -0
  235. data/vendor/simple-rss/Rakefile +212 -0
  236. data/vendor/simple-rss/install.rb +40 -0
  237. data/vendor/simple-rss/lib/simple-rss.rb +150 -0
  238. data/vendor/simple-rss/simple-rss.gemspec +12 -0
  239. data/vendor/simple-rss/test/base/base_test.rb +51 -0
  240. data/vendor/simple-rss/test/data/atom.xml +45 -0
  241. data/vendor/simple-rss/test/data/not-rss.xml +8 -0
  242. data/vendor/simple-rss/test/data/rss09.rdf +79 -0
  243. data/vendor/simple-rss/test/data/rss20.xml +818 -0
  244. data/vendor/simple-rss/test/test_helper.rb +4 -0
  245. metadata +314 -40
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe RestClient::Exception do
4
+ it "sets the exception message to ErrorMessage" do
5
+ RestClient::ResourceNotFound.new.message.should == 'Resource not found'
6
+ end
7
+
8
+ it "contains exceptions in RestClient" do
9
+ RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
10
+ RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
11
+ end
12
+ end
13
+
14
+ describe RestClient::RequestFailed do
15
+ before do
16
+ @response = mock('HTTP Response', :code => '502')
17
+ end
18
+
19
+ it "stores the http response on the exception" do
20
+ begin
21
+ raise RestClient::RequestFailed, :response
22
+ rescue RestClient::RequestFailed => e
23
+ e.response.should == :response
24
+ end
25
+ end
26
+
27
+ it "http_code convenience method for fetching the code as an integer" do
28
+ RestClient::RequestFailed.new(@response).http_code.should == 502
29
+ end
30
+
31
+ it "http_body convenience method for fetching the body (decoding when necessary)" do
32
+ @response.stub!(:[]).with('content-encoding').and_return('gzip')
33
+ @response.stub!(:body).and_return('compressed body')
34
+ RestClient::Request.should_receive(:decode).with('gzip', 'compressed body').and_return('plain body')
35
+ RestClient::RequestFailed.new(@response).http_body.should == 'plain body'
36
+ end
37
+
38
+ it "shows the status code in the message" do
39
+ RestClient::RequestFailed.new(@response).to_s.should match(/502/)
40
+ end
41
+ end
42
+
43
+ describe RestClient::ResourceNotFound do
44
+ it "also has the http response attached" do
45
+ begin
46
+ raise RestClient::ResourceNotFound, :response
47
+ rescue RestClient::ResourceNotFound => e
48
+ e.response.should == :response
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "backwards compatibility" do
54
+ it "alias RestClient::Request::Redirect to RestClient::Redirect" do
55
+ RestClient::Request::Redirect.should == RestClient::Redirect
56
+ end
57
+
58
+ it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
59
+ RestClient::Request::Unauthorized.should == RestClient::Unauthorized
60
+ end
61
+
62
+ it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
63
+ RestClient::Request::RequestFailed.should == RestClient::RequestFailed
64
+ end
65
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/../base'
2
+
3
+ class MockResponse
4
+ include RestClient::Mixin::Response
5
+
6
+ def initialize(body, res)
7
+ @net_http_res = res
8
+ @body = @body
9
+ end
10
+ end
11
+
12
+ describe RestClient::Mixin::Response do
13
+ before do
14
+ @net_http_res = mock('net http response')
15
+ @response = MockResponse.new('abc', @net_http_res)
16
+ end
17
+
18
+ it "fetches the numeric response code" do
19
+ @net_http_res.should_receive(:code).and_return('200')
20
+ @response.code.should == 200
21
+ end
22
+
23
+ it "beautifies the headers by turning the keys to symbols" do
24
+ h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
25
+ h.keys.first.should == :content_type
26
+ end
27
+
28
+ it "beautifies the headers by turning the values to strings instead of one-element arrays" do
29
+ h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
30
+ h.values.first.should == 'text/html'
31
+ end
32
+
33
+ it "fetches the headers" do
34
+ @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
35
+ @response.headers.should == { :content_type => 'text/html' }
36
+ end
37
+
38
+ it "extracts cookies from response headers" do
39
+ @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
40
+ @response.cookies.should == { 'session_id' => '1' }
41
+ end
42
+
43
+ it "can access the net http result directly" do
44
+ @response.net_http_res.should == @net_http_res
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe RestClient::RawResponse do
4
+ before do
5
+ @tf = mock("Tempfile", :read => "the answer is 42", :open => true)
6
+ @net_http_res = mock('net http response')
7
+ @response = RestClient::RawResponse.new(@tf, @net_http_res)
8
+ end
9
+
10
+ it "behaves like string" do
11
+ @response.to_s.should == 'the answer is 42'
12
+ end
13
+
14
+ it "exposes a Tempfile" do
15
+ @response.file.should == @tf
16
+ end
17
+ end
@@ -0,0 +1,476 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe RestClient::Request do
4
+ before do
5
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
6
+
7
+ @uri = mock("uri")
8
+ @uri.stub!(:request_uri).and_return('/resource')
9
+ @uri.stub!(:host).and_return('some')
10
+ @uri.stub!(:port).and_return(80)
11
+
12
+ @net = mock("net::http base")
13
+ @http = mock("net::http connection")
14
+ Net::HTTP.stub!(:new).and_return(@net)
15
+ @net.stub!(:start).and_yield(@http)
16
+ @net.stub!(:use_ssl=)
17
+ @net.stub!(:verify_mode=)
18
+ end
19
+
20
+ it "accept */* mimetype, preferring xml" do
21
+ @request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
22
+ end
23
+
24
+ it "decodes an uncompressed result body by passing it straight through" do
25
+ RestClient::Request.decode(nil, 'xyz').should == 'xyz'
26
+ end
27
+
28
+ it "decodes a gzip body" do
29
+ RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
30
+ end
31
+
32
+ it "ingores gzip for empty bodies" do
33
+ RestClient::Request.decode('gzip', '').should be_empty
34
+ end
35
+
36
+ it "decodes a deflated body" do
37
+ RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
38
+ end
39
+
40
+ it "processes a successful result" do
41
+ res = mock("result")
42
+ res.stub!(:code).and_return("200")
43
+ res.stub!(:body).and_return('body')
44
+ res.stub!(:[]).with('content-encoding').and_return(nil)
45
+ @request.process_result(res).should == 'body'
46
+ end
47
+
48
+ it "doesn't classify successful requests as failed" do
49
+ 203.upto(206) do |code|
50
+ res = mock("result")
51
+ res.stub!(:code).and_return(code.to_s)
52
+ res.stub!(:body).and_return("")
53
+ res.stub!(:[]).with('content-encoding').and_return(nil)
54
+ @request.process_result(res).should be_empty
55
+ end
56
+ end
57
+
58
+ it "parses a url into a URI object" do
59
+ URI.should_receive(:parse).with('http://example.com/resource')
60
+ @request.parse_url('http://example.com/resource')
61
+ end
62
+
63
+ it "adds http:// to the front of resources specified in the syntax example.com/resource" do
64
+ URI.should_receive(:parse).with('http://example.com/resource')
65
+ @request.parse_url('example.com/resource')
66
+ end
67
+
68
+ it "extracts the username and password when parsing http://user:password@example.com/" do
69
+ URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
70
+ @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
71
+ @request.user.should == 'joe'
72
+ @request.password.should == 'pass1'
73
+ end
74
+
75
+ it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
76
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
77
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
78
+ @request.parse_url_with_auth('http://example.com/resource')
79
+ @request.user.should == 'beth'
80
+ @request.password.should == 'pass2'
81
+ end
82
+
83
+ it "correctly formats cookies provided to the constructor" do
84
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
85
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
86
+ @request.should_receive(:default_headers).and_return({'foo' => 'bar'})
87
+ headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
88
+ end
89
+
90
+ it "determines the Net::HTTP class to instantiate by the method name" do
91
+ @request.net_http_request_class(:put).should == Net::HTTP::Put
92
+ end
93
+
94
+ it "merges user headers with the default headers" do
95
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
96
+ @request.make_headers('3' => '4').should == { '1' => '2', '3' => '4' }
97
+ end
98
+
99
+ it "prefers the user header when the same header exists in the defaults" do
100
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
101
+ @request.make_headers('1' => '3').should == { '1' => '3' }
102
+ end
103
+
104
+ it "converts header symbols from :content_type to 'Content-type'" do
105
+ @request.should_receive(:default_headers).and_return({})
106
+ @request.make_headers(:content_type => 'abc').should == { 'Content-type' => 'abc' }
107
+ end
108
+
109
+ it "converts header values to strings" do
110
+ @request.make_headers('A' => 1)['A'].should == '1'
111
+ end
112
+
113
+ it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
114
+ @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
115
+ klass = mock("net:http class")
116
+ @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
117
+ klass.should_receive(:new).and_return('result')
118
+ @request.should_receive(:transmit).with(@uri, 'result', 'payload')
119
+ @request.execute_inner
120
+ end
121
+
122
+ it "transmits the request with Net::HTTP" do
123
+ @http.should_receive(:request).with('req', 'payload')
124
+ @request.should_receive(:process_result)
125
+ @request.should_receive(:response_log)
126
+ @request.transmit(@uri, 'req', 'payload')
127
+ end
128
+
129
+ it "uses SSL when the URI refers to a https address" do
130
+ @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
131
+ @net.should_receive(:use_ssl=).with(true)
132
+ @http.stub!(:request)
133
+ @request.stub!(:process_result)
134
+ @request.stub!(:response_log)
135
+ @request.transmit(@uri, 'req', 'payload')
136
+ end
137
+
138
+ it "sends nil payloads" do
139
+ @http.should_receive(:request).with('req', nil)
140
+ @request.should_receive(:process_result)
141
+ @request.stub!(:response_log)
142
+ @request.transmit(@uri, 'req', nil)
143
+ end
144
+
145
+ it "passes non-hash payloads straight through" do
146
+ @request.process_payload("x").should == "x"
147
+ end
148
+
149
+ it "converts a hash payload to urlencoded data" do
150
+ @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
151
+ end
152
+
153
+ it "accepts nested hashes in payload" do
154
+ payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
155
+ payload.should include('user[name]=joe')
156
+ payload.should include('user[location][country]=USA')
157
+ payload.should include('user[location][state]=CA')
158
+ end
159
+
160
+ it "set urlencoded content_type header on hash payloads" do
161
+ @request.process_payload(:a => 1)
162
+ @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
163
+ end
164
+
165
+ it "sets up the credentials prior to the request" do
166
+ @http.stub!(:request)
167
+ @request.stub!(:process_result)
168
+ @request.stub!(:response_log)
169
+
170
+ @request.stub!(:user).and_return('joe')
171
+ @request.stub!(:password).and_return('mypass')
172
+ @request.should_receive(:setup_credentials).with('req')
173
+
174
+ @request.transmit(@uri, 'req', nil)
175
+ end
176
+
177
+ it "does not attempt to send any credentials if user is nil" do
178
+ @request.stub!(:user).and_return(nil)
179
+ req = mock("request")
180
+ req.should_not_receive(:basic_auth)
181
+ @request.setup_credentials(req)
182
+ end
183
+
184
+ it "setup credentials when there's a user" do
185
+ @request.stub!(:user).and_return('joe')
186
+ @request.stub!(:password).and_return('mypass')
187
+ req = mock("request")
188
+ req.should_receive(:basic_auth).with('joe', 'mypass')
189
+ @request.setup_credentials(req)
190
+ end
191
+
192
+ it "catches EOFError and shows the more informative ServerBrokeConnection" do
193
+ @http.stub!(:request).and_raise(EOFError)
194
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
195
+ end
196
+
197
+ it "execute calls execute_inner" do
198
+ @request.should_receive(:execute_inner)
199
+ @request.execute
200
+ end
201
+
202
+ it "class method execute wraps constructor" do
203
+ req = mock("rest request")
204
+ RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
205
+ req.should_receive(:execute)
206
+ RestClient::Request.execute(1 => 2)
207
+ end
208
+
209
+ it "raises a Redirect with the new location when the response is in the 30x range" do
210
+ res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
211
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
212
+ end
213
+
214
+ it "handles redirects with relative paths" do
215
+ res = mock('response', :code => '301', :header => { 'Location' => 'index' })
216
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
217
+ end
218
+
219
+ it "handles redirects with absolute paths" do
220
+ @request.instance_variable_set('@url', 'http://some/place/else')
221
+ res = mock('response', :code => '301', :header => { 'Location' => '/index' })
222
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
223
+ end
224
+
225
+ it "uses GET and clears payload when following 30x redirects" do
226
+ url = "http://example.com/redirected"
227
+
228
+ @request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
229
+
230
+ @request.should_receive(:execute_inner).once.ordered do
231
+ @request.url.should == url
232
+ @request.method.should == :get
233
+ @request.payload.should be_nil
234
+ end
235
+
236
+ @request.execute
237
+ end
238
+
239
+ it "raises Unauthorized when the response is 401" do
240
+ res = mock('response', :code => '401')
241
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
242
+ end
243
+
244
+ it "raises ResourceNotFound when the response is 404" do
245
+ res = mock('response', :code => '404')
246
+ lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
247
+ end
248
+
249
+ it "raises RequestFailed otherwise" do
250
+ res = mock('response', :code => '500')
251
+ lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
252
+ end
253
+
254
+ it "creates a proxy class if a proxy url is given" do
255
+ RestClient.stub!(:proxy).and_return("http://example.com/")
256
+ @request.net_http_class.should include(Net::HTTP::ProxyDelta)
257
+ end
258
+
259
+ it "creates a non-proxy class if a proxy url is not given" do
260
+ @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
261
+ end
262
+
263
+ it "logs a get request" do
264
+ RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
265
+ 'RestClient.get "http://url"'
266
+ end
267
+
268
+ it "logs a post request with a small payload" do
269
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
270
+ 'RestClient.post "http://url", "foo"'
271
+ end
272
+
273
+ it "logs a post request with a large payload" do
274
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
275
+ 'RestClient.post "http://url", "(1000 byte payload)"'
276
+ end
277
+
278
+ it "logs input headers as a hash" do
279
+ RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
280
+ 'RestClient.get "http://url", :accept=>"text/plain"'
281
+ end
282
+
283
+ it "logs a response including the status code, content type, and result body size in bytes" do
284
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
285
+ res.stub!(:[]).with('Content-type').and_return('text/html')
286
+ @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
287
+ end
288
+
289
+ it "logs a response with a nil Content-type" do
290
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
291
+ res.stub!(:[]).with('Content-type').and_return(nil)
292
+ @request.response_log(res).should == "# => 200 OK | 4 bytes"
293
+ end
294
+
295
+ it "logs a response with a nil body" do
296
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
297
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
298
+ @request.response_log(res).should == "# => 200 OK | text/html 0 bytes"
299
+ end
300
+
301
+ it "strips the charset from the response content type" do
302
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
303
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
304
+ @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
305
+ end
306
+
307
+ it "displays the log to stdout" do
308
+ RestClient.stub!(:log).and_return('stdout')
309
+ STDOUT.should_receive(:puts).with('xyz')
310
+ @request.display_log('xyz')
311
+ end
312
+
313
+ it "displays the log to stderr" do
314
+ RestClient.stub!(:log).and_return('stderr')
315
+ STDERR.should_receive(:puts).with('xyz')
316
+ @request.display_log('xyz')
317
+ end
318
+
319
+ it "append the log to the requested filename" do
320
+ RestClient.stub!(:log).and_return('/tmp/restclient.log')
321
+ f = mock('file handle')
322
+ File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
323
+ f.should_receive(:puts).with('xyz')
324
+ @request.display_log('xyz')
325
+ end
326
+
327
+ it "set read_timeout" do
328
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
329
+ @http.stub!(:request)
330
+ @request.stub!(:process_result)
331
+ @request.stub!(:response_log)
332
+
333
+ @net.should_receive(:read_timeout=).with(123)
334
+
335
+ @request.transmit(@uri, 'req', nil)
336
+ end
337
+
338
+ it "set open_timeout" do
339
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
340
+ @http.stub!(:request)
341
+ @request.stub!(:process_result)
342
+ @request.stub!(:response_log)
343
+
344
+ @net.should_receive(:open_timeout=).with(123)
345
+
346
+ @request.transmit(@uri, 'req', nil)
347
+ end
348
+
349
+ it "should default to not verifying ssl certificates" do
350
+ @request.verify_ssl.should == false
351
+ end
352
+
353
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
354
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
355
+ @http.stub!(:request)
356
+ @request.stub!(:process_result)
357
+ @request.stub!(:response_log)
358
+ @request.transmit(@uri, 'req', 'payload')
359
+ end
360
+
361
+ it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
362
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
363
+ @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
364
+ @http.stub!(:request)
365
+ @request.stub!(:process_result)
366
+ @request.stub!(:response_log)
367
+ @request.transmit(@uri, 'req', 'payload')
368
+ end
369
+
370
+ it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
371
+ mode = OpenSSL::SSL::VERIFY_PEER |
372
+ OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
373
+ @request = RestClient::Request.new( :method => :put,
374
+ :url => 'https://some/resource',
375
+ :payload => 'payload',
376
+ :verify_ssl => mode )
377
+ @net.should_receive(:verify_mode=).with(mode)
378
+ @http.stub!(:request)
379
+ @request.stub!(:process_result)
380
+ @request.stub!(:response_log)
381
+ @request.transmit(@uri, 'req', 'payload')
382
+ end
383
+
384
+ it "should default to not having an ssl_client_cert" do
385
+ @request.ssl_client_cert.should be(nil)
386
+ end
387
+
388
+ it "should set the ssl_client_cert if provided" do
389
+ @request = RestClient::Request.new(
390
+ :method => :put,
391
+ :url => 'https://some/resource',
392
+ :payload => 'payload',
393
+ :ssl_client_cert => "whatsupdoc!"
394
+ )
395
+ @net.should_receive(:cert=).with("whatsupdoc!")
396
+ @http.stub!(:request)
397
+ @request.stub!(:process_result)
398
+ @request.stub!(:response_log)
399
+ @request.transmit(@uri, 'req', 'payload')
400
+ end
401
+
402
+ it "should not set the ssl_client_cert if it is not provided" do
403
+ @request = RestClient::Request.new(
404
+ :method => :put,
405
+ :url => 'https://some/resource',
406
+ :payload => 'payload'
407
+ )
408
+ @net.should_not_receive(:cert=).with("whatsupdoc!")
409
+ @http.stub!(:request)
410
+ @request.stub!(:process_result)
411
+ @request.stub!(:response_log)
412
+ @request.transmit(@uri, 'req', 'payload')
413
+ end
414
+
415
+ it "should default to not having an ssl_client_key" do
416
+ @request.ssl_client_key.should be(nil)
417
+ end
418
+
419
+ it "should set the ssl_client_key if provided" do
420
+ @request = RestClient::Request.new(
421
+ :method => :put,
422
+ :url => 'https://some/resource',
423
+ :payload => 'payload',
424
+ :ssl_client_key => "whatsupdoc!"
425
+ )
426
+ @net.should_receive(:key=).with("whatsupdoc!")
427
+ @http.stub!(:request)
428
+ @request.stub!(:process_result)
429
+ @request.stub!(:response_log)
430
+ @request.transmit(@uri, 'req', 'payload')
431
+ end
432
+
433
+ it "should not set the ssl_client_key if it is not provided" do
434
+ @request = RestClient::Request.new(
435
+ :method => :put,
436
+ :url => 'https://some/resource',
437
+ :payload => 'payload'
438
+ )
439
+ @net.should_not_receive(:key=).with("whatsupdoc!")
440
+ @http.stub!(:request)
441
+ @request.stub!(:process_result)
442
+ @request.stub!(:response_log)
443
+ @request.transmit(@uri, 'req', 'payload')
444
+ end
445
+
446
+ it "should default to not having an ssl_ca_file" do
447
+ @request.ssl_ca_file.should be(nil)
448
+ end
449
+
450
+ it "should set the ssl_ca_file if provided" do
451
+ @request = RestClient::Request.new(
452
+ :method => :put,
453
+ :url => 'https://some/resource',
454
+ :payload => 'payload',
455
+ :ssl_ca_file => "Certificate Authority File"
456
+ )
457
+ @net.should_receive(:ca_file=).with("Certificate Authority File")
458
+ @http.stub!(:request)
459
+ @request.stub!(:process_result)
460
+ @request.stub!(:response_log)
461
+ @request.transmit(@uri, 'req', 'payload')
462
+ end
463
+
464
+ it "should not set the ssl_ca_file if it is not provided" do
465
+ @request = RestClient::Request.new(
466
+ :method => :put,
467
+ :url => 'https://some/resource',
468
+ :payload => 'payload'
469
+ )
470
+ @net.should_not_receive(:ca_file=).with("Certificate Authority File")
471
+ @http.stub!(:request)
472
+ @request.stub!(:process_result)
473
+ @request.stub!(:response_log)
474
+ @request.transmit(@uri, 'req', 'payload')
475
+ end
476
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe RestClient::Resource do
4
+ before do
5
+ @resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
6
+ end
7
+
8
+ context "Resource delegation" do
9
+ it "GET" do
10
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
11
+ @resource.get
12
+ end
13
+
14
+ it "POST" do
15
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
16
+ @resource.post 'abc', :content_type => 'image/jpg'
17
+ end
18
+
19
+ it "PUT" do
20
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
21
+ @resource.put 'abc', :content_type => 'image/jpg'
22
+ end
23
+
24
+ it "DELETE" do
25
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
26
+ @resource.delete
27
+ end
28
+
29
+ it "overrides resource headers" do
30
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
31
+ @resource.get 'X-Something' => '2'
32
+ end
33
+ end
34
+
35
+ it "can instantiate with no user/password" do
36
+ @resource = RestClient::Resource.new('http://some/resource')
37
+ end
38
+
39
+ it "is backwards compatible with previous constructor" do
40
+ @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
41
+ @resource.user.should == 'user'
42
+ @resource.password.should == 'pass'
43
+ end
44
+
45
+ it "concatinates urls, inserting a slash when it needs one" do
46
+ @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
47
+ end
48
+
49
+ it "concatinates urls, using no slash if the first url ends with a slash" do
50
+ @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
51
+ end
52
+
53
+ it "concatinates urls, using no slash if the second url starts with a slash" do
54
+ @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
55
+ end
56
+
57
+ it "concatinates even non-string urls, :posts + 1 => 'posts/1'" do
58
+ @resource.concat_urls(:posts, 1).should == 'posts/1'
59
+ end
60
+
61
+ it "offers subresources via []" do
62
+ parent = RestClient::Resource.new('http://example.com')
63
+ parent['posts'].url.should == 'http://example.com/posts'
64
+ end
65
+
66
+ it "transports options to subresources" do
67
+ parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
68
+ parent['posts'].user.should == 'user'
69
+ parent['posts'].password.should == 'password'
70
+ end
71
+
72
+ it "prints its url with to_s" do
73
+ RestClient::Resource.new('x').to_s.should == 'x'
74
+ end
75
+ end