fakeit 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +48 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.rubocop.yml +18 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +9 -0
- data/bin/fakeit +43 -0
- data/fakeit.gemspec +37 -0
- data/lib/fakeit.rb +18 -0
- data/lib/fakeit/app/app.rb +51 -0
- data/lib/fakeit/core_extensions/schema.rb +39 -0
- data/lib/fakeit/middleware/logger.rb +28 -0
- data/lib/fakeit/openapi/loader.rb +27 -0
- data/lib/fakeit/openapi/operation.rb +51 -0
- data/lib/fakeit/openapi/specification.rb +13 -0
- data/lib/fakeit/validation/validation_error.rb +6 -0
- data/lib/fakeit/validation/validator.rb +19 -0
- data/lib/fakeit/version.rb +3 -0
- metadata +219 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 981cca99f9c406f1d950151ec99a59c8301f35be503a1d987419561bab0bc431
|
4
|
+
data.tar.gz: fdafe0dc21b053068bb3f0abbc8556ecc8ebf73593b7fc3001fc5cd7774d97ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a5d23a302090b85a6e86f3e07da55c39bb2ce5f6cd18d10d0513046f19102c99c72dc21a6b5fccd924b4e6af12c2ab0685d2d708507a5798f6211f1b46be028
|
7
|
+
data.tar.gz: 97ee27c6dff7b3452b97713058ba5a6891f1d82fe852e12e1fe39e59d17b32052ef1b5493ef94dbf9609b687b89fd2fe9fe685ec1ebbd8f9b4a3830ae75863b5
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
build:
|
9
|
+
branches:
|
10
|
+
only:
|
11
|
+
- master
|
12
|
+
|
13
|
+
docker:
|
14
|
+
# specify the version you desire here
|
15
|
+
- image: circleci/ruby:2.6.2
|
16
|
+
|
17
|
+
# Specify service dependencies here if necessary
|
18
|
+
# CircleCI maintains a library of pre-built images
|
19
|
+
# documented at https://circleci.com/docs/2.0/circleci-images/
|
20
|
+
# - image: circleci/postgres:9.4
|
21
|
+
|
22
|
+
working_directory: ~/repo
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- checkout
|
26
|
+
|
27
|
+
# Download and cache dependencies
|
28
|
+
- restore_cache:
|
29
|
+
keys:
|
30
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
31
|
+
# fallback to using the latest cache if no exact match is found
|
32
|
+
- v1-dependencies-
|
33
|
+
|
34
|
+
- run:
|
35
|
+
name: install dependencies
|
36
|
+
command: |
|
37
|
+
gem install bundler
|
38
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
39
|
+
- save_cache:
|
40
|
+
paths:
|
41
|
+
- ./vendor/bundle
|
42
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
43
|
+
|
44
|
+
# run rubocop!
|
45
|
+
- run: bundle exec rake rubocop
|
46
|
+
|
47
|
+
# run rspec!
|
48
|
+
- run: bundle exec rake spec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/Documentation:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/FrozenStringLiteralComment:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/LineLength:
|
11
|
+
Max: 120
|
12
|
+
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Exclude:
|
15
|
+
- 'spec/**/*.rb'
|
16
|
+
|
17
|
+
Security/Open:
|
18
|
+
Enabled: false
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fakeit (0.1.1)
|
5
|
+
faker (~> 1.9)
|
6
|
+
openapi_parser (= 0.2.4)
|
7
|
+
rack (~> 2.0)
|
8
|
+
slop (~> 4.6)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
ast (2.4.0)
|
14
|
+
byebug (11.0.1)
|
15
|
+
concurrent-ruby (1.1.5)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
docile (1.3.1)
|
18
|
+
faker (1.9.3)
|
19
|
+
i18n (>= 0.7)
|
20
|
+
i18n (1.6.0)
|
21
|
+
concurrent-ruby (~> 1.0)
|
22
|
+
jaro_winkler (1.5.2)
|
23
|
+
json (2.2.0)
|
24
|
+
openapi_parser (0.2.4)
|
25
|
+
parallel (1.17.0)
|
26
|
+
parser (2.6.2.1)
|
27
|
+
ast (~> 2.4.0)
|
28
|
+
psych (3.1.0)
|
29
|
+
rack (2.0.7)
|
30
|
+
rack-test (1.1.0)
|
31
|
+
rack (>= 1.0, < 3)
|
32
|
+
rainbow (3.0.0)
|
33
|
+
rake (12.3.2)
|
34
|
+
rspec (3.8.0)
|
35
|
+
rspec-core (~> 3.8.0)
|
36
|
+
rspec-expectations (~> 3.8.0)
|
37
|
+
rspec-mocks (~> 3.8.0)
|
38
|
+
rspec-core (3.8.0)
|
39
|
+
rspec-support (~> 3.8.0)
|
40
|
+
rspec-expectations (3.8.2)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.8.0)
|
43
|
+
rspec-mocks (3.8.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.8.0)
|
46
|
+
rspec-support (3.8.0)
|
47
|
+
rubocop (0.67.2)
|
48
|
+
jaro_winkler (~> 1.5.1)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 2.5, != 2.5.1.1)
|
51
|
+
psych (>= 3.1.0)
|
52
|
+
rainbow (>= 2.2.2, < 4.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
55
|
+
ruby-progressbar (1.10.0)
|
56
|
+
simplecov (0.16.1)
|
57
|
+
docile (~> 1.1)
|
58
|
+
json (>= 1.8, < 3)
|
59
|
+
simplecov-html (~> 0.10.0)
|
60
|
+
simplecov-html (0.10.2)
|
61
|
+
slop (4.6.2)
|
62
|
+
unicode-display_width (1.5.0)
|
63
|
+
|
64
|
+
PLATFORMS
|
65
|
+
ruby
|
66
|
+
|
67
|
+
DEPENDENCIES
|
68
|
+
bundler (~> 2.0)
|
69
|
+
byebug (~> 11.0)
|
70
|
+
fakeit!
|
71
|
+
rack-test (~> 1.1)
|
72
|
+
rake (~> 12.0)
|
73
|
+
rspec (~> 3.0)
|
74
|
+
rubocop (~> 0.67)
|
75
|
+
simplecov (~> 0.16)
|
76
|
+
|
77
|
+
BUNDLED WITH
|
78
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Justin Feng
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Fakeit
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/JustinFeng/fakeit.svg?style=svg)](https://circleci.com/gh/JustinFeng/fakeit)
|
4
|
+
|
5
|
+
Create mock server from Openapi specification
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
* Randomly generated response
|
10
|
+
* Request validation
|
11
|
+
* Load specification from local or remote
|
12
|
+
* Support specification in yaml or json format
|
13
|
+
|
14
|
+
**Note:** Only support json content type as of now
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'fakeit'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install fakeit
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
$ fakeit --spec http://url/to/spec
|
35
|
+
|
36
|
+
Command line options:
|
37
|
+
|
38
|
+
$ fakeit --help
|
39
|
+
usage:
|
40
|
+
--spec spec file uri (required)
|
41
|
+
-p, --port custom port
|
42
|
+
-q, --quiet mute request and response log
|
43
|
+
|
44
|
+
other options:
|
45
|
+
-v, --version
|
46
|
+
-h, --help
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake` to run the tests.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
53
|
+
|
54
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/JustinFeng/fakeit.
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/fakeit
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'slop'
|
4
|
+
require 'fakeit'
|
5
|
+
|
6
|
+
usage = nil
|
7
|
+
begin
|
8
|
+
opts = Slop.parse do |o|
|
9
|
+
o.string '--spec', 'spec file uri (required)', required: true
|
10
|
+
o.integer '-p', '--port', 'custom port'
|
11
|
+
o.bool '-q', '--quiet', 'mute request and response log'
|
12
|
+
o.separator ''
|
13
|
+
o.separator 'other options:'
|
14
|
+
o.on '-v', '--version' do
|
15
|
+
puts Fakeit::VERSION
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
o.on '-h', '--help' do
|
19
|
+
puts o
|
20
|
+
exit
|
21
|
+
end
|
22
|
+
|
23
|
+
usage = o
|
24
|
+
end
|
25
|
+
rescue Slop::Error => e
|
26
|
+
puts e.message
|
27
|
+
puts usage
|
28
|
+
exit
|
29
|
+
end
|
30
|
+
|
31
|
+
server = Rack::Handler::WEBrick
|
32
|
+
|
33
|
+
trap(:INT) do
|
34
|
+
if server.respond_to?(:shutdown)
|
35
|
+
server.shutdown
|
36
|
+
else
|
37
|
+
exit
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
app = Fakeit.build(opts[:spec])
|
42
|
+
app.use(Fakeit::Middleware::Logger) unless opts[:quiet]
|
43
|
+
server.run(app, Port: opts[:port], Host: '0.0.0.0')
|
data/fakeit.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'fakeit/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'fakeit'
|
7
|
+
spec.version = Fakeit::VERSION
|
8
|
+
spec.authors = ['Justin Feng']
|
9
|
+
spec.email = ['realfengjia@foxmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Create mock server from Openapi specification'
|
12
|
+
spec.description = 'Create mock server from Openapi specification'
|
13
|
+
spec.homepage = 'https://github.com/JustinFeng/fakeit'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = 'bin'
|
22
|
+
spec.executables = 'fakeit'
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
26
|
+
spec.add_development_dependency 'byebug', '~> 11.0'
|
27
|
+
spec.add_development_dependency 'rack-test', '~> 1.1'
|
28
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'rubocop', '~> 0.67'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
32
|
+
|
33
|
+
spec.add_dependency 'faker', '~> 1.9'
|
34
|
+
spec.add_dependency 'openapi_parser', '0.2.4'
|
35
|
+
spec.add_dependency 'rack', '~> 2.0'
|
36
|
+
spec.add_dependency 'slop', '~> 4.6'
|
37
|
+
end
|
data/lib/fakeit.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'yaml'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'openapi_parser'
|
5
|
+
require 'faker'
|
6
|
+
require 'rack'
|
7
|
+
|
8
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'fakeit', '/**/*.rb')).each { |file| require file }
|
9
|
+
|
10
|
+
module Fakeit
|
11
|
+
class << self
|
12
|
+
def build(spec_file)
|
13
|
+
Rack::Builder.new do
|
14
|
+
run App.create(spec_file)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Fakeit
|
2
|
+
module App
|
3
|
+
class << self
|
4
|
+
def create(spec_file)
|
5
|
+
specification = Fakeit::Openapi.load(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)
|
11
|
+
&.tap { |operation| validate(operation, request) }
|
12
|
+
.then(&method(:rack_response))
|
13
|
+
rescue Fakeit::Validation::ValidationError => e
|
14
|
+
error_response(e)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def error_response(err)
|
21
|
+
{ message: err.message }
|
22
|
+
.to_json
|
23
|
+
.then { |message| [418, { 'Content-Type' => 'application/json' }, [message]] }
|
24
|
+
end
|
25
|
+
|
26
|
+
def rack_response(operation)
|
27
|
+
if operation
|
28
|
+
[operation.status, operation.headers, [operation.body]]
|
29
|
+
else
|
30
|
+
[404, {}, ['Not Found']]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate(operation, request)
|
35
|
+
operation.validate(
|
36
|
+
body: request.body&.read.to_s,
|
37
|
+
params: request.params,
|
38
|
+
headers: headers(request)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def headers(request)
|
43
|
+
request
|
44
|
+
.each_header
|
45
|
+
.select { |k, _| k.start_with? 'HTTP_' }
|
46
|
+
.map { |k, v| [k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-'), v] }
|
47
|
+
.to_h
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module OpenAPIParser
|
2
|
+
module Schemas
|
3
|
+
class Schema
|
4
|
+
BIG_INT = 2**32
|
5
|
+
|
6
|
+
def to_example
|
7
|
+
case type
|
8
|
+
when 'string' then string_example
|
9
|
+
when 'integer' then integer_example
|
10
|
+
when 'number' then Faker::Number.decimal.to_f
|
11
|
+
when 'boolean' then Faker::Boolean.boolean
|
12
|
+
when 'array' then [items.to_example]
|
13
|
+
else # object
|
14
|
+
properties.each_with_object({}) { |(name, schema), obj| obj[name] = schema.to_example }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def integer_example
|
21
|
+
if enum
|
22
|
+
enum.to_a.sample
|
23
|
+
else
|
24
|
+
Faker::Number.between(minimum || 1, maximum || BIG_INT)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def string_example
|
29
|
+
if enum
|
30
|
+
enum.to_a.sample
|
31
|
+
elsif pattern
|
32
|
+
Faker::Base.regexify(pattern)
|
33
|
+
else
|
34
|
+
Faker::Book.title
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Fakeit
|
2
|
+
module Middleware
|
3
|
+
class Logger
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
env
|
10
|
+
.tap(&method(:log_request))
|
11
|
+
.then { |e| @app.call(e) }
|
12
|
+
.tap(&method(:log_response))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def log_request(env)
|
18
|
+
env['rack.input']
|
19
|
+
&.tap { |body| puts "Request body: #{body.read}" }
|
20
|
+
&.tap { |body| body.rewind }
|
21
|
+
end
|
22
|
+
|
23
|
+
def log_response(response)
|
24
|
+
puts "Response body: #{response[2].first}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Fakeit
|
2
|
+
module Openapi
|
3
|
+
class << self
|
4
|
+
def load(src)
|
5
|
+
parse_method = parse_method(src)
|
6
|
+
|
7
|
+
open(src, &:read)
|
8
|
+
.then(&parse_method)
|
9
|
+
.then(&OpenAPIParser.method(:parse))
|
10
|
+
.then(&Specification.method(:new))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def parse_method(src)
|
16
|
+
case File.extname(src)
|
17
|
+
when '.json' then
|
18
|
+
JSON.method(:parse)
|
19
|
+
when '.yml' then
|
20
|
+
YAML.method(:safe_load)
|
21
|
+
else
|
22
|
+
raise 'Invalid openapi specification file'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Fakeit
|
2
|
+
module Openapi
|
3
|
+
class Operation
|
4
|
+
def initialize(request_operation)
|
5
|
+
@request_operation = request_operation
|
6
|
+
@validator = Fakeit::Validation::Validator.new(request_operation)
|
7
|
+
end
|
8
|
+
|
9
|
+
def status
|
10
|
+
openapi_response.first.to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
def headers
|
14
|
+
openapi_headers
|
15
|
+
&.map { |k, v| [k, v.schema.to_example] }
|
16
|
+
&.push(['Content-Type', openapi_content_type])
|
17
|
+
.to_h
|
18
|
+
end
|
19
|
+
|
20
|
+
def body
|
21
|
+
openapi_schema&.schema&.to_example&.then(&JSON.method(:generate)).to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate(**request_parts)
|
25
|
+
@validator.validate(request_parts)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def openapi_content
|
31
|
+
openapi_response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
|
32
|
+
end
|
33
|
+
|
34
|
+
def openapi_schema
|
35
|
+
openapi_content&.last
|
36
|
+
end
|
37
|
+
|
38
|
+
def openapi_content_type
|
39
|
+
openapi_content&.first
|
40
|
+
end
|
41
|
+
|
42
|
+
def openapi_headers
|
43
|
+
openapi_response.last.headers
|
44
|
+
end
|
45
|
+
|
46
|
+
def openapi_response
|
47
|
+
@request_operation.operation_object.responses.response.min
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Fakeit
|
2
|
+
module Validation
|
3
|
+
class Validator
|
4
|
+
def initialize(operation)
|
5
|
+
@operation = operation
|
6
|
+
end
|
7
|
+
|
8
|
+
def validate(body: '', params: {}, headers: {})
|
9
|
+
options = OpenAPIParser::SchemaValidator::Options.new(coerce_value: true)
|
10
|
+
|
11
|
+
@operation.validate_request_body('application/json', JSON.parse(body)) unless body.empty?
|
12
|
+
@operation.validate_path_params(options)
|
13
|
+
@operation.validate_request_parameter(params, headers, options)
|
14
|
+
rescue StandardError => e
|
15
|
+
raise ValidationError, e.message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fakeit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Feng
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '11.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack-test
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.67'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.67'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.16'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.16'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: faker
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.9'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.9'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: openapi_parser
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.2.4
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.2.4
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rack
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '2.0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '2.0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: slop
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '4.6'
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '4.6'
|
167
|
+
description: Create mock server from Openapi specification
|
168
|
+
email:
|
169
|
+
- realfengjia@foxmail.com
|
170
|
+
executables:
|
171
|
+
- fakeit
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- ".circleci/config.yml"
|
176
|
+
- ".gitignore"
|
177
|
+
- ".rspec"
|
178
|
+
- ".rubocop.yml"
|
179
|
+
- Gemfile
|
180
|
+
- Gemfile.lock
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.md
|
183
|
+
- Rakefile
|
184
|
+
- bin/fakeit
|
185
|
+
- fakeit.gemspec
|
186
|
+
- lib/fakeit.rb
|
187
|
+
- lib/fakeit/app/app.rb
|
188
|
+
- lib/fakeit/core_extensions/schema.rb
|
189
|
+
- lib/fakeit/middleware/logger.rb
|
190
|
+
- lib/fakeit/openapi/loader.rb
|
191
|
+
- lib/fakeit/openapi/operation.rb
|
192
|
+
- lib/fakeit/openapi/specification.rb
|
193
|
+
- lib/fakeit/validation/validation_error.rb
|
194
|
+
- lib/fakeit/validation/validator.rb
|
195
|
+
- lib/fakeit/version.rb
|
196
|
+
homepage: https://github.com/JustinFeng/fakeit
|
197
|
+
licenses:
|
198
|
+
- MIT
|
199
|
+
metadata: {}
|
200
|
+
post_install_message:
|
201
|
+
rdoc_options: []
|
202
|
+
require_paths:
|
203
|
+
- lib
|
204
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
209
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
210
|
+
requirements:
|
211
|
+
- - ">="
|
212
|
+
- !ruby/object:Gem::Version
|
213
|
+
version: '0'
|
214
|
+
requirements: []
|
215
|
+
rubygems_version: 3.0.3
|
216
|
+
signing_key:
|
217
|
+
specification_version: 4
|
218
|
+
summary: Create mock server from Openapi specification
|
219
|
+
test_files: []
|