weary 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,7 @@ describe Weary::Request do
17
17
  test.class.should == Net::HTTP
18
18
  end
19
19
 
20
+ # replace with FakeWeb
20
21
  it "should perform the request and retrieve a response" do
21
22
  test = Weary::Request.new("http://foo.bar")
22
23
  method = test.method
@@ -26,6 +27,7 @@ describe Weary::Request do
26
27
  test.perform.redirected?.should == true
27
28
  end
28
29
 
30
+ # replace with FakeWeb
29
31
  it "should follow redirects" do
30
32
  test = Weary::Request.new("http://foo.bar")
31
33
  method = test.method
@@ -5,96 +5,252 @@ describe Weary::Resource do
5
5
  @test = Weary::Resource.new("test")
6
6
  end
7
7
 
8
- it 'defaults to a GET request' do
9
- @test.via.should == :get
8
+ describe 'Name' do
9
+ it 'has a name' do
10
+ @test.name.should == "test"
11
+ end
12
+
13
+ it 'replaces the whitespace in the name' do
14
+ @test.name = " test number two"
15
+ @test.name.should == "test_number_two"
16
+ @test.name = "test\nthree"
17
+ @test.name.should == "test_three"
18
+ end
19
+
20
+ it 'downcases everything' do
21
+ @test.name = "TEST"
22
+ @test.name.should == "test"
23
+ end
24
+
25
+ it 'is a string' do
26
+ @test.name = :test
27
+ @test.name.should == "test"
28
+ end
10
29
  end
11
30
 
12
- it 'should add requirements to the "with" array' do
13
- @test.requires = [:foo, :bar]
14
- @test.with.should == [:foo, :bar]
15
- end
16
-
17
- it 'with paramaters could be comma delimited strings' do
18
- @test.with = "foo", "bar"
19
- @test.with.should == [:foo, :bar]
20
- end
21
-
22
- it 'with params could be a hash' do
23
- @test.with = {:foo => "Foo", :bar => "Bar"}
24
- @test.with.should == {:foo => "Foo", :bar => "Bar"}
25
- @test.requires = [:id, :user]
26
- @test.with.should == {:bar=>"Bar", :user => nil, :foo => "Foo", :id => nil}
27
- @test.with = [:foo, :bar]
28
- @test.with.should == [:foo, :bar, :id, :user]
29
- end
30
-
31
- it 'authenticates? should be boolean' do
32
- @test.authenticates = "foobar"
33
- @test.authenticates?.should == true
34
- @test.authenticates = false
35
- @test.authenticates?.should == false
36
- end
37
-
38
- it "oauth should be boolean" do
39
- @test.oauth = "foobar"
40
- @test.oauth?.should == true
41
- @test.oauth = false
42
- @test.oauth?.should == false
31
+ describe 'Via' do
32
+ it 'defaults as a GET request' do
33
+ @test.via.should == :get
34
+ end
35
+
36
+ it 'normalizes HTTP request verbs' do
37
+ @test.via = "POST"
38
+ @test.via.should == :post
39
+ end
40
+
41
+ it 'remains a GET if the input verb is not understood' do
42
+ @test.via = "foobar"
43
+ @test.via.should == :get
44
+ end
43
45
  end
44
46
 
45
- it "oauth should override basic authentication" do
46
- @test.authenticates = true
47
- @test.oauth = true
48
- @test.authenticates?.should == false
49
- @test.oauth?.should == true
47
+ describe 'Parameters' do
48
+ it 'is an array of params' do
49
+ @test.with = [:uid, :date]
50
+ @test.with.should == [:uid, :date]
51
+ @test.with = :user, :message
52
+ @test.with.should == [:user, :message]
53
+ end
54
+
55
+ it 'stores the params as symbols' do
56
+ @test.with = "user", "message"
57
+ @test.with.should == [:user, :message]
58
+ end
59
+
60
+ it 'defines required params' do
61
+ @test.requires = [:uid, :date]
62
+ @test.requires.should == [:uid, :date]
63
+ @test.requires = :user, :message
64
+ @test.requires.should == [:user, :message]
65
+ end
66
+
67
+ it 'stores the required params as symbols' do
68
+ @test.requires = "user", "message"
69
+ @test.requires.should == [:user, :message]
70
+ end
71
+
72
+ it 'required params inherently become passed params' do
73
+ @test.requires = [:user, :message]
74
+ @test.requires.should == [:user, :message]
75
+ @test.with.should == [:user, :message]
76
+ end
77
+
78
+ it 'merges required params with optional params' do
79
+ @test.with = [:user, :message]
80
+ @test.requires = [:id, :time]
81
+ @test.with.should == [:user, :message, :id, :time]
82
+ @test.requires.should == [:id, :time]
83
+ end
84
+
85
+ it 'optional params should merge with required params' do
86
+ @test.requires = [:id, :time]
87
+ @test.with.should == [:id, :time]
88
+ @test.with = [:user, :message]
89
+ @test.requires.should == [:id, :time]
90
+ @test.with.should == [:id, :time, :user, :message]
91
+ end
50
92
  end
