fakeit 0.6.2 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,8 @@ module Fakeit
4
4
  def load(src)
5
5
  parse_method = parse_method(src)
6
6
 
7
- open(src, &:read)
7
+ URI
8
+ .open(src, &:read)
8
9
  .then(&parse_method)
9
10
  .then(&OpenAPIParser.method(:parse))
10
11
  end
@@ -7,9 +7,7 @@ module Fakeit
7
7
  @app_options = app_options
8
8
  end
9
9
 
10
- def status
11
- response.first.to_i
12
- end
10
+ def status = response.first.to_i
13
11
 
14
12
  def headers
15
13
  response_headers
@@ -26,9 +24,7 @@ module Fakeit
26
24
  .to_s
27
25
  end
28
26
 
29
- def validate(**request_parts)
30
- @validator.validate(**request_parts)
31
- end
27
+ def validate(...) = @validator.validate(...)
32
28
 
33
29
  private
34
30
 
@@ -36,25 +32,15 @@ module Fakeit
36
32
  { use_example: @app_options.use_example, use_static: @app_options.method(:use_static?), depth: 0 }
37
33
  end
38
34
 
39
- def response_content
40
- response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
41
- end
35
+ def response_content = response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
42
36
 
43
- def response_schema
44
- response_content&.last
45
- end
37
+ def response_schema = response_content&.last
46
38
 
47
- def response_content_type
48
- response_content&.first
49
- end
39
+ def response_content_type = response_content&.first
50
40
 
51
- def response_headers
52
- response.last.headers
53
- end
41
+ def response_headers = response.last.headers
54
42
 
55
- def response
56
- @request_operation.operation_object.responses.response.min
57
- end
43
+ def response = @request_operation.operation_object.responses.response.min
58
44
  end
59
45
  end
60
46
  end
@@ -0,0 +1,6 @@
1
+ module Fakeit
2
+ module Openapi
3
+ class ReferenceError < StandardError
4
+ end
5
+ end
6
+ end
@@ -25,7 +25,7 @@ module Fakeit
25
25
  @mtime = new_mtime
26
26
  @doc = Fakeit::Openapi.load(@spec_file)
27
27
  rescue StandardError => _e
28
- Fakeit::Logger.warn(Rainbow('Invalid spec file, use previous snapshot instead').red)
28
+ Logger.warn(Rainbow('Invalid spec file, use previous snapshot instead').red)
29
29
  end
30
30
  end
31
31
  end
@@ -1,9 +1,7 @@
1
1
  module Fakeit
2
2
  module Validation
3
3
  class Validator
4
- def initialize(operation)
5
- @operation = operation
6
- end
4
+ def initialize(operation) = @operation = operation
7
5
 
8
6
  def validate(body: {}, params: {}, headers: {})
9
7
  options = OpenAPIParser::SchemaValidator::Options.new(coerce_value: true)
@@ -26,17 +24,11 @@ module Fakeit
26
24
  end
27
25
  end
28
26
 
29
- def can_validate?(media_type)
30
- media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
31
- end
27
+ def can_validate?(media_type) = media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
32
28
 
33
- def request_content_types
34
- request_body&.content&.keys.to_a
35
- end
29
+ def request_content_types = request_body&.content&.keys.to_a
36
30
 
37
- def request_body
38
- @operation.operation_object.request_body
39
- end
31
+ def request_body = @operation.operation_object.request_body
40
32
  end
41
33
  end
42
34
  end
@@ -1,3 +1,3 @@
1
1
  module Fakeit
2
- VERSION = '0.6.2'.freeze
2
+ VERSION = '0.8.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Feng
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-12 00:00:00.000000000 Z
11
+ date: 2021-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '12.0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '12.0'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,28 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.80'
89
+ version: '1.10'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.80'
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.5'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: simplecov
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +142,14 @@ dependencies:
128
142
  requirements:
129
143
  - - '='
130
144
  - !ruby/object:Gem::Version
131
- version: 0.11.2
145
+ version: 0.12.1
132
146
  type: :runtime
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
150
  - - '='
137
151
  - !ruby/object:Gem::Version
138
- version: 0.11.2
152
+ version: 0.12.1
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: rack
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +192,20 @@ dependencies:
178
192
  - - "~>"
179
193
  - !ruby/object:Gem::Version
180
194
  version: '3.0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: regexp-examples
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '='
200
+ - !ruby/object:Gem::Version
201
+ version: 1.5.1
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '='
207
+ - !ruby/object:Gem::Version
208
+ version: 1.5.1
181
209
  - !ruby/object:Gem::Dependency
182
210
  name: slop
183
211
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +220,20 @@ dependencies:
192
220
  - - "~>"
193
221
  - !ruby/object:Gem::Version
194
222
  version: '4.8'
223
+ - !ruby/object:Gem::Dependency
224
+ name: webrick
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '1.7'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '1.7'
195
237
  description: Create mock server from Openapi specification
196
238
  email:
197
239
  - realfengjia@foxmail.com
@@ -204,6 +246,7 @@ files:
204
246
  - ".gitignore"
205
247
  - ".rspec"
206
248
  - ".rubocop.yml"
249
+ - CHANGELOG.md
207
250
  - Gemfile
208
251
  - Gemfile.lock
209
252
  - LICENSE.txt
@@ -214,10 +257,13 @@ files:
214
257
  - docs/static.md
215
258
  - fakeit.gemspec
216
259
  - lib/fakeit.rb
