rack-json_schema 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,221 @@
1
+ require "spec_helper"
2
+
3
+ describe Rack::JsonSchema do
4
+ include Rack::Test::Methods
5
+
6
+ let(:app) do
7
+ local_schema = schema
8
+ local_response_body = response_body
9
+ local_response_headers = response_headers
10
+ Rack::Builder.app do
11
+ use Rack::JsonSchema::ErrorHandler
12
+ use Rack::JsonSchema::RequestValidation, schema: local_schema
13
+ use Rack::JsonSchema::ResponseValidation, schema: local_schema
14
+ run ->(env) do
15
+ [200, local_response_headers, [local_response_body]]
16
+ end
17
+ end
18
+ end
19
+
20
+ let(:response_headers) do
21
+ { "Content-Type" => "application/json" }
22
+ end
23
+
24
+ let(:response_body) do
25
+ if verb == :get
26
+ [response_data].to_json
27
+ else
28
+ response_data.to_json
29
+ end
30
+ end
31
+
32
+ let(:response_data) do
33
+ {
34
+ id: "01234567-89ab-cdef-0123-456789abcdef",
35
+ name: "example",
36
+ }
37
+ end
38
+
39
+ let(:schema) do
40
+ str = File.read(schema_path)
41
+ JSON.parse(str)
42
+ end
43
+
44
+ let(:schema_path) do
45
+ File.expand_path("../../fixtures/schema.json", __FILE__)
46
+ end
47
+
48
+ let(:response) do
49
+ last_response
50
+ end
51
+
52
+ let(:env) do
53
+ {}
54
+ end
55
+
56
+ let(:params) do
57
+ {}
58
+ end
59
+
60
+ subject do
61
+ send(verb, path, params, env)
62
+ response.status
63
+ end
64
+
65
+ shared_context "with valid POST request", :with_valid_post_request do
66
+ before do
67
+ env["CONTENT_TYPE"] = "application/json"
68
+ end
69
+
70
+ let(:verb) do
71
+ :post
72
+ end
73
+
74
+ let(:path) do
75
+ "/apps"
76
+ end
77
+
78
+ let(:params) do
79
+ { name: "abcd" }.to_json
80
+ end
81
+ end
82
+
83
+ describe "RequestValidation" do
84
+ let(:verb) do
85
+ :get
86
+ end
87
+
88
+ let(:path) do
89
+ "/apps"
90
+ end
91
+
92
+ context "with defined route" do
93
+ it { should == 200 }
94
+ end
95
+
96
+ context "with undefined route" do
97
+ let(:path) do
98
+ "/undefined"
99
+ end
100
+
101
+ it "returns link_not_found error" do
102
+ should == 404
103
+ response.body.should be_json_as(
104
+ id: "link_not_found",
105
+ message: "Not found",
106
+ )
107
+ end
108
+ end
109
+
110
+ context "with request body & invalid content type", :with_valid_post_request do
111
+ before do
112
+ env["CONTENT_TYPE"] = "text/plain"
113
+ end
114
+
115
+ it "returns invalid_content_type error" do
116
+ should == 400
117
+ response.body.should be_json_as(
118
+ id: "invalid_content_type",
119
+ message: "Invalid content type",
120
+ )
121
+ end
122
+ end
123
+
124
+ context "with valid request property", :with_valid_post_request do
125
+ it { should == 200 }
126
+ end
127
+
128
+ context "with suffixed content type", :with_valid_post_request do
129
+ before do
130
+ env["CONTENT_TYPE"] = "application/json; charset=utf8"
131
+ end
132
+ it { should == 200 }
133
+ end
134
+
135
+ context "with invalid request property", :with_valid_post_request do
136
+ let(:params) do
137
+ { name: "ab" }.to_json
138
+ end
139
+
140
+ it "returns invalid_parameter error" do
141
+ should == 400
142
+ response.body.should be_json_as(
143
+ id: "invalid_parameter",
144
+ message: %r<\AInvalid request\.\n#/name: failed schema .+: Expected string to match pattern>,
145
+ )
146
+ end
147
+ end
148
+
149
+ context "with malformed JSON request body", :with_valid_post_request do
150
+ let(:params) do
151
+ "malformed"
152
+ end
153
+
154
+ it "returns invalid_parameter error" do
155
+ should == 400
156
+ response.body.should be_json_as(
157
+ id: "invalid_json",
158
+ message: "Request body wasn't valid JSON",
159
+ )
160
+ end
161
+ end
162
+ end
163
+
164
+ describe "ResponseValidation" do
165
+ let(:verb) do
166
+ :get
167
+ end
168
+
169
+ let(:path) do
170
+ "/apps"
171
+ end
172
+
173
+ let(:body) do
174
+ {
175
+ foo: "bar",
176
+ }.to_json
177
+ end
178
+
179
+ context "with response content type except for JSON" do
180
+ let(:response_headers) do
181
+ { "Content-Type" => "text/plain" }
182
+ end
183
+
184
+ it "returns invalid_response_content_type error" do
185
+ should == 500
186
+ response.body.should be_json_as(
187
+ id: "invalid_response_content_type",
188
+ message: "Response Content-Type wasn't for JSON",
189
+ )
190
+ end
191
+ end
192
+
193
+ context "without required field" do
194
+ before do
195
+ response_data.delete(:id)
196
+ end
197
+
198
+ it "returns invalid_response_type error" do
199
+ should == 500
200
+ response.body.should be_json_as(
201
+ id: "invalid_response_type",
202
+ message: /Missing required keys "id" in object/,
203
+ )
204
+ end
205
+ end
206
+
207
+ context "with invalid pattern string field" do
208
+ before do
209
+ response_data[:id] = "123"
210
+ end
211
+
212
+ it "returns invalid_response_type error" do
213
+ should == 500
214
+ response.body.should be_json_as(
215
+ id: "invalid_response_type",
216
+ message: /Expected data to match "uuid" format, value was: 123/,
217
+ )
218
+ end
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,11 @@
1
+ require "rack/builder"
2
+ require "rack/json_schema"
3
+ require "rack/test"
4
+ require "rspec/json_matcher"
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+ config.include RSpec::JsonMatcher
11
+ end
metadata ADDED
@@ -0,0 +1,229 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-json_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 2.14.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 2.14.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-console
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec-json_matcher
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - r7kamura@gmail.com
170
+ executables:
171
+ - specup
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - CHANGELOG.md
177
+ - Gemfile
178
+ - LICENSE.txt
179
+ - README.md
180
+ - Rakefile
181
+ - bin/specup
182
+ - lib/rack-json_schema.rb
183
+ - lib/rack/json_schema.rb
184
+ - lib/rack/json_schema/base_request_handler.rb
185
+ - lib/rack/json_schema/docs.rb
186
+ - lib/rack/json_schema/error.rb
187
+ - lib/rack/json_schema/error_handler.rb
188
+ - lib/rack/json_schema/mock.rb
189
+ - lib/rack/json_schema/request_validation.rb
190
+ - lib/rack/json_schema/response_validation.rb
191
+ - lib/rack/json_schema/schema.rb
192
+ - lib/rack/json_schema/version.rb
193
+ - rack-json_schema.gemspec
194
+ - spec/fixtures/schema.json
195
+ - spec/rack/spec/docs_spec.rb
196
+ - spec/rack/spec/mock_spec.rb
197
+ - spec/rack/spec_spec.rb
198
+ - spec/spec_helper.rb
199
+ homepage: https://github.com/r7kamura/rack-json_schema
200
+ licenses:
201
+ - MIT
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 2.2.2
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: JSON Schema based Rack middlewares
223
+ test_files:
224
+ - spec/fixtures/schema.json
225
+ - spec/rack/spec/docs_spec.rb
226
+ - spec/rack/spec/mock_spec.rb
227
+ - spec/rack/spec_spec.rb
228
+ - spec/spec_helper.rb
229
+ has_rdoc: