wrest 0.0.8-java → 0.0.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/README.rdoc +24 -17
  2. data/Rakefile +6 -6
  3. data/VERSION.yml +1 -1
  4. data/examples/delicious.rb +7 -3
  5. data/examples/redirection.rb +27 -0
  6. data/examples/twitter.rb +78 -0
  7. data/{spec/wrest/http/request_spec.rb → examples/twitter_public_timeline.rb} +15 -12
  8. data/examples/wow_realm_status.rb +7 -3
  9. data/lib/wrest/components/attributes_container.rb +23 -5
  10. data/lib/wrest/components/attributes_container/alias_accessors.rb +66 -0
  11. data/lib/wrest/components/attributes_container/typecaster.rb +44 -38
  12. data/lib/wrest/components/mutators/base.rb +1 -1
  13. data/lib/wrest/components/translators.rb +1 -1
  14. data/lib/wrest/core_ext/string/conversions.rb +1 -1
  15. data/lib/wrest/exceptions.rb +14 -2
  16. data/lib/wrest/http.rb +1 -0
  17. data/lib/wrest/http/get.rb +2 -0
  18. data/lib/wrest/http/redirection.rb +35 -0
  19. data/lib/wrest/http/request.rb +37 -13
  20. data/lib/wrest/http/response.rb +17 -0
  21. data/lib/wrest/resource/base.rb +1 -2
  22. data/lib/wrest/uri.rb +2 -0
  23. data/lib/wrest/version.rb +1 -1
  24. data/spec/functional/sample_rails_app/README +3 -0
  25. data/spec/functional/sample_rails_app/Rakefile +10 -0
  26. data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +10 -0
  27. data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +7 -0
  28. data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +3 -0
  29. data/spec/functional/sample_rails_app/app/models/bottle.rb +3 -0
  30. data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +3 -0
  31. data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +3 -0
  32. data/spec/functional/sample_rails_app/config/boot.rb +110 -0
  33. data/spec/functional/sample_rails_app/config/database.yml +16 -0
  34. data/spec/functional/sample_rails_app/config/environment.rb +42 -0
  35. data/spec/functional/sample_rails_app/config/environments/development.rb +17 -0
  36. data/spec/functional/sample_rails_app/config/environments/production.rb +28 -0
  37. data/spec/functional/sample_rails_app/config/environments/test.rb +28 -0
  38. data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/functional/sample_rails_app/config/initializers/inflections.rb +10 -0
  40. data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +5 -0
  41. data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +19 -0
  42. data/spec/functional/sample_rails_app/config/initializers/session_store.rb +15 -0
  43. data/spec/functional/sample_rails_app/config/locales/en.yml +5 -0
  44. data/spec/functional/sample_rails_app/config/routes.rb +3 -0
  45. data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
  46. data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +13 -0
  47. data/spec/functional/sample_rails_app/db/schema.rb +20 -0
  48. data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
  49. data/spec/functional/sample_rails_app/log/development.log +1 -0
  50. data/spec/functional/sample_rails_app/public/404.html +30 -0
  51. data/spec/functional/sample_rails_app/public/422.html +30 -0
  52. data/spec/functional/sample_rails_app/public/500.html +30 -0
  53. data/spec/functional/sample_rails_app/public/favicon.ico +0 -0
  54. data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
  55. data/spec/functional/sample_rails_app/public/index.html +275 -0
  56. data/spec/functional/sample_rails_app/public/robots.txt +5 -0
  57. data/spec/functional/sample_rails_app/script/about +4 -0
  58. data/spec/functional/sample_rails_app/script/autospec +6 -0
  59. data/spec/functional/sample_rails_app/script/console +3 -0
  60. data/spec/functional/sample_rails_app/script/dbconsole +3 -0
  61. data/spec/functional/sample_rails_app/script/destroy +3 -0
  62. data/spec/functional/sample_rails_app/script/generate +3 -0
  63. data/spec/functional/sample_rails_app/script/performance/benchmarker +3 -0
  64. data/spec/functional/sample_rails_app/script/performance/profiler +3 -0
  65. data/spec/functional/sample_rails_app/script/plugin +3 -0
  66. data/spec/functional/sample_rails_app/script/runner +3 -0
  67. data/spec/functional/sample_rails_app/script/server +3 -0
  68. data/spec/functional/sample_rails_app/script/spec +10 -0
  69. data/spec/functional/sample_rails_app/script/spec_server +9 -0
  70. data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +9 -0
  71. data/spec/functional/sample_rails_app/test/test_helper.rb +38 -0
  72. data/spec/functional/sample_rails_app/tmtags +2559 -0
  73. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +20 -0
  74. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +100 -0
  75. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +18 -0
  76. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +5 -0
  77. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +1 -0
  78. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +14 -0
  79. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +140 -0
  80. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +16 -0
  81. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +26 -0
  82. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +16 -0
  83. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +26 -0
  84. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +23 -0
  85. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +15 -0
  86. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +235 -0
  87. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +84 -0
  88. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +337 -0
  89. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +63 -0
  90. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +50 -0
  91. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +75 -0
  92. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +65 -0
  93. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +74 -0
  94. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +9 -0
  95. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +88 -0
  96. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +29 -0
  97. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +262 -0
  98. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +62 -0
  99. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +57 -0
  100. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +462 -0
  101. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +4 -0
  102. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +107 -0
  103. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +98 -0
  104. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +5 -0
  105. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +173 -0
  106. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +98 -0
  107. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +1 -0
  108. data/spec/functional/spec_helper.rb +0 -0
  109. data/spec/{spec_helper.rb → unit/spec_helper.rb} +2 -2
  110. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +49 -0
  111. data/spec/{wrest → unit/wrest}/components/attributes_container/typecaster_spec.rb +28 -8
  112. data/spec/{wrest → unit/wrest}/components/attributes_container_spec.rb +42 -15
  113. data/spec/{wrest → unit/wrest}/components/mutators/base_spec.rb +1 -1
  114. data/spec/{wrest → unit/wrest}/components/mutators/camel_to_snake_spec.rb +0 -0
  115. data/spec/{wrest → unit/wrest}/components/mutators/xml_mini_type_caster_spec.rb +0 -0
  116. data/spec/{wrest → unit/wrest}/components/mutators/xml_simple_type_caster_spec.rb +0 -0
  117. data/spec/{wrest → unit/wrest}/components/mutators_spec.rb +0 -0
  118. data/spec/{wrest → unit/wrest}/components/translators/xml_spec.rb +0 -0
  119. data/spec/{wrest → unit/wrest}/components/translators_spec.rb +1 -1
  120. data/spec/{wrest → unit/wrest}/core_ext/hash/conversions_spec.rb +0 -0
  121. data/spec/{wrest → unit/wrest}/core_ext/string/conversions_spec.rb +14 -0
  122. data/spec/unit/wrest/http/redirection_spec.rb +42 -0
  123. data/spec/unit/wrest/http/request_spec.rb +70 -0
  124. data/spec/unit/wrest/http/response_spec.rb +45 -0
  125. data/spec/{wrest → unit/wrest}/resource/base_spec.rb +5 -4
  126. data/spec/{wrest → unit/wrest}/uri_spec.rb +68 -67
  127. data/spec/{wrest → unit/wrest}/uri_template_spec.rb +0 -0
  128. metadata +188 -39
  129. data/lib/wrest/exceptions/method_not_overridden_exception.rb +0 -17
  130. data/lib/wrest/exceptions/unsupported_content_type_exception.rb +0 -17
  131. data/spec/wrest/http/response_spec.rb +0 -21
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
File without changes
@@ -1,7 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/wrest")
2
2
  require 'spec'