51
93
 
52
- it "providing an access token should set oauth to true" do
53
- consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
54
- token = OAuth::AccessToken.new(consumer, "token", "secret")
55
- @test.oauth = false
56
- @test.access_token = token
57
- @test.oauth?.should == true
58
- @test.access_token.should == token
94
+ describe 'Authenticates' do
95
+ it 'defaults to false' do
96
+ @test.authenticates?.should == false
97
+ end
98
+
99
+ it 'is always set to a boolean' do
100
+ @test.authenticates = "foobar"
101
+ @test.authenticates?.should == true
102
+ @test.authenticates = "false"
103
+ @test.authenticates?.should == true
104
+ end
59
105
  end
60
106
 
61
- it "an access token must contain an OAuth::AccessToken" do
62
- lambda { @test.access_token = "foobar" }.should raise_error
107
+ describe 'Redirection' do
108
+ it 'defaults to true' do
109
+ @test.follows?.should == true
110
+ end
111
+
112
+ it 'is always set to a boolean' do
113
+ @test.follows = "foobar"
114
+ @test.follows?.should == true
115
+ @test.follows = "false"
116
+ @test.follows?.should == true
117
+ @test.follows = false
118
+ @test.follows?.should == false
119
+ end
63
120
  end
64
121
 
65
- it 'follows_redirects? should be boolean' do
66
- @test.follows = "false"
67
- @test.follows_redirects?.should == true
68
- @test.follows = false
69
- @test.follows_redirects?.should == false
122
+ describe 'URL' do
123
+ it 'is a valid URI' do
124
+ @test.url = 'http://foo.bar/foobar'
125
+ @test.url.scheme.should == 'http'
126
+ @test.url.host.should == 'foo.bar'
127
+ @test.url.path.should == '/foobar'
128
+ @test.url.normalize.to_s.should == 'http://foo.bar/foobar'
129
+ end
130
+
131
+ it "rejects fake url's" do
132
+ lambda { @test.url = "this is not really a url" }.should raise_error
133
+ end
70
134
  end
71
135
 
