fakeit 0.5.3 → 0.6.0

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
  SHA256:
3
- metadata.gz: 110dc63680d60c4b438f4d2162c7164a6306519e4852cd68dbb36de630210a20
4
- data.tar.gz: 9ccc8d513d56cb66f6ed5438841052add1206cf2f5f0710cefa1956b2d2ffb72
3
+ metadata.gz: '08e498097efdfb3a803d5fcafc2f755b37f74ade0b0d409ba9ea36cd0ffbc0fd'
4
+ data.tar.gz: 77abcf6e9aa8db912d6b6a158a2cc8a95bbced90311617692e1f67f99e1d7249
5
5
  SHA512:
6
- metadata.gz: 23e5abad04ebea0e94c2bb643f77ecd86b4daec69ca13df18713a4c2ca2e601c019110435041d510281a41edd753ac3baf013a8fe1fbd606480eb7eea232bba6
7
- data.tar.gz: fb0fd7e1a33e71b434baa3a3c8a053a41cd8f29d3d93454b73d41fd9c8bf847974553964367b03039f7905444d061843bd0557feda246e7686a50c75c6fccb75
6
+ metadata.gz: cce0c415a9e2fb40b9b55178dd9b85b8b8f643453ab3e3d4a2203269af0ca7650adca35b464c30f7e82c3b29e8f2319ff1a8543ee8f3d0f793dfa28945f038f5
7
+ data.tar.gz: ac9ca06debd2181ee4332edbd30e7a3413f1cf4caeec7981340c6189258615411928364836601b8772be3d8efa87dd72e69e18870b8bd1e0c5dbcf3eb5ee821c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fakeit (0.5.3)
4
+ fakeit (0.6.0)
5
5
  faker (= 2.11.0)
6
6
  openapi_parser (= 0.8.0)
7
7
  rack (~> 2.0)
data/docs/static.md CHANGED
@@ -6,6 +6,7 @@ Static value generation rule
6
6
  |---|---|---|
7
7
  |complex|oneOf|first item|
8
8
  | |anyOf|same as allOf|
9
+ |boolean|N/A|`true`|
9
10
  |array|N/A|size is `1`|
10
11
  | |minItems|size is minItems|
11
12
  |string|N/A|`string`|
@@ -36,7 +36,7 @@ module Fakeit
36
36
 
37
37
  def validate(operation, request)
38
38
  operation.validate(
39
- body: request.body&.read.to_s,
39
+ body: BodyParser.parse(request),
40
40
  params: parse_query(request.query_string),
41
41
  headers: headers(request)
42
42
  )
@@ -0,0 +1,30 @@
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
+ 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
@@ -18,17 +18,17 @@ module Fakeit
18
18
  end
19
19
 
20
20
  def random_array_size(example_options)
21
- uniqueItems ? min_array : Faker::Number.between(from: min_array, to: max_array(example_options[:depth]))
21
+ uniqueItems ? unique_array_size : Faker::Number.between(from: min_array, to: max_array(example_options[:depth]))
22
22
  end
23
23
 
24
24
  def generate_items(size, retries, example_options, result)
25
- loop do
25
+ while result.size < size
26
26
  item = items.to_example(example_options)
27
27
 
28
28
  if need_retry?(item, result, retries)
29
29
  retries -= 1
30
- elsif (result << item).size >= size
31
- break
30
+ else
31
+ result << item
32
32
  end
33
33
  end
34
34
  end
@@ -41,6 +41,10 @@ module Fakeit
41
41
  uniqueItems && result.include?(item) && retries.positive?
42
42
  end
43
43
 
44
+ def unique_array_size
45
+ [min_array, 1].max
46
+ end
47
+
44
48
  def min_array
45
49
  minItems || 1
46
50
  end
@@ -5,13 +5,10 @@ module Fakeit
5
5
  @operation = operation
6
6
  end
7
7
 
8
- def validate(body: '', params: {}, headers: {})
8
+ def validate(body: {}, params: {}, headers: {})
9
9
  options = OpenAPIParser::SchemaValidator::Options.new(coerce_value: true)
10
10
 
11
- if request_content_type
12
- body_data = parse(body)
13
- @operation.validate_request_body(request_content_type, body_data)
14
- end
11
+ validate_body(body) unless request_content_types.empty?
15
12
  @operation.validate_path_params(options)
16
13
  @operation.validate_request_parameter(params, headers, options)
17
14
  rescue StandardError => e
@@ -20,14 +17,21 @@ module Fakeit
20
17
 
21
18
  private
22
19
 
23
- def parse(body)
24
- JSON.parse(body)
25
- rescue StandardError
26
- raise ValidationError, 'Invalid json payload'
20
+ def validate_body(body)
21
+ if request_content_types.include?(body[:media_type])
22
+ @operation.validate_request_body(body[:media_type], body[:data]) if can_validate?(body[:media_type])
23
+ else
24
+ raise ValidationError, 'Invalid request content type' if body[:media_type]
25
+ raise ValidationError, 'Request body is required' if request_body.required
26
+ end
27
+ end
28
+
29
+ def can_validate?(media_type)
30
+ media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
27
31
  end
28
32
 
29
- def request_content_type
30
- request_body&.content&.find { |k, _| k =~ %r{^application/.*json} }&.first
33
+ def request_content_types
34
+ request_body&.content&.keys.to_a
31
35
  end
32
36
 
33
37
  def request_body
@@ -1,3 +1,3 @@
1
1
  module Fakeit
2
- VERSION = '0.5.3'.freeze
2
+ VERSION = '0.6.0'.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.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Feng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-05 00:00:00.000000000 Z
11
+ date: 2020-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -215,6 +215,7 @@ files:
215
215
  - fakeit.gemspec
216
216
  - lib/fakeit.rb
217
217
  - lib/fakeit/app/app.rb
218
+ - lib/fakeit/app/body_parser.rb
218
219
  - lib/fakeit/app/options.rb
219
220
  - lib/fakeit/core_extensions/findable.rb
220
221
  - lib/fakeit/core_extensions/path_item_finder.rb