wrest 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,16 @@
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
+
1
10
  require File.dirname(__FILE__) + '/../spec_helper'
2
11
 
3
12
  module Wrest
4
- describe Uri do
13
+ describe Wrest::Uri do
5
14
  def build_ok_response(body = '')
6
15
  returning mock(Net::HTTPOK) do |response|
7
16
  response.stub!(:code).and_return('200')
@@ -26,20 +35,76 @@ module Wrest
26
35
  Uri.new('http://localhost:3000').should_not be_https
27
36
  end
28
37
 
29
- it "should understand equality" do
30
- Uri.new('https://localhost:3000/ooga').should_not == 'https://localhost:3000/ooga'
31
- Uri.new('https://localhost:3000/ooga').should_not == Uri.new('https://localhost:3000/booga')
32
- Uri.new('https://localhost:3000').should_not == Uri.new('https://localhost:3500')
33
- Uri.new('https://localhost:3000').should_not == Uri.new('http://localhost:3000')
34
- Uri.new('http://localhost:3000').should == Uri.new('http://localhost:3000')
38
+ it "should know how to build a new uri from an existing one by appending a path" do
39
+ Uri.new('http://localhost:3000')['/ooga/booga'].should == Uri.new('http://localhost:3000/ooga/booga')
40
+ end
41
+
42
+ it "should know its full path" do
43
+ Uri.new('http://localhost:3000/ooga').full_path.should == '/ooga'
44
+ Uri.new('http://localhost:3000/ooga?foo=meh&bar=1').full_path.should == '/ooga?foo=meh&bar=1'
45
+ end
46
+
47
+ it "should know its host" do
48
+ Uri.new('http://localhost:3000/ooga').host.should == 'localhost'
49
+ end
50
+
51
+ it "should know its port" do
52
+ Uri.new('http://localhost:3000/ooga').port.should == 3000
53
+ Uri.new('http://localhost/ooga').port.should == 80
54
+ end
55
+
56
+ it "should include the username and password while building a new uri if no options are provided" do
57
+ Uri.new(
58
+ 'http://localhost:3000',
59
+ :username => 'foo',
60
+ :password => 'bar')['/ooga/booga'].should == Uri.new(
61
+ 'http://localhost:3000/ooga/booga',
62
+ :username => 'foo',
63
+ :password => 'bar')
35
64
  end
65
+
66
+ it "should use the username and password provided while building a new uri if present" do
67
+ uri = Uri.new('http://localhost:3000', :username => 'foo', :password => 'bar')
68
+ uri.username.should == 'foo'
69
+ uri.password.should == 'bar'
70
+
71
+ extended_uri = uri['/ooga/booga', {:username => 'meh', :password => 'baz'}]
72
+ extended_uri.username.should == 'meh'
73
+ extended_uri.password.should == 'baz'
74
+ end
75
+
76
+ describe 'Equals' do
77
+ it "should understand equality" do
78
+ Uri.new('https://localhost:3000/ooga').should_not == nil
79
+ Uri.new('https://localhost:3000/ooga').should_not == 'https://localhost:3000/ooga'
80
+ Uri.new('https://localhost:3000/ooga').should_not == Uri.new('https://localhost:3000/booga')
81
+
82
+ Uri.new('https://ooga:booga@localhost:3000/ooga').should_not == Uri.new('https://foo:bar@localhost:3000/booga')
83
+ Uri.new('http://ooga:booga@localhost:3000/ooga').should_not == Uri.new('http://foo:bar@localhost:3000/booga')
84
+ Uri.new('http://localhost:3000/ooga').should_not == Uri.new('http://foo:bar@localhost:3000/booga')
85
+
86
+ Uri.new('https://localhost:3000').should_not == Uri.new('https://localhost:3500')
87
+ Uri.new('https://localhost:3000').should_not == Uri.new('http://localhost:3000')
88
+ Uri.new('http://localhost:3000', :username => 'ooga', :password => 'booga').should_not == Uri.new('http://ooga:booga@localhost:3000')
89
+
90
+ Uri.new('http://localhost:3000').should == Uri.new('http://localhost:3000')
91
+ Uri.new('http://localhost:3000', :username => 'ooga', :password => 'booga').should == Uri.new('http://localhost:3000', :username => 'ooga', :password => 'booga')
92
+ Uri.new('http://ooga:booga@localhost:3000').should == Uri.new('http://ooga:booga@localhost:3000')
93
+ end
36
94
 
