restfulness 0.3.2 → 0.3.3

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.
@@ -23,50 +23,55 @@ describe Restfulness::Request do
23
23
  let :resource do
24
24
  RequestResource
25
25
  end
26
+
27
+ # Fake IO class, meant to mimic Puma::NullIO
28
+ class EmptyIO
29
+ def read(count = nil, buffer = nil); ""; end
30
+ end
26
31
 
27
32
 
28
33
  describe "#initialize" do
29
34
  it "should prepare basic objects" do
30
- obj.action.should be_nil
31
- obj.headers.should eql({})
32
- obj.body.should be_nil
35
+ expect(obj.action).to be_nil
36
+ expect(obj.headers).to eql({})
37
+ expect(obj.body).to be_nil
33
38
  end
34
39
  end
35
40
 
36
41
  describe "#uri=" do
37
42
  it "should convert URL into URI" do
38
43
  obj.uri = "https://example.com/project/12345"
39
- obj.uri.path.should eql("/project/12345")
44
+ expect(obj.uri.path).to eql("/project/12345")
40
45
  end
41
46
  end
42
47
 
43
48
  describe "#action" do
44
49
  it "should provide basic action" do
45
50
  obj.action = :get
46
- obj.action.should eql(:get)
51
+ expect(obj.action).to eql(:get)
47
52
  end
48
53
  end
49
54
 
50
55
  describe "#path" do
51
56
  it "should be nil if there is no route" do
52
- obj.stub(:route).and_return(nil)
53
- obj.path.should be_nil
57
+ allow(obj).to receive(:route).and_return(nil)
58
+ expect(obj.path).to be_nil
54
59
  end
55
60
 
56
61
  context "with route" do
57
62
  let :path do
58
63
  obj.uri = "https://example.com/project/12345"
59
64
  route = Restfulness::Route.new('project', resource)
60
- obj.stub(:route).and_return(route)
65
+ allow(obj).to receive(:route).and_return(route)
61
66
  obj.path
62
67
  end
63
68
 
64
69
  it "should build path" do
65
- path.to_s.should eql('/project/12345')
70
+ expect(path.to_s).to eql('/project/12345')
66
71
  end
67
72
 
68
73
  it "should re-use same path object" do
69
- path.object_id.should eql(obj.path.object_id)
74
+ expect(path.object_id).to eql(obj.path.object_id)
70
75
  end
71
76
  end
72
77
  end
@@ -75,57 +80,88 @@ describe Restfulness::Request do
75
80
  it "should ask the router for a route" do
76
81
  obj.uri = "https://example.com/project/12345"
77
82
  route = double(:Route)
78
- app.router.should_receive(:route_for).with(obj.uri.path).and_return(route)
79
- obj.route.should eql(route)
83
+ expect(app.router).to receive(:route_for).with(obj.uri.path).and_return(route)
84
+ expect(obj.route).to eql(route)
80
85
  end
81
86
  end
82
87
 
83
88
  describe "#query" do
84
89
  it "should parse uri with query" do
85
90
  obj.uri = "https://example.com/project/12345?foo=bar&test=23"
86
- obj.query.should be_a(HashWithIndifferentAccess)
87
- obj.query[:foo].should eql('bar')
88
- obj.query[:test].to_i.should eql(23)
91
+ expect(obj.query).to be_a(HashWithIndifferentAccess)
92
+ expect(obj.query[:foo]).to eql('bar')
93
+ expect(obj.query[:test].to_i).to eql(23)
89
94
  end
90
95
 
91
96
  it "should handle uri with empty query" do
92
97
  obj.uri = "https://example.com/project/12345"
93
- obj.query.should be_empty
98
+ expect(obj.query).to be_empty
94
99
  end
95
100
 
96
101
  it "should handle complex query items" do
97
102
  obj.uri = "https://example.com/project/12345?foo[]=bar&foo[]=bar2&hash[a]=b&hash[b]=c"
98
- obj.query[:foo].should eql(['bar', 'bar2'])
99
- obj.query[:hash].should be_a(HashWithIndifferentAccess)
100
- obj.query[:hash][:a].should eql('b')
103
+ expect(obj.query[:foo]).to eql(['bar', 'bar2'])
104
+ expect(obj.query[:hash]).to be_a(HashWithIndifferentAccess)
105
+ expect(obj.query[:hash][:a]).to eql('b')
101
106
  end
102
107
  end
103
108
 
104
109
  describe "#sanitized_query_string" do
105
110
  it "should be empty if no query" do
106
111
  obj.uri = "https://example.com/project/12345"