72
- it 'should be intelligent about crafting names' do
73
- @test.name = "foo bar \n"
74
- @test.name.should == "foo_bar"
136
+ describe 'Request Builder' do
137
+ before do
138
+ @test.url = 'http://foo.bar'
139
+ @test.with = :user, :message
140
+ @test.headers = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
141
+ end
142
+
143
+ it "merges defaults in with passed params" do
144
+ params = {:message => "Hello"}
145
+ defaults = {:user => "me"}
146
+ @test.setup_parameters(params, defaults).should == {:message => "Hello", :user => "me"}
147
+ @test.setup_parameters(params).should == {:message => "Hello"}
148
+ end
149
+
150
+ it 'alerts if a required param is missing' do
151
+ @test.requires = [:id]
152
+ params = {:user => "me", :message => "Hello"}
153
+ lambda { @test.find_missing_requirements(params) }.should raise_error
154
+ end
155
+
156
+ it 'removes unnecessary parameters' do
157
+ params = {:message => "Hello", :user => "me", :foo => "bar"}
158
+ @test.remove_unnecessary_params(params).should == {:message => "Hello", :user => "me"}
159
+ end
160
+
161
+ it 'prepares param options for the request' do
162
+ params = {:user => "me", :message => "Hello"}
163
+ @test.prepare_request_body(params)[:query].should == {:user => "me", :message => "Hello"}
164
+ @test.prepare_request_body(params).has_key?(:body).should == false
165
+ @test.via = :post
166
+ @test.prepare_request_body(params)[:body].should == {:user => "me", :message => "Hello"}
167
+ @test.prepare_request_body(params).has_key?(:query).should == false
168
+ end
169
+
170
+ it 'alerts if credentials are missing but authorization is required' do
171
+ @test.authenticates = true
172
+ lambda { @test.setup_authentication({}) }.should raise_error
173
+ end
174
+
175
+ it 'negotiates what form of authentication is used (basic or oauth)' do
176
+ @test.authenticates = true
177
+ basic_auth = {:username => "mwunsch", :password => "secret123"}
178
+ oauth_consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
179
+ oauth_token = OAuth::AccessToken.new(oauth_consumer, "token", "secret")
180
+
181
+ basic_authentication = @test.setup_authentication({},basic_auth)
182
+ oauth_authentication = @test.setup_authentication({},oauth_token)
183
+
184
+ basic_authentication.has_key?(:basic_auth).should == true
185
+ basic_authentication.has_key?(:oauth).should == false
186
+ basic_authentication[:basic_auth].should == basic_auth
187
+
188
+ oauth_authentication.has_key?(:oauth).should == true
189
+ oauth_authentication.has_key?(:basic_auth).should == false
190
+ oauth_authentication[:oauth].should == oauth_token
191
+ end
192
+
193
+ it 'sets request options' do
194
+ setup = @test.setup_options({})
195
+
196
+ setup.has_key?(:headers).should == true
197
+ setup.has_key?(:no_follow).should == false
198
+ setup[:headers].should == @test.headers
199
+ end
200
+
201
+ it 'sets redirection following options' do
202
+ @test.follows = false
203
+ setup = @test.setup_options({})
204
+
205
+ setup[:no_follow].should == true
206
+ end
207
+
208
+ it 'sets parameter body options' do
209
+ params = {:user => "me", :message => "Hello"}
210
+ setup_get = @test.setup_options(params)
211
+
212
+ setup_get.has_key?(:query).should == true
213
+ setup_get[:query] == params
214
+
215
+ @test.via = :post
216
+ setup_post = @test.setup_options(params)
217
+
218
+ setup_post.has_key?(:query).should == false
219
+ setup_post.has_key?(:body).should == true
220
+ setup_post[:body].should == params
221
+ end
222
+
223
+ it 'sets up authentication options' do
224
+ @test.authenticates = true
225
+ basic_auth = {:username => "mwunsch", :password => "secret123"}
226
+ oauth_consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
227
+ oauth_token = OAuth::AccessToken.new(oauth_consumer, "token", "secret")
228
+ setup_basic = @test.setup_options({}, basic_auth)
229
+ setup_oauth = @test.setup_options({}, oauth_token)
230
+
231
+ setup_basic[:basic_auth].should == basic_auth
232
+ setup_oauth[:oauth].should == oauth_token
233
+ end
234
+
235
+ it 'builds a Request' do
236
+ @test.authenticates = true
237
+ @test.requires = [:id]
238
+ params = {:user => "me", :message => "Hello", :bar => "foo"}
239
+ defaults = {:id => 1234, :foo => "bar"}
240
+ basic_auth = {:username => "mwunsch", :password => "secret123"}
241
+
242
+ request = @test.build!(params, defaults, basic_auth)
243
+
244
+ request.class.should == Weary::Request
245
+ request.method.should == :get
246
+ request.options[:basic_auth].should == basic_auth
247
+ end
75
248
  end
76
249
 
77
- it 'should only allow specified http methods' do
78
- @test.via = "Post"
79
- @test.via.should == :post
80
- @test.via = :foobar
81
- @test.via.should == :get
82
- end
83
-
84
- it 'format should be a symbol' do
85
- @test.format = "xml"
86
- @test.format.class.should == Symbol
250
+ it 'can convert to a hash' do
251
+ @test.to_hash.has_key?(:test).should == true
87
252
  end
88
253
 
89
- it 'format should be an allowed format' do
90
- @test.format = 'text/json'
91
- @test.format.should == :json
92
- lambda { @test.format = :foobar }.should raise_error
93
- end
94
254
 