37
95
 
38
- it "should have the same hash code if it is the same uri" do
39
- Uri.new('https://localhost:3000').hash.should == Uri.new('https://localhost:3000').hash
40
- Uri.new('https://localhost:3001').hash.should_not == Uri.new('https://localhost:3000').hash
96
+ it "should have the same hash code if it is the same uri" do
97
+ Uri.new('https://localhost:3000').hash.should == Uri.new('https://localhost:3000').hash
98
+ Uri.new('http://ooga:booga@localhost:3000').hash.should == Uri.new('http://ooga:booga@localhost:3000').hash
99
+ Uri.new('http://localhost:3000', :username => 'ooga', :password => 'booga').hash.should == Uri.new('http://localhost:3000', :username => 'ooga', :password => 'booga').hash
100
+
101
+ Uri.new('https://localhost:3001').hash.should_not == Uri.new('https://localhost:3000').hash
102
+ Uri.new('https://ooga:booga@localhost:3000').hash.should_not == Uri.new('https://localhost:3000').hash
103
+ Uri.new('https://localhost:3000', :username => 'ooga', :password => 'booga').hash.should_not == Uri.new('https://localhost:3000').hash
104
+ Uri.new('https://localhost:3000', :username => 'ooga', :password => 'booga').hash.should_not == Uri.new('http://localhost:3000', :username => 'foo', :password => 'bar').hash
105
+ end
41
106
  end
42
-
107
+
43
108
  describe 'Get' do
44
109
  it "should know how to get" do
45
110
  uri = "http://localhost:3000/glassware".to_uri
@@ -48,7 +113,10 @@ module Wrest
48
113
  http = mock(Net::HTTP)
49
114
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
50
115
 
51
- http.should_receive(:get).with('/glassware', {}).and_return(build_ok_response)
116
+ request = Net::HTTP::Get.new('/glassware', {})
117
+ Net::HTTP::Get.should_receive(:new).with('/glassware', {}).and_return(request)
118
+
119
+ http.should_receive(:request).with(request, nil).and_return(build_ok_response)
52
120
 
53
121
  uri.get
54
122
  end
@@ -59,8 +127,11 @@ module Wrest
59
127
 
60
128
  http = mock(Net::HTTP)
61
129
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
62
-
63
- http.should_receive(:get).with('/glassware?owner=Kai&type=bottle', 'page' => '2', 'per_page' => '5').and_return(build_ok_response)
130
+
131
+ request = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'})
132
+ Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}).and_return(request)
133
+
134
+ http.should_receive(:request).with(request, nil).and_return(build_ok_response)
64
135
 
65
136
  uri.get({:owner => 'Kai', :type => 'bottle'}, :page => '2', :per_page => '5')
66
137
  end
@@ -72,7 +143,10 @@ module Wrest
72
143
  http = mock(Net::HTTP)
73
144
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
74
145
 
75
- http.should_receive(:get).with('/glassware?owner=Kai&type=bottle', {}).and_return(build_ok_response)
146
+ request = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {})
147
+ Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {}).and_return(request)
148
+
149
+ http.should_receive(:request).with(request, nil).and_return(build_ok_response)
76
150
 
77
151
  uri.get(:owner => 'Kai', :type => 'bottle')
78
152
  end
@@ -85,7 +159,10 @@ module Wrest
85
159
  http = mock(Net::HTTP)
86
160
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
87
161
 
88
- http.should_receive(:post).with('/glassware', '<ooga>Booga</ooga>', {'page' => '2', 'per_page' => '5'}).and_return(build_ok_response)
162
+ request = Net::HTTP::Post.new('/glassware', {'page' => '2', 'per_page' => '5'})
163
+ Net::HTTP::Post.should_receive(:new).with('/glassware', {'page' => '2', 'per_page' => '5'}).and_return(request)
164
+
165
+ http.should_receive(:request).with(request, '<ooga>Booga</ooga>').and_return(build_ok_response)
89
166
 
90
167
  uri.post '<ooga>Booga</ooga>', :page => '2', :per_page => '5'
91
168
  end
@@ -96,8 +173,11 @@ module Wrest
96
173
 