107
- obj.sanitized_query_string.should be_empty
112
+ expect(obj.sanitized_query_string).to be_empty
108
113
  end
109
114
  it "should filter out bad keys" do # See sanitizer tests for more
110
115
  obj.uri = "https://example.com/project/12345?foo=bar&password=safe"
111
- obj.sanitized_query_string.should match(/foo=bar/)
112
- obj.sanitized_query_string.should_not match(/password=safe/)
116
+ expect(obj.sanitized_query_string).to match(/foo=bar/)
117
+ expect(obj.sanitized_query_string).not_to match(/password=safe/)
118
+ end
119
+ end
120
+
121
+ describe "#content_type" do
122
+ it "should be nil if no content type" do
123
+ expect(obj.content_type).to be_nil
124
+ end
125
+
126
+ it "should provide a content type object for the request" do
127
+ obj.headers[:content_type] = "application/json; charset=utf-8"
128
+ expect(obj.content_type).to be_a(Restfulness::Headers::MediaType)
129
+ expect(obj.content_type.json?).to be true
130
+ end
131
+
132
+ it "should cache content_type object" do
133
+ obj.headers[:content_type] = "application/json"
134
+ obj.content_type.freeze
135
+ expect(obj.content_type.frozen?).to be true
136
+ end
137
+ end
138
+
139
+ describe "#accept" do
140
+ it "should be nil if no accept header provided" do
141
+ expect(obj.accept).to be_nil
142
+ end
143
+ it "should provide a accept object for the request" do
144
+ obj.headers[:accept] = "application/json; version=3"
145
+ expect(obj.accept).to be_a(Restfulness::Headers::Accept)
146
+ expect(obj.accept.json?).to be true
147
+ expect(obj.accept.version).to eql("3")
113
148
  end
114
149
  end
115
150
 
116
151
  describe "#params" do
117
152
  it "should not return anything for empty body" do
118
- obj.stub(:body).and_return(nil)
119
- obj.params.should be_empty
153
+ allow(obj).to receive(:body).and_return(nil)
154
+ expect(obj.params).to be_empty
120
155
  end
121
156
 
122
157
  it "should raise 400 bad request for invalid json body" do
123
158
  obj.headers[:content_type] = "application/json; charset=utf-8"
124
- obj.stub(:body).and_return("invalidjson!")
159
+ allow(obj).to receive(:body).and_return('{"data":"invalid}')
125
160
  expect {
126
161
  obj.params
127
162
  }.to raise_error(Restfulness::HTTPException, "Bad Request"){ |exception|
128
163
  expect(exception.status).to eq 400
164
+ expect(exception.payload).to eq "Invalid JSON in request body"
129
165
  }
130
166
  end
131
167
 
@@ -150,78 +186,94 @@ describe Restfulness::Request do
150
186
  it "should decode a JSON body" do
151
187
  obj.headers[:content_type] = "application/json"
152
188
  obj.body = "{\"foo\":\"bar\"}"
153
- obj.params['foo'].should eql('bar')
189
+ expect(obj.params['foo']).to eql('bar')
154
190
  end
155
191
 
156
192
  it "should decode a WWW Form body" do
157
193
  obj.headers[:content_type] = "application/x-www-form-urlencoded"
158
194
  obj.body = "grant_type=password&username=johndoe&password=A3ddj3w"
159
- obj.params['grant_type'].should eql('password')
160
- obj.params['username'].should eql('johndoe')
195
+ expect(obj.params['grant_type']).to eql('password')
196
+ expect(obj.params['username']).to eql('johndoe')
161
197
  end
162
198
 
163
199
  it "should deal with empty WWW Form body" do
164
200
  obj.headers[:content_type] = "application/x-www-form-urlencoded"
165
201
  obj.body = ""
166
- obj.params.should be_empty
202
+ expect(obj.params).to be_empty
167
203
  end
168
204
 
169
205
  it "should deal with a StringIO WWW form body" do
170
206
  obj.headers[:content_type] = "application/x-www-form-urlencoded"
171
207
  obj.body = StringIO.new("grant_type=password&username=johndoe&password=A3ddj3w")
172
- obj.params['grant_type'].should eql('password')
173
- obj.params['username'].should eql('johndoe')
208
+ expect(obj.params['grant_type']).to eql('password')
209
+ expect(obj.params['username']).to eql('johndoe')
210
+ end
211
+
212
+ it "should deal with a Tempfile WWW form body" do
213
+ obj.headers[:content_type] = "application/x-www-form-urlencoded"
214
+ file = Tempfile.new("params")
215
+ file.write("grant_type=password&username=johndoe&password=A3ddj3w")
216
+ file.rewind
217
+ obj.body = file
218
+ expect(obj.params['grant_type']).to eql('password')
219
+ expect(obj.params['username']).to eql('johndoe')
174
220
  end