3
3
 
4
- ["/custom_matchers/**/*.rb"].each{|directory|
4
+ ['/../custom_matchers/**/*.rb'].each{|directory|
5
5
  Dir["#{File.expand_path(File.dirname(__FILE__) + directory)}"].each { |file|
6
6
  require file
7
7
  }
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ module Wrest::Components
4
+ describe AttributesContainer::AliasAccessors do
5
+ before :each do
6
+ @Demon = Class.new
7
+ @Demon.class_eval do
8
+ include Wrest::Components::AttributesContainer
9
+ end
10
+ end
11
+
12
+ it "should provide a macro to enable aliasing accessors" do
13
+ lambda{ @Demon.class_eval{ alias_accessors :shiriki => :chambala } }.should_not raise_error(NoMethodError)
14
+ end
15
+
16
+ describe 'aliasing' do
17
+ before :each do
18
+ @Demon.class_eval{ alias_accessors :sex => :gender, :age => :maturity }
19
+ end
20
+
21
+ it "should provide an accessor methods when we alias to an attribute" do
22
+ demon = @Demon.new
23
+ demon.should respond_to(:gender)
24
+ demon.should respond_to(:gender=)
25
+ demon.should respond_to(:gender?)
26
+ demon.should respond_to(:maturity)
27
+ demon.should respond_to(:maturity=)
28
+ demon.should respond_to(:maturity?)
29
+ end
30
+
31
+ it "should ensure that the aliased getter method delegates to the actual getter" do
32
+ demon = @Demon.new :sex => 'male'
33
+ demon.gender.should == 'male'
34
+ end
35
+
36
+ it "should ensure that the aliased setter method delegates to the actual getter" do
37
+ demon = @Demon.new
38
+ demon.should_receive(:sex=).with('male')
39
+ demon.gender = 'male'
40
+ end
41
+
42
+ it "should ensure that the aliased query method delegates to the actual queryier" do
43
+ demon = @Demon.new :age => '1000'
44
+ demon.gender?.should be_false
45
+ demon.maturity?.should be_true
46
+ end
47
+ end
48
+ end
49
+ end
@@ -5,8 +5,8 @@ module Wrest::Components
5
5
  before :each do
6
6
  @Demon = Class.new
7
7
  @Demon.class_eval do
8
- include AttributesContainer
9
- include AttributesContainer::Typecaster
8
+ include Wrest::Components::AttributesContainer
9
+ include Wrest::Components::AttributesContainer::Typecaster
10
10
  end
11
11
  end
12
12
 
@@ -16,10 +16,30 @@ module Wrest::Components
16
16
  kai_wren.age.should == 1
17
17
  end
18
18
 
19
- it "should not apply a lambda to the value of a given key if it is not a string" do
20
- @Demon.class_eval{ typecast :age => lambda{|id_string| id_string.to_i} }
21
- kai_wren = @Demon.new('age' => :ooga)
22
- kai_wren.age.should == :ooga
19
+ describe "where the value is not a typecastable type" do
20
+ it "string should not typecast" do
21
+ @Demon.class_eval{ typecast :age => lambda{|id_string| id_string.to_i} }
22
+ kai_wren = @Demon.new('age' => :ooga)
23
+ kai_wren.age.should == :ooga
24
+ end
25
+
26
+ it "hash should not typecast" do
27
+ class TestUser
28
+ include Wrest::Components::AttributesContainer
29
+ end
30
+
31
+ @Demon.class_eval{ typecast :user => lambda{|user| TestUser.new(user)}}
32
+
33
+ kai_wren = @Demon.new('user' => {'foo' => 'bar'})
34
+ kai_wren.user.class.should == TestUser
35
+ kai_wren.user.foo.should == 'bar'
36
+ end
37
+
38
+ it "array should not typecast" do
39
+ @Demon.class_eval{ typecast :addresses => lambda{|addresses| addresses.first} }
40
+ kai_wren = @Demon.new('addresses' => ['foo', 'bar'])
41
+ kai_wren.addresses.should == 'foo'
42
+ end
23
43
  end
24
44
 
25
45
  it "should leave nils unchanged" do
@@ -38,8 +58,8 @@ module Wrest::Components
38
58
  before :each do
39
59
  @Sidhe = Class.new
40
60
  @Sidhe.class_eval do
41
- include AttributesContainer
42
- include AttributesContainer::Typecaster
61
+ include Wrest::Components::AttributesContainer
62
+ include Wrest::Components::AttributesContainer::Typecaster
43
63
 
44
64
  typecast :age => as_integer
45
65
  end
@@ -12,10 +12,34 @@ require File.dirname(__FILE__) + '/../../spec_helper'
12
12
  module Wrest::Components
13
13
  describe AttributesContainer do
14
14
  class HumanBeing
15
- include AttributesContainer
15
+ include Wrest::Components::AttributesContainer
16
16
  always_has :id
17
17
  end
18
18
 
19
+ describe "typecasting" do
20
+ before(:each) do
21
+ @Demon = Class.new
22
+
23
+ @Demon.class_eval do
24
+ include Wrest::Components::AttributesContainer
25
+ end
26
+ end
27
+
28
+ it "should delegate to AttributesContainer::Typecaster#typecast to actually do the typecasting" do
29
+ @Demon.class_eval do
30
+ typecast :foo => lambda{|value| value.to_i}
31
+ end
32
+ @Demon.new(:foo => '1').foo.should == 1
33
+ end
34
+
35
+ it "should provide helpers for common typecasts" do
36
+ @Demon.class_eval do
37
+ typecast :foo => as_integer
38
+ end
39
+ @Demon.new(:foo => '1').foo.should == 1
40
+ end
41
+ end
42
+
19
43
  it "should allow instantiation with no attributes" do
20
44
  lambda{ HumanBeing.new }.should_not raise_error
21
45
  end
@@ -26,39 +50,41 @@ module Wrest::Components
26
50
  @Demon = Class.new
27
51
  end
28
52
 
53
+ # Methods are string in 1.8 and symbols in 1.9. We'll use to_sym to
54
+ # allow us to build on both.
29
55
  it "should define attribute getters at the class level" do
30
56
  kai_wren = @Demon.new
31
- kai_wren.methods.should_not include('trainer')
57
+ kai_wren.methods.map(&:to_sym).should_not include(:trainer)
32
58
 
33
59
  @Demon.class_eval{
34
- include AttributesContainer
60
+ include Wrest::Components::AttributesContainer
35
61
  always_has :trainer
36
62
  }
37
63
 
38
- kai_wren.methods.should include('trainer')
64
+ kai_wren.methods.map(&:to_sym).should include(:trainer)
39
65
  end
40
66
 
41
67
  it "should define attribute setters at the class level" do
42
68
  kai_wren = @Demon.new
43
- kai_wren.methods.should_not include('trainer=')
69
+ kai_wren.methods.map(&:to_sym).should_not include(:trainer=)
44
70
 
45
71
  @Demon.class_eval{
46
- include AttributesContainer
72
+ include Wrest::Components::AttributesContainer
47
73
  always_has :trainer
48
74
  }
49
75
 
50
- kai_wren.methods.should include('trainer=')
76
+ kai_wren.methods.map(&:to_sym).should include(:trainer=)
51
77
  end
52
78
 
53
79
  it "should define attribute query methods at the class level" do
54
80
  kai_wren = @Demon.new
55
- kai_wren.methods.should_not include('trainer?')
81
+ kai_wren.methods.map(&:to_sym).should_not include(:trainer?)
56
82
 
57
83
  @Demon.class_eval{
58
- include AttributesContainer
84
+ include Wrest::Components::AttributesContainer
59
85
  always_has :trainer
60
86
  }
61
- kai_wren.methods.should include('trainer?')
87
+ kai_wren.methods.map(&:to_sym).should include(:trainer?)
62
88
  end
63
89
  end
64
90
 
@@ -66,7 +92,7 @@ module Wrest::Components
66
92
  before :each do
67
93
  @Demon = Class.new
68
94
  @Demon.class_eval{
69
- include AttributesContainer
95
+ include Wrest::Components::AttributesContainer
70
96
  always_has :trainer
71
97
 
72
98
  def method_missing(method_name, *args)
@@ -155,14 +181,15 @@ module Wrest::Components
155
181
  @li_piao.should_not respond_to(:god=)
156
182
  end
157
183
 
158
- it "should fail when query methods for attributes that don't exist are invoked" do
159
- lambda{ @li_piao.ooga? }.should raise_error(NoMethodError)
184
+ it "should return false when query methods for attributes that don't exist are invoked" do
185
+ @li_piao.ooga?.should be_false
160
186
  end
161
187
 
162
188
  it "should provide query methods for attributes" do
163
189
  li_piao = HumanBeing.new( :profession => 'Natural Magician', :enhanced_by => nil)
164
190
  li_piao.profession?.should be_true
165
191
  li_piao.enhanced_by?.should be_false
192
+ li_piao.gender?.should be_false
166
193
  end
167
194
 
168
195
  it "should respond to query methods for attributes" do
@@ -179,14 +206,14 @@ module Wrest::Components
179
206
  @li_piao.id = 6
180
207
  @li_piao.id.should == 6
181
208
  end
182
-
209
+
183
210
  it "should provide getter and query methods to instance which has corresponding attribute" do
184
211
  zotoh_zhaan = HumanBeing.new(:species => "Delvian")
185
212
  zotoh_zhaan.species.should == "Delvian"
186
213
  zotoh_zhaan.species?.should be_true
187
214
  zotoh_zhaan.species = "Human"
188
215
  lambda{@li_piao.species}.should raise_error(NoMethodError)
189
- lambda{@li_piao.species?}.should raise_error(NoMethodError)
216
+ @li_piao.species?.should be_false
190
217
  @li_piao.should_not respond_to(:species=)
191
218
  @li_piao.methods.grep(/:species=/).should be_empty
192
219
  end
@@ -12,7 +12,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
12
12
  module Wrest::Components
13
13
  describe Mutators::Base do
14
14
  it "should raise an exception if mutate is invoked without do_mutate being implemented in a subclass" do
15
- lambda{ Class.new(Mutators::Base).new.mutate([]) }.should raise_error(Wrest::Exceptions::MethodNotOverriddenException)
15
+ lambda{ Class.new(Mutators::Base).new.mutate([]) }.should raise_error(Wrest::Exceptions::MethodNotOverridden)
16
16
  end
17
17
 
18
18
  it "should ensure that the next mutator is invoked for a subclass" do
@@ -12,7 +12,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
12
12
  module Wrest::Components
13
13
  describe Translators do
14
14
  it "should know how to raise an exception if the mime type doesn't exist" do
15
- lambda{ Translators.lookup('weird/unknown')}.should raise_error(Wrest::Exceptions::UnsupportedContentTypeException)
15
+ lambda{ Translators.lookup('weird/unknown')}.should raise_error(Wrest::Exceptions::UnsupportedContentType)
16
16
  end
17
17
  end
18
18
  end
@@ -22,4 +22,18 @@ describe String, 'conversions' do
22
22
  uri.username.should == 'ooga'
23
23
  uri.password.should == 'booga'
24
24
  end
25
+
26
+ it "should strip leading spaces from a uri before building it" do
27
+ ' http://localhost:3000'.to_uri.should == Wrest::Uri.new('http://localhost:3000')
28
+ end
29
+
30
+ it "should strip trailing spaces from a uri before building it" do
31
+ 'http://localhost:3000 '.to_uri.should == Wrest::Uri.new('http://localhost:3000')
32
+ end
33
+
34
+ it "should levae the original string unchanged after building a uri from it" do
35
+ url = ' http://localhost:3000 '
36
+ url.to_uri.should == Wrest::Uri.new('http://localhost:3000')
37
+ url.should == ' http://localhost:3000 '
38
+ end
25
39
  end
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Wrest::Http::Redirection do
4
+
5
+ it "should make a request to the url in its location header and return the response" do
6
+ mock_net_http_response = mock(Net::HTTPRedirection)
7
+ redirect_url = 'http://redirect.com'
8
+ redirect_uri = redirect_url.to_uri
9
+ mock_net_http_response.should_receive(:[]).with('location').and_return(redirect_url)
10
+ mock_net_http_response.should_receive(:code).and_return('200')
11
+
12
+ Wrest::Uri.should_receive(:new).with(redirect_url, anything).and_return(redirect_uri)
13
+
14
+
15
+ after_redirect_request = Wrest::Http::Get.new(redirect_uri)
16
+ final_mock_response = mock(Wrest::Http::Response)
17
+ after_redirect_request.should_receive(:invoke).and_return(final_mock_response)
18
+
19
+ Wrest::Http::Get.should_receive(:new).with(redirect_uri, {}, {}, {:username=>nil, :password=>nil}).and_return(after_redirect_request)
20
+
21
+ response = Wrest::Http::Redirection.new(mock_net_http_response)
22
+ response.follow(:follow_redirects_count => 0, :follow_redirects_limit => 5).should == final_mock_response
23
+ end
24
+
25
+ it "should raise a Wrest::Exceptions::AutoRedirectLimitExceeded if there are more redirections than the limit" do
26
+ request_url = 'http://redirect.com'
27
+
28
+ response = mock(Net::HTTPRedirection)
29
+ response.stub!(:code).and_return('301')
30
+ response.stub!(:message).and_return('')
31
+ response.stub!(:body).and_return('')
32
+ response.should_receive(:[]).with('location').exactly(5).times.and_return(request_url)
33
+
34
+ http_connection = mock(Net::HTTP)
35
+ http_connection.stub!(:read_timeout=)
36
+ http_connection.should_receive(:request).exactly(5).times.and_return(response)
37
+
38
+ Net::HTTP.should_receive(:new).exactly(5).times.and_return(http_connection)
39
+
40
+ lambda{ request_url.to_uri(:follow_redirects_limit => 5).get }.should raise_error(Wrest::Exceptions::AutoRedirectLimitExceeded)
41
+ end
42
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ require File.dirname(__FILE__) + '/../../spec_helper'
11
+
12
+ describe Wrest::Http::Request do
13
+ it "should convert all symbols in header keys to strings" do
14
+ Wrest::Http::Request.new(
15
+ 'http://localhost/foo'.to_uri, Net::HTTP::Get, {},
16
+ nil, 'Content-Type' => 'application/xml', :per_page => '10'
17
+ ).headers.should == {
18
+ 'Content-Type' => 'application/xml',
19
+ 'per_page' => '10'
20
+ }
21
+ end
22
+
23
+ it "should default the 'follow_redirects' option to true for a Get" do
24
+ Wrest::Http::Get.new('http://localhost/foo'.to_uri).follow_redirects.should be_true
25
+ end
26
+
27
+ it "should default the 'follow_redirects_count' option to 0" do
28
+ Wrest::Http::Get.new('http://localhost/foo'.to_uri).follow_redirects_count.should == 0
29
+ end
30
+
31
+ it "should default the 'follow_redirects_limit' option to 5" do
32
+ Wrest::Http::Get.new('http://localhost/foo'.to_uri).follow_redirects_limit.should == 5
33
+ end
34
+
35
+ it "should allow Gets to disable redirect follows" do
36
+ Wrest::Http::Get.new('http://localhost/foo'.to_uri, {}, {}, {:follow_redirects => false}).follow_redirects.should be_false
37
+ end
38
+
39
+ it "should increment the follow_redirects_count for every redirect leving the count unaffected in previous requests" do
40
+ request = Wrest::Http::Get.new('http://localhost/foo'.to_uri)
41
+ redirect_location = 'http://coathangers.com'
42
+ redirected_request = mock('Request to http://coathangers.com')
43
+
44
+ mock_connection = mock('Http Connection')
45
+ request.stub!(:create_connection).and_return(mock_connection)
46
+
47
+ raw_response = mock(Net::HTTPRedirection)
48
+ raw_response.stub!(:code).and_return('301')
49
+ raw_response.stub!(:message).and_return('')
50
+ raw_response.stub!(:body).and_return('')
51
+ raw_response.stub!(:[]).with('location').and_return(redirect_location)
52
+
53
+ response = Wrest::Http::Redirection.new(raw_response)
54
+
55
+ mock_connection.should_receive(:request).and_return(raw_response)
56
+
57
+ Wrest::Http::Response.should_receive(:new).and_return(response)
58
+ redirected_request.stub!(:get)
59
+ redirect_location.should_receive(:to_uri).with(hash_including(:follow_redirects_count=>1)).and_return(redirected_request)
60
+
61
+ request.invoke
62
+ request.follow_redirects_count.should == 0
63
+ end
64
+
65
+ it "should default the 'follow_redirects' option to false for a Post, Put or Delete" do
66
+ Wrest::Http::Post.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
67
+ Wrest::Http::Put.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
68
+ Wrest::Http::Delete.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
69
+ end
70
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ module Wrest
4
+ describe Http::Response do
5
+ it "should build a Redirection instead of a normal response if the code is 3xx" do
6
+ http_response = mock(Net::HTTPRedirection)
7
+ http_response.stub!(:code).and_return('301')
8
+
9
+ Http::Response.new(http_response).class.should == Wrest::Http::Redirection
10
+ end
11
+
12
+ it "should build a normal Response for non 3xx codes" do
13
+ http_response = mock(Net::HTTPResponse)
14
+ http_response.stub!(:code).and_return('200')
15
+
16
+ Http::Response.new(http_response).class.should == Wrest::Http::Response
17
+ end
18
+
19
+ it "should know how to delegate to a translator" do
20
+ http_response = mock('response')
21
+ http_response.stub!(:code).and_return('200')
22
+ Components::Translators::Xml.should_receive(:deserialise).with(http_response)
23
+ Http::Response.new(http_response).deserialise_using(Components::Translators::Xml)
24
+ end
25
+
26
+ it "should know how to load a translator based on content type" do
27
+ http_response = mock('response')
28
+ http_response.stub!(:code).and_return('422')
29
+ http_response.should_receive(:content_type).and_return('application/xml')
30
+
31
+ response = Http::Response.new(http_response)
32
+ response.should_receive(:deserialise_using).with(Components::Translators::Xml)
33
+
34
+ response.deserialise
35
+ end
36
+
37
+ it "should simply return itself when asked to follow (null object behaviour - see MovedPermanently for more context)" do
38
+ http_response = mock('response')
39
+ http_response.stub!(:code).and_return('422')
40
+
41
+ response = Http::Response.new(http_response)
42
+ response.follow.equal?(response).should be_true
43
+ end
44
+ end
45
+ end