fakeit 0.1.5 → 0.2.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 +4 -4
- data/Gemfile.lock +18 -44
- data/README.md +10 -4
- data/bin/fakeit +10 -3
- data/docs/random.md +31 -0
- data/docs/static.md +29 -0
- data/fakeit.gemspec +1 -1
- data/lib/fakeit/app/options.rb +3 -2
- data/lib/fakeit/openapi/example/array_example.rb +21 -9
- data/lib/fakeit/openapi/example/boolean_example.rb +2 -2
- data/lib/fakeit/openapi/example/integer_example.rb +15 -3
- data/lib/fakeit/openapi/example/number_example.rb +12 -2
- data/lib/fakeit/openapi/example/object_example.rb +2 -2
- data/lib/fakeit/openapi/example/string_example.rb +41 -6
- data/lib/fakeit/openapi/operation.rb +8 -4
- data/lib/fakeit/openapi/schema.rb +15 -18
- data/lib/fakeit/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 727b95e51aed4489946200aac98d814fe6132e0fe7857819b85e3f48d2f6ab40
|
|
4
|
+
data.tar.gz: f3a78ca7afd4782f7003cdd3b888e334e60ea8edfd59b1fec058c822cd72ca6a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 60bd99ceff20e1ee73843901d6ba5b29955f044092162428736ca093b4f32557c5de755eae435c98d0488fd40997d5c420fac6c067635191b0074cc23a7b09c1
|
|
7
|
+
data.tar.gz: b549806af5f29b2284023a637bf79f3cb770a9ca449f3079d5be5c997bccaa4d4fc41efe607258f96b7b6d487eeb6f8fd47ef1c383f2aa3a45f82920397fdf3d
|
data/Gemfile.lock
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
fakeit (0.
|
|
4
|
+
fakeit (0.2.0)
|
|
5
5
|
faker (~> 1.9)
|
|
6
|
-
openapi_parser (= 0.3.
|
|
6
|
+
openapi_parser (= 0.3.1)
|
|
7
7
|
rack (~> 2.0)
|
|
8
8
|
rack-cors (~> 1.0)
|
|
9
9
|
slop (~> 4.6)
|
|
@@ -15,27 +15,17 @@ GEM
|
|
|
15
15
|
byebug (11.0.1)
|
|
16
16
|
concurrent-ruby (1.1.5)
|
|
17
17
|
diff-lcs (1.3)
|
|
18
|
-
docile (1.3.
|
|
19
|
-
|
|
20
|
-
faker (1.9.4)
|
|
18
|
+
docile (1.3.2)
|
|
19
|
+
faker (1.9.6)
|
|
21
20
|
i18n (>= 0.7)
|
|
22
|
-
pastel (~> 0.7.2)
|
|
23
|
-
thor (~> 0.20.0)
|
|
24
|
-
tty-pager (~> 0.12.0)
|
|
25
|
-
tty-screen (~> 0.6.5)
|
|
26
|
-
tty-tree (~> 0.3.0)
|
|
27
21
|
i18n (1.6.0)
|
|
28
22
|
concurrent-ruby (~> 1.0)
|
|
29
|
-
jaro_winkler (1.5.
|
|
23
|
+
jaro_winkler (1.5.3)
|
|
30
24
|
json (2.2.0)
|
|
31
|
-
openapi_parser (0.3.
|
|
25
|
+
openapi_parser (0.3.1)
|
|
32
26
|
parallel (1.17.0)
|
|
33
|
-
parser (2.6.
|
|
27
|
+
parser (2.6.3.0)
|
|
34
28
|
ast (~> 2.4.0)
|
|
35
|
-
pastel (0.7.3)
|
|
36
|
-
equatable (~> 0.6)
|
|
37
|
-
tty-color (~> 0.5)
|
|
38
|
-
psych (3.1.0)
|
|
39
29
|
rack (2.0.7)
|
|
40
30
|
rack-cors (1.0.3)
|
|
41
31
|
rack-test (1.1.0)
|
|
@@ -46,46 +36,30 @@ GEM
|
|
|
46
36
|
rspec-core (~> 3.8.0)
|
|
47
37
|
rspec-expectations (~> 3.8.0)
|
|
48
38
|
rspec-mocks (~> 3.8.0)
|
|
49
|
-
rspec-core (3.8.
|
|
39
|
+
rspec-core (3.8.2)
|
|
50
40
|
rspec-support (~> 3.8.0)
|
|
51
|
-
rspec-expectations (3.8.
|
|
41
|
+
rspec-expectations (3.8.4)
|
|
52
42
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
53
43
|
rspec-support (~> 3.8.0)
|
|
54
|
-
rspec-mocks (3.8.
|
|
44
|
+
rspec-mocks (3.8.1)
|
|
55
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
56
46
|
rspec-support (~> 3.8.0)
|
|
57
|
-
rspec-support (3.8.
|
|
58
|
-
rubocop (0.
|
|
47
|
+
rspec-support (3.8.2)
|
|
48
|
+
rubocop (0.72.0)
|
|
59
49
|
jaro_winkler (~> 1.5.1)
|
|
60
50
|
parallel (~> 1.10)
|
|
61
|
-
parser (>= 2.
|
|
62
|
-
psych (>= 3.1.0)
|
|
51
|
+
parser (>= 2.6)
|
|
63
52
|
rainbow (>= 2.2.2, < 4.0)
|
|
64
53
|
ruby-progressbar (~> 1.7)
|
|
65
|
-
unicode-display_width (>= 1.4.0, < 1.
|
|
66
|
-
ruby-progressbar (1.10.
|
|
67
|
-
simplecov (0.
|
|
54
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
|
55
|
+
ruby-progressbar (1.10.1)
|
|
56
|
+
simplecov (0.17.0)
|
|
68
57
|
docile (~> 1.1)
|
|
69
58
|
json (>= 1.8, < 3)
|
|
70
59
|
simplecov-html (~> 0.10.0)
|
|
71
60
|
simplecov-html (0.10.2)
|
|
72
|
-
slop (4.
|
|
73
|
-
|
|
74
|
-
strings-ansi (~> 0.1)
|
|
75
|
-
unicode-display_width (~> 1.5)
|
|
76
|
-
unicode_utils (~> 1.4)
|
|
77
|
-
strings-ansi (0.1.0)
|
|
78
|
-
thor (0.20.3)
|
|
79
|
-
tty-color (0.5.0)
|
|
80
|
-
tty-pager (0.12.1)
|
|
81
|
-
strings (~> 0.1.4)
|
|
82
|
-
tty-screen (~> 0.6)
|
|
83
|
-
tty-which (~> 0.4)
|
|
84
|
-
tty-screen (0.6.5)
|
|
85
|
-
tty-tree (0.3.0)
|
|
86
|
-
tty-which (0.4.1)
|
|
87
|
-
unicode-display_width (1.5.0)
|
|
88
|
-
unicode_utils (1.4.0)
|
|
61
|
+
slop (4.7.0)
|
|
62
|
+
unicode-display_width (1.6.0)
|
|
89
63
|
|
|
90
64
|
PLATFORMS
|
|
91
65
|
ruby
|
data/README.md
CHANGED
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
Create mock server from Openapi specification
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Features
|
|
13
13
|
|
|
14
|
-
* Randomly generated response
|
|
14
|
+
* Randomly or statically generated response
|
|
15
15
|
* Request validation
|
|
16
16
|
* Load specification from local or remote
|
|
17
17
|
* Support specification in yaml or json format
|
|
@@ -37,14 +37,20 @@ Command line options:
|
|
|
37
37
|
--spec spec file uri (required)
|
|
38
38
|
-p, --port custom port
|
|
39
39
|
-q, --quiet mute request and response log
|
|
40
|
-
--permissive log validation error as warning instead of
|
|
41
|
-
--use-example use example provided in spec
|
|
40
|
+
--permissive log validation error as warning instead of denying request
|
|
41
|
+
--use-example use example provided in spec if exists
|
|
42
|
+
|
|
43
|
+
trial options:
|
|
44
|
+
--static generate static response
|
|
42
45
|
|
|
43
46
|
other options:
|
|
44
47
|
-v, --version
|
|
45
48
|
-h, --help
|
|
46
49
|
|
|
47
50
|
**Notes:**
|
|
51
|
+
* See [here](docs/random.md) for Openapi properties supported in random response generation
|
|
52
|
+
* See [here](docs/static.md) for default value in static response generation
|
|
53
|
+
* Regarding `--use-example` mode, property without example specified will still be randomly or statically generated
|
|
48
54
|
* Random response generation can not handle recursive schema reference. If you do need it in your spec file, please provide `example` property for the recursive part of schema and specify `--use-example` option.
|
|
49
55
|
|
|
50
56
|
## Development
|
data/bin/fakeit
CHANGED
|
@@ -10,8 +10,11 @@ begin
|
|
|
10
10
|
o.string '--spec', 'spec file uri (required)', required: true
|
|
11
11
|
o.integer '-p', '--port', 'custom port'
|
|
12
12
|
o.bool '-q', '--quiet', 'mute request and response log'
|
|
13
|
-
o.bool '--permissive', 'log validation error as warning instead of
|
|
14
|
-
o.bool '--use-example', 'use example provided in spec'
|
|
13
|
+
o.bool '--permissive', 'log validation error as warning instead of denying request'
|
|
14
|
+
o.bool '--use-example', 'use example provided in spec if exists'
|
|
15
|
+
o.separator ''
|
|
16
|
+
o.separator 'trial options:'
|
|
17
|
+
o.bool '--static', 'generate static response'
|
|
15
18
|
o.separator ''
|
|
16
19
|
o.separator 'other options:'
|
|
17
20
|
o.on '-v', '--version' do
|
|
@@ -41,7 +44,11 @@ trap(:INT) do
|
|
|
41
44
|
end
|
|
42
45
|
end
|
|
43
46
|
|
|
44
|
-
options = Fakeit::App::Options.new(
|
|
47
|
+
options = Fakeit::App::Options.new(
|
|
48
|
+
permissive: opts.permissive?,
|
|
49
|
+
use_example: opts.use_example?,
|
|
50
|
+
static: opts.static?
|
|
51
|
+
)
|
|
45
52
|
app = Fakeit.build(opts[:spec], options)
|
|
46
53
|
|
|
47
54
|
app.use Rack::Cors do
|
data/docs/random.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Random
|
|
2
|
+
|
|
3
|
+
The following Openapi properties are supported in random response generation
|
|
4
|
+
|
|
5
|
+
|Type|Property|Notes|
|
|
6
|
+
|---|---|---|
|
|
7
|
+
|complex|oneOf| |
|
|
8
|
+
| |allOf| |
|
|
9
|
+
| |anyOf| |
|
|
10
|
+
|array|uniqueItems|Try to generate unique random items, but will give up after exceeding retry limits|
|
|
11
|
+
| |minItems|Default: `1`|
|
|
12
|
+
| |maxItems|Default: `minItems + 2`|
|
|
13
|
+
|string|enum| |
|
|
14
|
+
| |pattern| |
|
|
15
|
+
| |format=uri| |
|
|
16
|
+
| |format=uuid| |
|
|
17
|
+
| |format=guid| |
|
|
18
|
+
| |format=email| |
|
|
19
|
+
| |format=date|In past 100 days|
|
|
20
|
+
| |format=date-time|In past 100 days|
|
|
21
|
+
| |minLength|Default: `0`|
|
|
22
|
+
| |maxLength|Default: `minLength + 10`|
|
|
23
|
+
|integer|enum| |
|
|
24
|
+
| |minimum|Default: `1`|
|
|
25
|
+
| |maximum|Default: `2^32`|
|
|
26
|
+
| |exclusiveMinimum| |
|
|
27
|
+
| |exclusiveMaximum| |
|
|
28
|
+
| |multipleOf| |
|
|
29
|
+
|number|minimum|Default: `0.0`|
|
|
30
|
+
| |maximum|Default: `2^32`|
|
|
31
|
+
| |multipleOf| |
|
data/docs/static.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Static
|
|
2
|
+
|
|
3
|
+
Static value generation rule
|
|
4
|
+
|
|
5
|
+
|Type|Property|Rule|
|
|
6
|
+
|---|---|---|
|
|
7
|
+
|complex|oneOf|first item|
|
|
8
|
+
| |anyOf|same as allOf|
|
|
9
|
+
|array|N/A|size is `1`|
|
|
10
|
+
| |minItems|size is minItems|
|
|
11
|
+
|string|N/A|`string`|
|
|
12
|
+
| |enum|first item|
|
|
13
|
+
| |pattern|Generated but always the same|
|
|
14
|
+
| |format=uri|`https://some.uri`|
|
|
15
|
+
| |format=uuid|`11111111-1111-1111-1111-111111111111`|
|
|
16
|
+
| |format=guid|`11111111-1111-1111-1111-111111111111`|
|
|
17
|
+
| |format=email|`some@email.com`|
|
|
18
|
+
| |format=date|today|
|
|
19
|
+
| |format=date-time|midnight today|
|
|
20
|
+
| |minLength|`1` repeats for (minLength + 10) times if no maxLength specified|
|
|
21
|
+
| |maxLength|`1` repeats for maxLength times|
|
|
22
|
+
|integer|N/A|`1`|
|
|
23
|
+
| |enum|first item|
|
|
24
|
+
| |minimum|minimum|
|
|
25
|
+
| |exclusiveMinimum|minimum + 1|
|
|
26
|
+
| |multipleOf|minimum value meets multipleOf condition|
|
|
27
|
+
|number|N/A|0.0|
|
|
28
|
+
| |minimum|minimum rounded to 2 decimal places|
|
|
29
|
+
| |multipleOf|minimum value meets multipleOf condition|
|
data/fakeit.gemspec
CHANGED
|
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
|
|
|
33
33
|
spec.add_development_dependency 'simplecov', '~> 0.16'
|
|
34
34
|
|
|
35
35
|
spec.add_dependency 'faker', '~> 1.9'
|
|
36
|
-
spec.add_dependency 'openapi_parser', '0.3.
|
|
36
|
+
spec.add_dependency 'openapi_parser', '0.3.1'
|
|
37
37
|
spec.add_dependency 'rack', '~> 2.0'
|
|
38
38
|
spec.add_dependency 'rack-cors', '~> 1.0'
|
|
39
39
|
spec.add_dependency 'slop', '~> 4.6'
|
data/lib/fakeit/app/options.rb
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
module Fakeit
|
|
2
2
|
module App
|
|
3
3
|
class Options
|
|
4
|
-
attr_reader :permissive, :use_example
|
|
4
|
+
attr_reader :permissive, :use_example, :static
|
|
5
5
|
|
|
6
|
-
def initialize(permissive: false, use_example: false)
|
|
6
|
+
def initialize(permissive: false, use_example: false, static: false)
|
|
7
7
|
@permissive = permissive
|
|
8
8
|
@use_example = use_example
|
|
9
|
+
@static = static
|
|
9
10
|
end
|
|
10
11
|
end
|
|
11
12
|
end
|
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
module Fakeit
|
|
2
2
|
module Openapi
|
|
3
3
|
module Example
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
size = retries = uniqueItems ? min_array : Faker::Number.between(min_array, max_array)
|
|
8
|
-
[].tap { |result| generate_items(size, retries, use_example, result) }
|
|
4
|
+
def array_example(options)
|
|
5
|
+
example_options = add_depth(options)
|
|
6
|
+
example_options[:static] ? static_array_example(example_options) : random_array_example(example_options)
|
|
9
7
|
end
|
|
10
8
|
|
|
11
9
|
private
|
|
12
10
|
|
|
13
|
-
def
|
|
11
|
+
def static_array_example(example_options)
|
|
12
|
+
size = retries = min_array
|
|
13
|
+
[].tap { |result| generate_items(size, retries, example_options, result) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def random_array_example(example_options)
|
|
17
|
+
size = retries = uniqueItems ? min_array : Faker::Number.between(min_array, max_array(example_options[:depth]))
|
|
18
|
+
[].tap { |result| generate_items(size, retries, example_options, result) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def generate_items(size, retries, example_options, result)
|
|
14
22
|
loop do
|
|
15
|
-
item = items.to_example(
|
|
23
|
+
item = items.to_example(example_options)
|
|
16
24
|
|
|
17
25
|
if need_retry?(item, result, retries)
|
|
18
26
|
retries -= 1
|
|
@@ -22,6 +30,10 @@ module Fakeit
|
|
|
22
30
|
end
|
|
23
31
|
end
|
|
24
32
|
|
|
33
|
+
def add_depth(example_options)
|
|
34
|
+
example_options.merge(depth: example_options[:depth] + 1)
|
|
35
|
+
end
|
|
36
|
+
|
|
25
37
|
def need_retry?(item, result, retries)
|
|
26
38
|
uniqueItems && result.include?(item) && retries.positive?
|
|
27
39
|
end
|
|
@@ -30,8 +42,8 @@ module Fakeit
|
|
|
30
42
|
minItems || 1
|
|
31
43
|
end
|
|
32
44
|
|
|
33
|
-
def max_array
|
|
34
|
-
maxItems ||
|
|
45
|
+
def max_array(depth)
|
|
46
|
+
maxItems || min_array + (depth > 1 ? 2 : 9)
|
|
35
47
|
end
|
|
36
48
|
end
|
|
37
49
|
end
|
|
@@ -3,7 +3,21 @@ module Fakeit
|
|
|
3
3
|
module Example
|
|
4
4
|
BIG_INT = 2**32
|
|
5
5
|
|
|
6
|
-
def integer_example
|
|
6
|
+
def integer_example(example_options)
|
|
7
|
+
example_options[:static] ? static_integer_example : random_integer_example
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def static_integer_example
|
|
13
|
+
if enum
|
|
14
|
+
enum.to_a.first
|
|
15
|
+
else
|
|
16
|
+
int_rand_begin * int_multiple
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def random_integer_example
|
|
7
21
|
if enum
|
|
8
22
|
enum.to_a.sample
|
|
9
23
|
else
|
|
@@ -11,8 +25,6 @@ module Fakeit
|
|
|
11
25
|
end
|
|
12
26
|
end
|
|
13
27
|
|
|
14
|
-
private
|
|
15
|
-
|
|
16
28
|
def int_rand_begin
|
|
17
29
|
min_int / int_multiple + int_rand_begin_adjust
|
|
18
30
|
end
|
|
@@ -3,12 +3,22 @@ module Fakeit
|
|
|
3
3
|
module Example
|
|
4
4
|
BIG_NUM = 2**32
|
|
5
5
|
|
|
6
|
-
def number_example
|
|
7
|
-
|
|
6
|
+
def number_example(example_options)
|
|
7
|
+
example_options[:static] ? static_number_example : random_number_example
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
private
|
|
11
11
|
|
|
12
|
+
def static_number_example
|
|
13
|
+
(num_rand_begin * num_multiple)
|
|
14
|
+
.then { |result| multipleOf ? result : result.round(2) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def random_number_example
|
|
18
|
+
(Faker::Number.between(num_rand_begin, num_rand_end) * num_multiple)
|
|
19
|
+
.then { |result| multipleOf ? result : result.round(2) }
|
|
20
|
+
end
|
|
21
|
+
|
|
12
22
|
def num_rand_begin
|
|
13
23
|
multipleOf ? (min_num / multipleOf).ceil : min_num
|
|
14
24
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
module Fakeit
|
|
2
2
|
module Openapi
|
|
3
3
|
module Example
|
|
4
|
-
def object_example(
|
|
5
|
-
properties.each_with_object({}) { |(name, schema), obj| obj[name] = schema.to_example(
|
|
4
|
+
def object_example(example_options)
|
|
5
|
+
properties.each_with_object({}) { |(name, schema), obj| obj[name] = schema.to_example(example_options) }
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
end
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
module Fakeit
|
|
2
2
|
module Openapi
|
|
3
3
|
module Example
|
|
4
|
-
|
|
4
|
+
STATIC_FORMAT_HANDLERS = {
|
|
5
|
+
'uri' => -> { 'https://some.uri' },
|
|
6
|
+
'uuid' => -> { '11111111-1111-1111-1111-111111111111' },
|
|
7
|
+
'guid' => -> { '11111111-1111-1111-1111-111111111111' },
|
|
8
|
+
'email' => -> { 'some@email.com' },
|
|
9
|
+
'date' => -> { Date.today.iso8601 },
|
|
10
|
+
'date-time' => lambda do
|
|
11
|
+
now = Time.now
|
|
12
|
+
Time.new(now.year, now.month, now.day, 0, 0, 0, now.utc_offset).iso8601
|
|
13
|
+
end
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
16
|
+
RANDOM_FORMAT_HANDLERS = {
|
|
5
17
|
'uri' => -> { Faker::Internet.url },
|
|
6
18
|
'uuid' => -> { SecureRandom.uuid },
|
|
7
19
|
'guid' => -> { SecureRandom.uuid },
|
|
@@ -10,16 +22,39 @@ module Fakeit
|
|
|
10
22
|
'date-time' => -> { Faker::Time.backward(100).iso8601 }
|
|
11
23
|
}.freeze
|
|
12
24
|
|
|
13
|
-
def string_example
|
|
25
|
+
def string_example(example_options)
|
|
26
|
+
example_options[:static] ? static_string_example : random_string_example
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def static_string_example
|
|
32
|
+
Faker::Config.random = Random.new(1) # Fix seed for faker
|
|
33
|
+
|
|
34
|
+
if enum then enum.to_a.first
|
|
35
|
+
elsif pattern then Faker::Base.regexify(pattern)
|
|
36
|
+
elsif format then static_string_format
|
|
37
|
+
elsif length_constraint then static_string_with_length
|
|
38
|
+
else 'string'
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def random_string_example
|
|
14
43
|
if enum then enum.to_a.sample
|
|
15
44
|
elsif pattern then Faker::Base.regexify(pattern)
|
|
16
|
-
elsif format then
|
|
45
|
+
elsif format then random_string_format
|
|
17
46
|
elsif length_constraint then string_with_length
|
|
18
47
|
else Faker::Book.title
|
|
19
48
|
end
|
|
20
49
|
end
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
def static_string_with_length
|
|
52
|
+
'1' * max_string_length
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def static_string_format
|
|
56
|
+
(STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]
|
|
57
|
+
end
|
|
23
58
|
|
|
24
59
|
def length_constraint
|
|
25
60
|
minLength || maxLength
|
|
@@ -37,8 +72,8 @@ module Fakeit
|
|
|
37
72
|
maxLength || min_string_length + 10
|
|
38
73
|
end
|
|
39
74
|
|
|
40
|
-
def
|
|
41
|
-
(
|
|
75
|
+
def random_string_format
|
|
76
|
+
(RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]
|
|
42
77
|
end
|
|
43
78
|
|
|
44
79
|
def unknown_format
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
module Fakeit
|
|
2
2
|
module Openapi
|
|
3
3
|
class Operation
|
|
4
|
-
def initialize(request_operation,
|
|
4
|
+
def initialize(request_operation, app_options)
|
|
5
5
|
@request_operation = request_operation
|
|
6
6
|
@validator = Fakeit::Validation::Validator.new(request_operation)
|
|
7
|
-
@
|
|
7
|
+
@app_options = app_options
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def status
|
|
@@ -13,7 +13,7 @@ module Fakeit
|
|
|
13
13
|
|
|
14
14
|
def headers
|
|
15
15
|
response_headers
|
|
16
|
-
&.map { |k, v| [k, v.schema.to_example(
|
|
16
|
+
&.map { |k, v| [k, v.schema.to_example(example_options)] }
|
|
17
17
|
.to_h
|
|
18
18
|
.tap { |headers| headers['Content-Type'] = response_content_type if response_content_type }
|
|
19
19
|
end
|
|
@@ -21,7 +21,7 @@ module Fakeit
|
|
|
21
21
|
def body
|
|
22
22
|
response_schema
|
|
23
23
|
&.schema
|
|
24
|
-
&.to_example(
|
|
24
|
+
&.to_example(example_options)
|
|
25
25
|
&.then(&JSON.method(:generate))
|
|
26
26
|
.to_s
|
|
27
27
|
end
|
|
@@ -32,6 +32,10 @@ module Fakeit
|
|
|
32
32
|
|
|
33
33
|
private
|
|
34
34
|
|
|
35
|
+
def example_options
|
|
36
|
+
{ use_example: @app_options.use_example, static: @app_options.static, depth: 0 }
|
|
37
|
+
end
|
|
38
|
+
|
|
35
39
|
def response_content
|
|
36
40
|
response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
|
|
37
41
|
end
|
|
@@ -10,42 +10,39 @@ module Fakeit
|
|
|
10
10
|
module Schema
|
|
11
11
|
include Fakeit::Openapi::Example
|
|
12
12
|
|
|
13
|
-
def to_example(
|
|
14
|
-
return example if use_example && example
|
|
13
|
+
def to_example(**example_options)
|
|
14
|
+
return example if example_options[:use_example] && example
|
|
15
15
|
|
|
16
|
-
return one_of_example(
|
|
17
|
-
return all_of_example(
|
|
18
|
-
return any_of_example(
|
|
16
|
+
return one_of_example(example_options) if one_of
|
|
17
|
+
return all_of_example(example_options) if all_of
|
|
18
|
+
return any_of_example(example_options) if any_of
|
|
19
19
|
|
|
20
|
-
type_based_example(
|
|
20
|
+
type_based_example(example_options)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
private
|
|
24
24
|
|
|
25
|
-
def one_of_example(
|
|
26
|
-
one_of.sample.to_example(
|
|
25
|
+
def one_of_example(example_options)
|
|
26
|
+
example_options[:static] ? one_of.first.to_example(example_options) : one_of.sample.to_example(example_options)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def all_of_example(
|
|
29
|
+
def all_of_example(example_options)
|
|
30
30
|
all_of
|
|
31
31
|
.select { |option| option.type == 'object' }
|
|
32
|
-
.map { |option| option.to_example(
|
|
32
|
+
.map { |option| option.to_example(example_options) }
|
|
33
33
|
.reduce(&:merge)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
def any_of_example(
|
|
36
|
+
def any_of_example(example_options)
|
|
37
37
|
any_of
|
|
38
38
|
.select { |option| option.type == 'object' }
|
|
39
|
-
.sample(Faker::Number.between(1, any_of.size))
|
|
40
|
-
.map { |option| option.to_example(
|
|
39
|
+
.then { |options| example_options[:static] ? options : options.sample(Faker::Number.between(1, any_of.size)) }
|
|
40
|
+
.map { |option| option.to_example(example_options) }
|
|
41
41
|
.reduce(&:merge)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
def type_based_example(
|
|
45
|
-
|
|
46
|
-
when 'string', 'integer', 'number', 'boolean' then send("#{type}_example")
|
|
47
|
-
when 'array', 'object' then send("#{type}_example", use_example)
|
|
48
|
-
end
|
|
44
|
+
def type_based_example(example_options)
|
|
45
|
+
send("#{type}_example", example_options) if %w[string integer number boolean array object].include?(type)
|
|
49
46
|
end
|
|
50
47
|
end
|
|
51
48
|
end
|
data/lib/fakeit/version.rb
CHANGED
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.
|
|
4
|
+
version: 0.2.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: 2019-
|
|
11
|
+
date: 2019-07-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -128,14 +128,14 @@ dependencies:
|
|
|
128
128
|
requirements:
|
|
129
129
|
- - '='
|
|
130
130
|
- !ruby/object:Gem::Version
|
|
131
|
-
version: 0.3.
|
|
131
|
+
version: 0.3.1
|
|
132
132
|
type: :runtime
|
|
133
133
|
prerelease: false
|
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
|
135
135
|
requirements:
|
|
136
136
|
- - '='
|
|
137
137
|
- !ruby/object:Gem::Version
|
|
138
|
-
version: 0.3.
|
|
138
|
+
version: 0.3.1
|
|
139
139
|
- !ruby/object:Gem::Dependency
|
|
140
140
|
name: rack
|
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -196,6 +196,8 @@ files:
|
|
|
196
196
|
- README.md
|
|
197
197
|
- Rakefile
|
|
198
198
|
- bin/fakeit
|
|
199
|
+
- docs/random.md
|
|
200
|
+
- docs/static.md
|
|
199
201
|
- fakeit.gemspec
|
|
200
202
|
- lib/fakeit.rb
|
|
201
203
|
- lib/fakeit/app/app.rb
|