175
221
 
176
222
  it "should deal with empty JSON String body" do
177
223
  obj.headers[:content_type] = "application/json"
178
224
  obj.body = ""
179
- obj.params.should be_empty
225
+ expect(obj.params).to be_empty
180
226
  end
181
227
 
182
228
  it "should deal with empty JSON StringIO body" do
183
229
  obj.headers[:content_type] = "application/json"
184
230
  obj.body = StringIO.new("")
185
- obj.params.should be_empty
231
+ expect(obj.params).to be_empty
232
+ end
233
+
234
+ it "should handle some crappy IO object" do
235
+ obj.body = EmptyIO.new()
236
+ expect(obj.body).to receive(:read)
237
+ expect(obj.params).to be_empty
186
238
  end
187
239
  end
188
240
 
189
241
  describe "#sanitized_params" do
190
242
  it "should provide nil if the params hash has not been used" do
191
- obj.stub(:body).and_return(nil)
192
- obj.sanitized_params.should be_nil
243
+ allow(obj).to receive(:body).and_return(nil)
244
+ expect(obj.sanitized_params).to be_nil
193
245
  end
194
246
  it "should provide santized params if params have been used" do
195
247
  obj.headers[:content_type] = "application/json"
196
248
  obj.body = "{\"foo\":\"bar\",\"password\":\"safe\"}"
197
- obj.params['password'].should eql('safe')
198
- obj.sanitized_params['foo'].should eql('bar')
199
- obj.sanitized_params['password'].should_not be_blank
200
- obj.sanitized_params['password'].should_not eql('safe')
249
+ expect(obj.params['password']).to eql('safe')
250
+ expect(obj.sanitized_params['foo']).to eql('bar')
251
+ expect(obj.sanitized_params['password']).not_to be_blank
252
+ expect(obj.sanitized_params['password']).not_to eql('safe')
201
253
  end
202
254
  end
203
255
 
204
256
  describe "#http_accept_language" do
205
257
  it "should provide an instance of Parser" do
206
- obj.http_accept_language.should be_a(HttpAcceptLanguage::Parser)
258
+ expect(obj.http_accept_language).to be_a(HttpAcceptLanguage::Parser)
207
259
  end
208
260
  it "should use the accept_language header" do
209
261
  header = "en-us,en-gb;q=0.8,en"
210
262
  obj.headers[:accept_language] = header
211
- obj.http_accept_language.header.should eql(header)
263
+ expect(obj.http_accept_language.header).to eql(header)
212
264
  end
213
265
  end
214
266
 
215
267
  describe "method helpers" do
216
268
  it "should respond to method questions" do
217
269
  [:get?, :post?, :put?, :delete?, :head?, :options?].each do |q|
218
- obj.should respond_to(q)
270
+ expect(obj).to respond_to(q)
219
271
  end
220
272
  end
221
273
  it "should suggest type of method" do
222
274
  obj.action = :get
223
- obj.get?.should be_true
224
- obj.post?.should be_false
275
+ expect(obj.get?).to be true
276
+ expect(obj.post?).to be false
225
277
  end
226
278
  end
227
279
 
@@ -10,26 +10,26 @@ describe Restfulness::Requests::AuthorizationHeader do
10
10
 
11
11
  it "should accept standard header" do