97
174
  http = mock(Net::HTTP)
98
175
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
176
+
177
+ request = Net::HTTP::Put.new('/glassware', {'page' => '2', 'per_page' => '5'})
178
+ Net::HTTP::Put.should_receive(:new).with('/glassware', {'page' => '2', 'per_page' => '5'}).and_return(request)
99
179
 
100
- http.should_receive(:put).with('/glassware', '<ooga>Booga</ooga>', {'page' => '2', 'per_page' => '5'}).and_return(build_ok_response)
180
+ http.should_receive(:request).with(request, '<ooga>Booga</ooga>').and_return(build_ok_response)
101
181
 
102
182
  uri.put '<ooga>Booga</ooga>', :page => '2', :per_page => '5'
103
183
  end
@@ -109,7 +189,10 @@ module Wrest
109
189
  http = mock(Net::HTTP)
110
190
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
111
191
 
112
- http.should_receive(:delete).with('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}).and_return(build_ok_response(nil))
192
+ request = Net::HTTP::Delete.new('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'})
193
+ Net::HTTP::Delete.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}).and_return(request)
194
+
195
+ http.should_receive(:request).with(request, nil).and_return(build_ok_response(nil))
113
196
 
114
197
  uri.delete({:owner => 'Kai', :type => 'bottle'}, :page => '2', :per_page => '5')
115
198
  end
@@ -121,7 +204,10 @@ module Wrest
121
204
  http = mock(Net::HTTP)
122
205
  Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
123
206
 
124
- http.should_receive(:options).with('/glassware').and_return(build_ok_response(nil))
207
+ request = Net::HTTP::Options.new('/glassware')
208
+ Net::HTTP::Options.should_receive(:new).with('/glassware', {}).and_return(request)
209
+
210
+ http.should_receive(:request).with(request, nil).and_return(build_ok_response(nil))
125
211
 
126
212
  uri.options
127
213
  end
@@ -133,8 +219,14 @@ module Wrest
133
219
  http = mock(Net::HTTP)
134
220
  Net::HTTP.should_receive(:new).with('localhost', 3000).any_number_of_times.and_return(http)
135
221
 
136
- http.should_receive(:get).with('/glassware?owner=Kai&type=bottle', 'page' => '2', 'per_page' => '5').and_return(build_ok_response)
137
- http.should_receive(:post).with('/glassware', '<ooga>Booga</ooga>', {'page' => '2', 'per_page' => '5'}).and_return(build_ok_response)
222
+ request_get = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'})
223
+ Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}).and_return(request_get)
224
+
225
+ request_post = Net::HTTP::Post.new('/glassware', {'page' => '2', 'per_page' => '5'})
226
+ Net::HTTP::Post.should_receive(:new).with('/glassware', {'page' => '2', 'per_page' => '5'}).and_return(request_post)
227
+
228
+ http.should_receive(:request).with(request_get, nil).and_return(build_ok_response)
229
+ http.should_receive(:request).with(request_post, '<ooga>Booga</ooga>').and_return(build_ok_response)
138
230
 
139
231
  uri.get({:owner => 'Kai', :type => 'bottle'}, :page => '2', :per_page => '5')
140
232
  uri.post '<ooga>Booga</ooga>', :page => '2', :per_page => '5'
@@ -10,7 +10,7 @@
10
10
  require File.dirname(__FILE__) + '/../spec_helper'
11
11
 
12
12
  module Wrest
13
- describe UriTemplate do
13
+ describe Wrest::UriTemplate do
14
14
  it "should not maintain a reference to the string it is initialized with" do
15
15
  url_pattern = "http://localhost:3000/:resource/:id.:format"
16
16
  template = UriTemplate.new(url_pattern)
@@ -24,5 +24,15 @@ module Wrest
24
24
  :resource => 'shen_coins', :id => 5, :format => :json
25
25
  ).should == "http://localhost:3000/shen_coins/5.json".to_uri
26
26
  end
27
+
28
+ it "should also handle username and password" do
29
+ template = UriTemplate.new("http://:user::password@coathangers.com/:resource/:id")
30
+ template.to_uri(
31
+ :user => 'kaiwren',
32
+ :password => 'fupuppies',
33
+ :resource => 'portal',
34
+ :id => '1'
35
+ ).should == "http://kaiwren:fupuppies@coathangers.com/portal/1".to_uri
36
+ end
27
37
  end