95
- it 'should be able to set Headers' do
96
- @test.headers = {'Content-Type' => 'text/html'}
97
- @test.headers.should == {'Content-Type' => 'text/html'}
98
- end
99
255
 
100
256
  end
@@ -25,49 +25,45 @@ describe Weary::Response do
25
25
  @test.success?.should == true
26
26
  end
27
27
 
28
+ # replace with better mocking method
28
29
  it 'should parse JSON' do
29
30
  test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/json'}, get_fixture("vimeo.json")), :get)
30
31
  test.parse[0]["title"].should == "\"The Noises Rest\""
31
32
  end
32
33
 
34
+ # replace with better mocking method
33
35
  it 'should parse XML' do
34
36
  test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/xml'}, get_fixture("twitter.xml")), :get)
35
37
  test.parse.class.should == Hash
36
38
  test.parse['statuses'][0]['id'].should == "2186350626"
37
39
  end
38
40
 
41
+ # replace with better mocking method
39
42
  it 'should parse YAML' do
40
43
  test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/yaml'}, get_fixture("github.yml")), :get)
41
44
  test.parse.class.should == Hash
42
45
  test.parse["repository"][:name].should == "rails"
43
46
  end
44
47
 
45
- it 'should be able to search XML or HTML with Nokogiri' do
46
- test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/xml'}, get_fixture("twitter.xml")), :get)
47
- test.search("status:first > id").text.should == "2186350626"
48
- end
49
-
48
+ # replace with better mocking method
50
49
  it 'should use [] to parse the document' do
51
50
  test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/xml'}, get_fixture("twitter.xml")), :get).parse
52
51
  test['statuses'][0]['id'].should == "2186350626"
53
52
  end
54
53
 
55
- it 'should parse the document to a hash if we try to search a non-XMLish document' do
56
- test = Weary::Response.new(mock_response(:get, 200, {'content-type' => 'text/yaml'}, get_fixture("github.yml")), :get)
57
- test.search("foo bar").class.should == Hash
58
- test.search("foo bar")["repository"][:name].should == "rails"
59
- end
60
-
54
+ # replace with better mocking method
61
55
  it 'should raise an exception if there was a Server Error' do
62
56
  test = Weary::Response.new(mock_response(:get, 500), :get)
63
57
  lambda { test.parse }.should raise_error
64
58
  end
65
59
 
60
+ # replace with better mocking method
66
61
  it 'should raise an exception if there was a Client Error' do
67
62
  test = Weary::Response.new(mock_response(:get, 404), :get)
68
63
  lambda { test.parse }.should raise_error
69
64
  end
70
65
 
66
+ # replace with better mocking method
71
67
  it 'should follow HTTP redirects' do
72
68
  test = Weary::Response.new(mock_response(:get, 301), :get)
73
69
  test.redirected?.should == true
@@ -1,161 +1,4 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
3
  describe Weary do
