sinatra-rest-base 1.0.3 → 1.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75d50b09e35bd0c09b5384a9f6c053e8b8b7c22d
4
- data.tar.gz: 76e0bcb886023c0e11e1bb0a362d13cc5862f566
3
+ metadata.gz: 24af1cf8be8da4b7423bf9e76473d2848e365f65
4
+ data.tar.gz: aba24043540d4645d0af30268dda2b57df333af9
5
5
  SHA512:
6
- metadata.gz: 12e058363080135672533f3bfe0d74034d67adca7993bba787c45d8201cdabf8983d0e8ab85e784071803a865df1f93463059e581ae0e4109971229828b92b0e
7
- data.tar.gz: 74f257be8746f15e4b43105c87def2243afc46f056055c3d984c3964fd37211d9c079801a639d97e4d48142c444ef0416b9b95358400b986ebe4fde1c229da31
6
+ metadata.gz: 23e50403fb3aceec1bac42a754e9c7a313634c5c81ced7ce9c7d4a1cdbebc47dbb55b58da2221a47bbfea91ebb4e573a30d3399bd9b38fa4bfd02eea72018b2f
7
+ data.tar.gz: 2bd4bb2135d2deaa0ba284529235d3e7e330b833bd27e7c01f52d361b9e5cffcbb4dde9bd8f3e9cccd5a7db586a3cf577e8adf2200a646e610127e0a9a429c95
@@ -26,14 +26,18 @@ module RestBase
26
26
  def set_request_body
27
27
  read_body = request.body.read
28
28
 
29
- if read_body.empty?
30
- @request_body = {}
31
- elsif request.media_type.downcase == 'text/xml'
32
- @request_body = Hash.from_xml(read_body)
33
- @request_body = Hash[@request_body.map{ |k, v| [k.to_sym, v] }]
34
- @request_body = @request_body[:request]
35
- else
36
- @request_body = JSON.parse(read_body, :symbolize_names => true)
29
+ begin
30
+ if read_body.empty?
31
+ @request_body = {}
32
+ elsif request.media_type.downcase == 'text/xml'
33
+ @request_body = Hash.from_xml(read_body)
34
+ @request_body = Hash[@request_body.map{ |k, v| [k.to_sym, v] }]
35
+ @request_body = @request_body[:request]
36
+ else
37
+ @request_body = JSON.parse(read_body, :symbolize_names => true)
38
+ end
39
+ rescue
40
+ raise StandardError.new("Invalid Request Body")
37
41
  end
38
42
  end
39
43
 
@@ -1,3 +1,3 @@
1
1
  module RestBase
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -13,6 +13,10 @@ module RestBase
13
13
  get '/unauthorized', :version => 1 do
14
14
  error 401
15
15
  end
16
+
17
+ get '/notfound', :version => 1 do
18
+ error 404
19
+ end
16
20
 
17
21
  get '/internal_error', :version => 1 do
18
22
  error 500
@@ -181,6 +181,16 @@ describe RestBase::Application do
181
181
 
182
182
  expect(last_response.body).to eq(@expected_hash.to_json)
183
183
  end
184
+
185
+ it "should fail with bad json" do
186
+ post '/', "}", @headers
187
+
188
+ @expected_hash.delete(:result)
189
+ @expected_hash[:errors] = ["Invalid Request Body"]
190
+
191
+ expect(last_response.status).to eq(500)
192
+ expect(last_response.body).to eq(@expected_hash.to_json)
193
+ end
184
194
  end
185
195
 
186
196
  describe :xml do
@@ -222,5 +232,18 @@ describe RestBase::Application do
222
232
 
223
233
  expect(last_response.body).to eq(@expected_hash.to_xml(:root => :response))
224
234
  end
235
+
236
+ it "should fail on bad xml" do
237
+ @headers['CONTENT_TYPE'] = 'text/xml'
238
+ @headers['HTTP_ACCEPT'] = 'text/xml'
239
+
240
+ @expected_hash.delete(:result)
241
+ @expected_hash[:errors] = ["Invalid Request Body"]
242
+
243
+ post '/', "<BadXML>", @headers
244
+
245
+ expect(last_response.status).to eq(500)
246
+ expect(last_response.body).to eq(@expected_hash.to_xml(:root => :response))
247
+ end
225
248
  end
226
249
  end
@@ -33,7 +33,16 @@ describe RestBase::Application do
33
33
  it "should provide endpoint not found error" do
34
34
  @expected_body[:errors] << "Endpoint not found"
35
35
 
36
- get '/not_found', {}, @headers
36
+ get '/idontexist', {}, @headers
37
+
38
+ expect(last_response.status).to eq(404)
39
+ expect(last_response.body).to eq(@expected_body.to_json)
40
+ end
41
+
42
+ it "should provide error 404" do
43
+ @expected_body[:errors] << "Endpoint not found"
44
+
45
+ get '/notfound', {}, @headers
37
46
 
38
47
  expect(last_response.status).to eq(404)
39
48
  expect(last_response.body).to eq(@expected_body.to_json)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rest-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - bmills
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-21 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json