28
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidu Ponnappa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 +05:30
12
+ date: 2009-08-13 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,6 +48,8 @@ files:
48
48
  - bin/jwrest
49
49
  - bin/wrest
50
50
  - bin/wrest_shell.rb
51
+ - examples/delicious.rb
52
+ - examples/wow_realm_status.rb
51
53
  - lib/wrest.rb
52
54
  - lib/wrest/components.rb
53
55
  - lib/wrest/components/attributes_container.rb
@@ -68,11 +70,18 @@ files:
68
70
  - lib/wrest/exceptions.rb
69
71
  - lib/wrest/exceptions/method_not_overridden_exception.rb
70
72
  - lib/wrest/exceptions/unsupported_content_type_exception.rb
73
+ - lib/wrest/http.rb
74
+ - lib/wrest/http/delete.rb
75
+ - lib/wrest/http/get.rb
76
+ - lib/wrest/http/options.rb
77
+ - lib/wrest/http/post.rb
78
+ - lib/wrest/http/put.rb
79
+ - lib/wrest/http/request.rb
80
+ - lib/wrest/http/response.rb
71
81
  - lib/wrest/resource.rb
72
82
  - lib/wrest/resource/base.rb
73
83
  - lib/wrest/resource/collection.rb
74
84
  - lib/wrest/resource/state.rb
75
- - lib/wrest/response.rb
76
85
  - lib/wrest/uri.rb
77
86
  - lib/wrest/uri_template.rb
78
87
  - lib/wrest/version.rb
@@ -91,8 +100,9 @@ files:
91
100
  - spec/wrest/components/translators_spec.rb
92
101
  - spec/wrest/core_ext/hash/conversions_spec.rb
93
102
  - spec/wrest/core_ext/string/conversions_spec.rb
103
+ - spec/wrest/http/request_spec.rb
104
+ - spec/wrest/http/response_spec.rb
94
105
  - spec/wrest/resource/base_spec.rb
95
- - spec/wrest/response_spec.rb
96
106
  - spec/wrest/uri_spec.rb
97
107
  - spec/wrest/uri_template_spec.rb
98
108
  has_rdoc: true
@@ -119,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
129
  requirements: []
120
130
 
121
131
  rubyforge_project: wrest
122
- rubygems_version: 1.3.2
132
+ rubygems_version: 1.3.3
123
133
  signing_key:
124
134
  specification_version: 3
125
135
  summary: REST client library for Ruby.
@@ -137,7 +147,10 @@ test_files:
137
147
  - spec/wrest/components/translators_spec.rb
138
148
  - spec/wrest/core_ext/hash/conversions_spec.rb
139
149
  - spec/wrest/core_ext/string/conversions_spec.rb
150
+ - spec/wrest/http/request_spec.rb
151
+ - spec/wrest/http/response_spec.rb
140
152
  - spec/wrest/resource/base_spec.rb
141
- - spec/wrest/response_spec.rb
142
153
  - spec/wrest/uri_spec.rb
143
154
  - spec/wrest/uri_template_spec.rb
155
+ - examples/delicious.rb
156
+ - examples/wow_realm_status.rb
@@ -1,42 +0,0 @@
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
- module Wrest #:nodoc:
11
- # Decorates a response providing support for deserialisation.
12
- #
13
- # The following methods are also available (unlisted by rdoc because they're forwarded):
14
- #
15
- # <tt>:@http_response, :code, :message, :body, :http_version,
16
- # :[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch,
17
- # :get_fields, :key?, :type_params</tt>
18
- #
19
- # They behave exactly like their Net::HTTPResponse equivalents.
20
- class Response
21
- extend Forwardable
22
- def_delegators :@http_response, :code, :message, :body, :http_version,
23
- :[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch,
24
- :get_fields, :key?, :type_params
25
-
26
- def initialize(http_response)
27
- @http_response = http_response
28
- end
29
-
30
- def deserialise
31
- deserialise_using(Wrest::Components::Translators.lookup(@http_response.content_type))
32
- end
33
-
34
- def deserialise_using(translator)
35
- translator.deserialise(@http_response)
36
- end
37
-
38
- def headers
39
- @http_response.to_hash
40
- end
41
- end
42
- end