4
- before do
5
- @test = Class.new
6
- @test.instance_eval { extend Weary }
7
- end
8
-
9
- describe 'Default Domain' do
10
- it 'should be set with a url' do
11
- @test.on_domain 'http://twitter.com/'
12
- @test.domain.should == 'http://twitter.com/'
13
- end
14
-
15
- it 'should raise an exception when a url is not present' do
16
- lambda { @test.on_domain("foobar") }.should raise_error
17
- end
18
-
19
- it 'should only take the first url given' do
20
- @test.on_domain("with http://google.com/ and http://yahoo.com/")
21
- @test.domain.should == "http://google.com/"
22
- end
23
- end
24
-
25
- describe 'Default Format' do
26
- it 'can be set' do
27
- @test.as_format(:xml)
28
- @test.instance_variable_get(:@default_format).should == :xml
29
- end
30
-
31
- it 'should be a recognized format' do
32
- @test.as_format('text/yaml')
33
- @test.on_domain('http://foobar.com/')
34
- @test.get('foobar').format.should == :yaml
35
- end
36
- end
37
-
38
- describe 'Default URL Pattern' do
39
- it 'can be set' do
40
- @test.construct_url("<domain><resource>.<format>")
41
- @test.instance_variable_defined?(:@url_pattern).should == true
42
- end
43
-
44
- it 'should be a string' do
45
- @test.construct_url(123)
46
- @test.instance_variable_get(:@url_pattern).class.should == String
47
- end
48
- end
49
-
50
- describe "Basic Authentication Credentials" do
51
- # i want to change this to be more specific about it being
52
- # basic authentication
53
- it "should accept a username and password" do
54
- @test.authenticates_with("foo","bar")
55
- @test.instance_variable_get(:@username).should == "foo"
56
- @test.instance_variable_get(:@password).should == "bar"
57
- end
58
- end
59
-
60
- describe "OAuth" do
61
- before do
62
- consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
63
- @token = OAuth::AccessToken.new(consumer, "token", "secret")
64
- @test.oauth @token
65
- end
66
-
67
- it "should accept an OAuth Access Token" do
68
- @test.instance_variable_get(:@oauth).should == @token
69
- lambda { @test.oauth "foobar" }.should raise_error
70
- end
71
- it "should notify the Resource that this is using OAuth" do
72
- @test.domain "http://foo.bar"
73
- r = @test.declare("show")
74
- r.oauth?.should == true
75
- r.access_token.should == @token
76
- end
77
- it "should be able to handle tokens set within the resource intelligently" do
78
- test = Class.new
79
- test.instance_eval { extend Weary }
80
- test.domain "http://foo.bar"
81
- r = test.declare("show")
82
- r.oauth?.should == false
83
- r.access_token.should == nil
84
- r.oauth = true
85
- lambda { test.send(:form_resource, r) }.should raise_error
86
- r.access_token = @token
87
- r.access_token.should == @token
88
- test.send(:form_resource, r)[:show][:oauth].should == true
89
- test.send(:form_resource, r)[:show][:access_token].should == @token
90
- end
91
- end
92
-
93
- describe "Set Headers" do
94
- it "should be a hash of values to pass in the Request head" do
95
- @test.on_domain "http://foo.bar"
96
- @test.set_headers "Content-Type" => "text/html"
97
- r = @test.get "resource"
98
- r.headers.should == {"Content-Type"=>'text/html'}
99
- end
100
- end
101
-
102
- describe "Common Request Paramaters" do
103
- it "should define with params that every resource inherits" do
104
- @test.on_domain "http://foo.bar"
105
- @test.always_with [:login, :token]
106
- r = @test.get "resource"
107
- r.with.should == [:login, :token]
108
- r.requires = [:foobar]
109
- r.with.should == [:login, :token, :foobar]
110
- end
111
-
112
- it "should be able to be a hash" do
113
- @test.on_domain "http://foo.bar"
114
- @test.always_with :foo => "Foo", :bar => "Bar"
115
- r = @test.get "resource"
116
- r.with.should == {:foo => "Foo", :bar => "Bar"}
117
- r.requires = [:foobar]
118
- r.with.should == {:foo => "Foo", :bar => "Bar", :foobar => nil}
119
- end
120
- end
121
-
122
- describe 'Resource Declaration' do
123
- before do
124
- @test.on_domain "http://foobar.com/"
125
- end
126
-
127
- it 'should have a common method for building requests' do
128
- block = Proc.new { |r| r.via = :post; r.url = "http://bar.foo/name.json"}
129
- test = @test.build_resource('name',:get,block)
130
- test.class.should == Weary::Resource
131
- test.url.should == "http://bar.foo/name.json"
132
- test.via.should == :post
133
- end
134
-
135
- it 'should add a new resource' do
136
- @test.get "resource"
137
- @test.resources[0].has_key?(:resource).should == true
138
- end
139
-
140
- it 'should instantiate a Resource object' do
141
- @test.get("resource").class.should == Weary::Resource
142
- end
143
-
144
- it 'should craft code to be evaluated' do
145
- r = @test.get("resource")
146
- @test.send(:craft_methods, r).class.should == String
147
- end
148
-
149
- it 'should define new instance methods' do
150
- @test.get("resource")
151
- @test.public_method_defined?(:resource).should == true
152
- end
153
-
154
- it 'should use sensible default formats' do
155
- @test.get("resource").format.should == :json
156
- @test.as_format(:xml)
157
- @test.post("foo").format.should == :xml
158
- end
159
- end
160
-
161
4
  end