217
- - lib/fakeit/app/app.rb
218
- - lib/fakeit/app/body_parser.rb
260
+ - lib/fakeit/app/app_builder.rb
261
+ - lib/fakeit/app/helpers/body_parser.rb
262
+ - lib/fakeit/app/helpers/response_builder.rb
219
263
  - lib/fakeit/app/options.rb
220
- - lib/fakeit/core_extensions/path_item_finder.rb
264
+ - lib/fakeit/app/routes/config_route.rb
265
+ - lib/fakeit/app/routes/openapi_route.rb
266
+ - lib/fakeit/core_extensions/reference.rb
221
267
  - lib/fakeit/core_extensions/schema.rb
222
268
  - lib/fakeit/logger.rb
223
269
  - lib/fakeit/middleware/recorder.rb
@@ -229,6 +275,7 @@ files:
229
275
  - lib/fakeit/openapi/example/string_example.rb
230
276
  - lib/fakeit/openapi/loader.rb
231
277
  - lib/fakeit/openapi/operation.rb
278
+ - lib/fakeit/openapi/reference_error.rb
232
279
  - lib/fakeit/openapi/schema.rb
233
280
  - lib/fakeit/openapi/specification.rb
234
281
  - lib/fakeit/validation/validation_error.rb
@@ -238,7 +285,7 @@ homepage: https://github.com/JustinFeng/fakeit
238
285
  licenses:
239
286
  - MIT
240
287
  metadata: {}
241
- post_install_message:
288
+ post_install_message:
242
289
  rdoc_options: []
243
290
  require_paths:
244
291
  - lib
@@ -246,15 +293,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
246
293
  requirements:
247
294
  - - ">="
248
295
  - !ruby/object:Gem::Version
249
- version: 2.7.0
296
+ version: 3.0.0
250
297
  required_rubygems_version: !ruby/object:Gem::Requirement
251
298
  requirements:
252
299
  - - ">="
253
300
  - !ruby/object:Gem::Version
254
301
  version: '0'
255
302
  requirements: []
256
- rubygems_version: 3.1.2
257
- signing_key:
303
+ rubygems_version: 3.2.3
304
+ signing_key:
258
305
  specification_version: 4
259
306
  summary: Create mock server from Openapi specification
260
307
  test_files: []
@@ -1,63 +0,0 @@
1
- module Fakeit
2
- module App
3
- class << self
4
- def create(spec_file, options)
5
- specification = Fakeit::Openapi::Specification.new(spec_file)
6
-
7
- proc do |env|
8
- request = Rack::Request.new(env)
9
- specification
10
- .operation(request.request_method.downcase.to_sym, request.path_info, options)
11
- .then { _1 ? handle(_1, request, options) : not_found }
12
- end
13
- end
14
-
15
- private
16
-
17
- def handle(operation, request, options)
18
- validate(operation, request)
19
- response(operation)
20
- rescue Fakeit::Validation::ValidationError => e
21
- Fakeit::Logger.warn(Rainbow(e.message).red)
22
- options.permissive ? response(operation) : error(e)
23
- end
24
-
25
- def error(err)
26
- [418, { 'Content-Type' => 'application/json' }, [{ message: err.message }.to_json]]
27
- end
28
-
29
- def not_found
30
- [404, {}, ['Not Found']]
31
- end
32
-
33
- def response(operation)
34
- [operation.status, operation.headers, [operation.body]]
35
- end
36
-
37
- def validate(operation, request)
38
- operation.validate(
39
- body: BodyParser.parse(request),
40
- params: parse_query(request.query_string),
41
- headers: headers(request)
42
- )
43
- end
44
-
45
- def headers(request)
46
- request
47
- .each_header
48
- .select { |k, _| k.start_with? 'HTTP_' }
49
- .map { |k, v| [k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-'), v] }
50
- .to_h
51
- end
52
-
53
- def parse_query(query_string)
54
- rack_query = Rack::Utils.parse_nested_query(query_string)
55
- cgi_query = CGI.parse(query_string)
56
-
57
- rack_query.merge(cgi_query.slice(*rack_query.keys)) do |_, oldval, newval|
58
- newval.is_a?(Array) && newval.size > 1 ? newval : oldval
59
- end
60
- end
61
- end
62
- end
63
- end
@@ -1,30 +0,0 @@
1
- module Fakeit
2
- module App
3
- class BodyParser
4
- class << self
5
- def parse(request)
6
- case request.media_type
7
- when %r{^application/.*json}
8
- { media_type: request.media_type, data: parse_json(request.body.read) }
9
- when 'multipart/form-data'
10
- { media_type: request.media_type, data: parse_form_data(request.params) }
11
- else
12
- { media_type: request.media_type, data: request.body.read }
13
- end
14
- end
15
-
16
- private
17
-
18
- def parse_json(body)
19
- body.empty? ? {} : JSON.parse(body)
20
- rescue StandardError
21
- raise Fakeit::Validation::ValidationError, 'Invalid json payload'
22
- end
23
-
24
- def parse_form_data(params)
25
- params.transform_values { |v| v.class == Hash && v[:tempfile] ? v[:tempfile].read : v }
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,14 +0,0 @@
1
- # https://github.com/ota42y/openapi_parser/issues/75
2
- module OpenAPIParser
3
- class PathItemFinder
4
- private
5
-
6
- def find_path_and_params(http_method, request_path)
7
- return [request_path, {}] if matches_directly?(request_path, http_method)
8
-
9
- matching = matching_paths_with_params(request_path, http_method)
10
-
11
- matching.min_by { |match| match[1].size }
12
- end
13
- end
14
- end