12
12
  obj = klass.new("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
13
- obj.schema.should eql("Basic")
14
- obj.params.should eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
13
+ expect(obj.schema).to eql("Basic")
14
+ expect(obj.params).to eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
15
15
  end
16
16
 
17
17
  it "should accept non-standard schema" do
18
18
  obj = klass.new("bAsic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
19
- obj.schema.should eql("Basic")
20
- obj.params.should eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
19
+ expect(obj.schema).to eql("Basic")
20
+ expect(obj.params).to eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
21
21
  end
22
22
 
23
23
  it "should ignore any whitespace" do
24
24
  obj = klass.new(" Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== ")
25
- obj.schema.should eql("Basic")
26
- obj.params.should eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
25
+ expect(obj.schema).to eql("Basic")
26
+ expect(obj.params).to eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
27
27
  end
28
28
 
29
29
  it "should append additional stuff" do
30
30
  obj = klass.new("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== foooo")
31
- obj.schema.should eql("Basic")
32
- obj.params.should eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ== foooo")
31
+ expect(obj.schema).to eql("Basic")
32
+ expect(obj.params).to eql("QWxhZGRpbjpvcGVuIHNlc2FtZQ== foooo")
33
33
  end
34
34
 
35
35
  end
@@ -17,14 +17,14 @@ describe Restfulness::Requests::Authorization do
17
17
 
18
18
  it "should be nil if no authorization header resent" do
19
19
  auth = request.authorization
20
- auth.should be_nil
20
+ expect(auth).to be_nil
21
21
  end
22
22
 
23
23
  it "should build new authorization header when present" do
24
24
  request.headers[:authorization] = "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
25
25
  auth = request.authorization
26
- auth.should be_a(Restfulness::Requests::AuthorizationHeader)
27
- auth.schema.should eql("Basic")
26
+ expect(auth).to be_a(Restfulness::Requests::AuthorizationHeader)
27
+ expect(auth.schema).to eql("Basic")
28
28
  end
29
29
 
30
30
  end
@@ -37,8 +37,8 @@ describe Restfulness::Resource do
37
37
  end
38
38
  it "should assign request and response" do
39
39
  obj = resource.new(request, response)
40
- obj.request.should eql(request)
41
- obj.response.should eql(response)
40
+ expect(obj.request).to eql(request)
41
+ expect(obj.response).to eql(response)
42
42
  end
43
43
  end
44
44
 
@@ -49,10 +49,10 @@ describe Restfulness::Resource do
49
49
 
50
50
  it "should return list of supported methods" do
51
51
  obj = resource.new(request, response)
52
- obj.options.should be_nil
52
+ expect(obj.options).to be_nil
53
53
  acts = response.headers['Allow'].split(/, /)
54
54
  ['GET', 'POST', 'OPTIONS'].each do |act|
55
- acts.should include(act)
55
+ expect(acts).to include(act)
56
56
  end
57
57
  end
58
58
  end
@@ -64,8 +64,8 @@ describe Restfulness::Resource do
64
64
  it "should perform action" do
65
65
  request.action = :get
66
66
  obj = resource.new(request, response)
67
- obj.should_receive(:get).and_return('res')
68
- obj.call.should eql('res')
67
+ expect(obj).to receive(:get).and_return('res')
68
+ expect(obj.call).to eql('res')
69
69
  end
70
70
 
71
71
  end
@@ -78,13 +78,13 @@ describe Restfulness::Resource do
78
78
  it "should be true on valid method" do
79
79
  request.action = :get
80
80
  obj = resource.new(request, response)
81
- obj.method_allowed?.should be_true
81
+ expect(obj.method_allowed?).to be true
82
82
  end
83
83
 
84
84
  it "should be false on invalid method" do
85
85
  request.action = :put
86
86
  obj = resource.new(request, response)
87
- obj.method_allowed?.should be_false
87
+ expect(obj.method_allowed?).to be false
88
88
  end
89
89
  end
90
90
 
@@ -99,14 +99,14 @@ describe Restfulness::Resource do
99
99
  end
100
100
 
101
101
  it "should all be true for questions" do
102
- obj.exists?.should be_true
103
- obj.authorized?.should be_true
104
- obj.allowed?.should be_true
102
+ expect(obj.exists?).to be true
103
+ expect(obj.authorized?).to be true
104
+ expect(obj.allowed?).to be true
105
105
  end
106
106
 
107
107
  it "should be nil for values" do
108
- obj.last_modified.should be_nil
109
- obj.etag.should be_nil
108
+ expect(obj.last_modified).to be_nil
109
+ expect(obj.etag).to be_nil
110
110
  end
111
111
  end
112
112
 
@@ -131,12 +131,12 @@ describe Restfulness::Resource do
131
131
  end
132
132
 
133
133
  it "should try to set the locale" do
134
- obj.should_receive(:set_locale)
134
+ expect(obj).to receive(:set_locale)
135
135
  obj.check_callbacks
136
136
  end
137
137
 
138
138
  it "should raise error on invalid method" do
139
- obj.stub(:method_allowed?).and_return(false)
139
+ allow(obj).to receive(:method_allowed?).and_return(false)
140
140
  expect {
141
141
  obj.check_callbacks
142
142
  }.to raise_error(Restfulness::HTTPException, "Method Not Allowed")
@@ -145,7 +145,7 @@ describe Restfulness::Resource do
145
145
  [:head, :get, :patch, :delete].each do |action|
146
146
  it "should raise error when not exists for #{action.to_s.upcase}" do
147
147
  request.action = action
148
- obj.stub(:exists?).and_return(false)
148
+ allow(obj).to receive(:exists?).and_return(false)
149
149
  expect {
150
150
  obj.check_callbacks
151
151
  }.to raise_error(Restfulness::HTTPException, "Resource Not Found")
@@ -155,20 +155,20 @@ describe Restfulness::Resource do
155
155
  [:put, :post].each do |action|
156
156
  it "should not check exists? for #{action.to_s.upcase}" do
157
157
  obj.request.action = action
158
- obj.should_not_receive(:exists?)
158
+ expect(obj).not_to receive(:exists?)
159
159
  obj.check_callbacks
160
160
  end
161
161
  end
162
162
 
163
163
  it "should raise error when not authorized" do
164
- obj.stub(:authorized?).and_return(false)
164
+ allow(obj).to receive(:authorized?).and_return(false)
165
165
  expect {
166
166
  obj.check_callbacks
167
167
  }.to raise_error(Restfulness::HTTPException, "Unauthorized")
168
168
  end
169
169
 
170
170
  it "should raise error when not allowed" do
171
- obj.stub(:allowed?).and_return(false)
171
+ allow(obj).to receive(:allowed?).and_return(false)
172
172
  expect {
173
173
  obj.check_callbacks
174
174
  }.to raise_error(Restfulness::HTTPException, "Forbidden")
@@ -176,7 +176,7 @@ describe Restfulness::Resource do
176
176
 
177
177
  describe "with etag" do
178
178
  it "should raise error when equal" do
179
- obj.stub(:etag).and_return('sometag')
179
+ allow(obj).to receive(:etag).and_return('sometag')
180
180
  request.headers[:if_none_match] = 'sometag'
181
181
  expect {
182
182
  obj.check_callbacks
@@ -184,7 +184,7 @@ describe Restfulness::Resource do
184
184
  end
185
185
 
186
186
  it "should continue if not equal" do
187
- obj.stub(:etag).and_return('sometag')
187
+ allow(obj).to receive(:etag).and_return('sometag')
188
188
  request.headers[:if_none_match] = 'someoldtag'
189
189
  expect {
190
190
  obj.check_callbacks
@@ -192,7 +192,7 @@ describe Restfulness::Resource do
192
192
  end
193
193
 
194
194
  it "should not be called unless action is :get or :head" do
195
- obj.should_not_receive(:etag)
195
+ expect(obj).not_to receive(:etag)
196
196
  request.headers[:if_none_match] = 'sometag'
197
197
  [:post, :put, :delete].each do |action|
198
198
  request.action = action
@@ -204,7 +204,7 @@ describe Restfulness::Resource do
204
204
  describe "with if modified" do
205
205
  it "should raise error when equal" do
206
206
  time = Time.now
207
- obj.stub(:last_modified).and_return(time)
207
+ allow(obj).to receive(:last_modified).and_return(time)
208
208
  request.headers[:if_modified_since] = time.to_s
209
209
  expect {
210
210
  obj.check_callbacks
@@ -213,7 +213,7 @@ describe Restfulness::Resource do
213
213
 
214
214
  it "should continue if not equal" do
215
215
  time = Time.now
216
- obj.stub(:last_modified).and_return(time)
216
+ allow(obj).to receive(:last_modified).and_return(time)
217
217
  request.headers[:if_modified_since] = (time - 60).to_s
218
218
  expect {
219
219
  obj.check_callbacks
@@ -221,7 +221,7 @@ describe Restfulness::Resource do
221
221
  end
222
222
 
223
223
  it "should not be called unless action is :get or :head" do
224
- obj.should_not_receive(:last_modified)
224
+ expect(obj).not_to receive(:last_modified)
225
225
  request.headers[:if_modified_since] = 'somedate'
226
226
  [:post, :put, :delete].each do |action|
227
227
  request.action = action
@@ -251,16 +251,16 @@ describe Restfulness::Resource do
251
251
  describe "#locale" do
252
252
  it "should return acceptable locale" do
253
253
  I18n.available_locales = ['es']
254
- obj.send(:locale).should eql(:es)
254
+ expect(obj.send(:locale)).to eql(:es)
255
255
  end
256
256
  end
257
257
 
258
258
  describe "#set_locale" do
259
259
  it "should set the global locale value" do
260
260
  I18n.available_locales = ['en', 'es']
261
- I18n.locale.should_not eql(:es)
261
+ expect(I18n.locale).not_to eql(:es)
262
262
  obj.send(:set_locale)
263
- I18n.locale.should eql(:es)
263
+ expect(I18n.locale).to eql(:es)
264
264
  end
265
265
  end
266
266