rack-json_schema 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79d209276140e0736a40756b6505b4b8a6eeec02
4
- data.tar.gz: e6f68298e2836bd614ceb53bf2d396282cb28148
3
+ metadata.gz: e64c6e28164dfe606ffec931969f51da0d6e66a5
4
+ data.tar.gz: 7e19454007e9b43b4f3915bdf127e5276bd6fdb2
5
5
  SHA512:
6
- metadata.gz: e88d4285760b99907f5eeb0f14eeea65ab03e0b5f8e7136339139188918955e62b61b309530cfbca52c5848086c504685458552f0b24120340b03d74da218346
7
- data.tar.gz: 417d7ef7f5029194670d29dc94ca3f3a7531b735f60c9363def5ee8874f1c1e07810ee3759a5e94d8f8d187f8a1343ec532da3ab3cfc438fb944b96c63217a7c
6
+ metadata.gz: 7e7e277bc22ef4457488e12b36336ae96369ee9feeaf0c71b80291c1ec882c5941b3956435eb40bc2325c6841ae1424552379f55cfa9036dcbce21d5946eab4c
7
+ data.tar.gz: 1368238b6dc3f699709e591249d5fcdb8566ebff96b003707ba3b8effd7fda08e7be5103947f700c5f9526ea7f20c8f226ad6b64c142c1d0c0a4a3ea6c84dfda
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 1.1.5
2
+ * Check Content-Type on response validation
3
+
1
4
  ## 1.1.4
2
5
  * Skip validation of non-json request
3
6
 
@@ -31,7 +31,7 @@ module Rack
31
31
  # Raises an error if any error detected, skipping validation for non-defined link
32
32
  # @raise [Rack::JsonSchema::ResponseValidation::InvalidResponse]
33
33
  def call
34
- if !has_error_status? && has_link_for_current_action?
34
+ if !has_error_status? && has_link_for_current_action? && has_link_of_media_type_json?
35
35
  case
36
36
  when !has_json_content_type?
37
37
  raise InvalidResponseContentType
@@ -41,9 +41,14 @@ module Rack
41
41
  end
42
42
  end
43
43
 
44
+ # @return [true, false] True if Link mediaType is for JSON
45
+ def has_link_of_media_type_json?
46
+ mime_type_json?(link.media_type)
47
+ end
48
+
44
49
  # @return [true, false] True if response Content-Type is for JSON
45
50
  def has_json_content_type?
46
- %r<\Aapplication/.*json> === headers["Content-Type"]
51
+ mime_type_json?(mime_type)
47
52
  end
48
53
 
49
54
  # @return [true, false] True if given data is valid to the JSON schema
@@ -93,6 +98,22 @@ module Rack
93
98
  def has_error_status?
94
99
  response_status >= 400
95
100
  end
101
+
102
+ # @return [String, nil] Response MIME Type specified in Content-Type header field
103
+ # @example
104
+ # mime_type #=> "application/json"
105
+ def mime_type
106
+ headers["Content-Type"].split(";").first if headers["Content-Type"]
107
+ end
108
+
109
+ # @return [true, false] return true if mime type is for JSON
110
+ # @example
111
+ # "application/json" #=> true
112
+ # "application/calendar+json" #=> true
113
+ # "application/vnd.myapp.custom-json" #=> false
114
+ def mime_type_json?(value)
115
+ %r<\Aapplication/(?:.*\+)?json> === value
116
+ end
96
117
  end
97
118
 
98
119
  # Base error class for Rack::JsonSchema::ResponseValidation
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module JsonSchema
3
- VERSION = "1.1.4"
3
+ VERSION = "1.1.5"
4
4
  end
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "erubis"
21
21
  spec.add_dependency "jdoc", ">= 0.1.4"
22
- spec.add_dependency "json_schema"
22
+ spec.add_dependency "json_schema", "~> 0.2"
23
23
  spec.add_dependency "rack"
24
24
  spec.add_development_dependency "bundler", "~> 1.5"
25
25
  spec.add_development_dependency "pry"
@@ -68,6 +68,14 @@
68
68
  "rel": "self",
69
69
  "title": "Info"
70
70
  },
71
+ {
72
+ "description": "Info for existing apps in text format",
73
+ "href": "/apps/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fid)}/text",
74
+ "method": "GET",
75
+ "rel": "self",
76
+ "title": "Info",
77
+ "mediaType": "text/plain"
78
+ },
71
79
  {
72
80
  "description": "List existing apps.",
73
81
  "href": "/apps",
@@ -202,12 +202,22 @@ describe Rack::JsonSchema do
202
202
  { "Content-Type" => "text/plain" }
203
203
  end
204
204
 
205
- it "returns invalid_response_content_type error" do
206
- should == 500
207
- response.body.should be_json_as(
208
- id: "invalid_response_content_type",
209
- message: "Response Content-Type wasn't for JSON",
210
- )
205
+ context "when mediaType is specified as text/plain" do
206
+ let(:path) do
207
+ "/apps/#{response_data[:id]}/text"
208
+ end
209
+
210
+ it { should == 200 }
211
+ end
212
+
213
+ context "when mediaType is not specified" do
214
+ it "returns invalid_response_content_type error" do
215
+ should == 500
216
+ response.body.should be_json_as(
217
+ id: "invalid_response_content_type",
218
+ message: "Response Content-Type wasn't for JSON",
219
+ )
220
+ end
211
221
  end
212
222
  end
213
223
 
@@ -238,5 +248,13 @@ describe Rack::JsonSchema do
238
248
  )
239
249
  end
240
250
  end
251
+
252
+ context "with response content type for JSON" do
253
+ let(:response_headers) do
254
+ { "Content-Type" => "application/json; charset=utf-8" }
255
+ end
256
+
257
+ it { should == 200 }
258
+ end
241
259
  end
242
260
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: json_schema
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '0.2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '0.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
57
  requirement: !ruby/object:Gem::Requirement