fakeit 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35c58ad2d52ce6f2ba2375738faaed686b7945ce0d3e438d0d5e3e78f7d32d71
4
- data.tar.gz: 4c9a1566b7256b2b5b8fb7772daf1329810644b646f2ac0c939d5a3fd824a8db
3
+ metadata.gz: 60b523cb9a2c8cea3454b35cace650d51f2bf8e2ed8e53e07b0f35ffe45c9288
4
+ data.tar.gz: a3340dafc8607d5de8e9cb7f00c9b60679e7f84bdd2161f8d8607dad2e6089b3
5
5
  SHA512:
6
- metadata.gz: 0c3b0375ac956484083b108bb3284ccd33adda44e002b310b7e44fdb2ce151f1b62729a095f9caa02d2953d7dd809e4144fa33612756aa3189cee358e279ae18
7
- data.tar.gz: 71e3391f4570e12b3c940476e0657e700088ef72f122ccf6e354e71e43ed47ef3d082e7d7f972fc5b35c3fb111aba38110b00d36725eaaafbaeb18750d6ebc96
6
+ metadata.gz: cfab31deab5494ae69b2adeecec85efff46d69104ad71ce0d637e4809a492bb7ac45062d8a73e5cd4adb34ad347ac798fecd05f4a1b3b00bab37a3b44adab511
7
+ data.tar.gz: c4ef907db3d2df3de0fed6bf5a54b904969b080f312951665b6cfe863c4c10ecf2f3d1e7bf5b712d668878aa2b9b3b1dda7539da927ac9cabea005fec2528509
@@ -0,0 +1,151 @@
1
+ # Changelog
2
+
3
+ ## [v0.6.3](https://github.com/JustinFeng/fakeit/tree/v0.6.3) (2020-09-11)
4
+
5
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.6.2...v0.6.3)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Enhanced regex example generation
10
+
11
+ ## [v0.6.2](https://github.com/JustinFeng/fakeit/tree/v0.6.2) (2020-07-12)
12
+
13
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.6.1...v0.6.2)
14
+
15
+ ## [v0.6.1](https://github.com/JustinFeng/fakeit/tree/v0.6.1) (2020-05-02)
16
+
17
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.6.0...v0.6.1)
18
+
19
+ ## [v0.6.0](https://github.com/JustinFeng/fakeit/tree/v0.6.0) (2020-05-02)
20
+
21
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.5.3...v0.6.0)
22
+
23
+ **Implemented enhancements:**
24
+
25
+ - Added support for multipart/form-data validation
26
+
27
+ **Fixed bugs:**
28
+
29
+ - Multipart request contain special character will respond 500 [\#16](https://github.com/JustinFeng/fakeit/issues/16)
30
+
31
+ ## [v0.5.3](https://github.com/JustinFeng/fakeit/tree/v0.5.3) (2020-04-05)
32
+
33
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.5.2...v0.5.3)
34
+
35
+ ## [v0.5.2](https://github.com/JustinFeng/fakeit/tree/v0.5.2) (2020-04-03)
36
+
37
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.5.1...v0.5.2)
38
+
39
+ ## [v0.5.1](https://github.com/JustinFeng/fakeit/tree/v0.5.1) (2020-04-03)
40
+
41
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.5.0...v0.5.1)
42
+
43
+ ## [v0.5.0](https://github.com/JustinFeng/fakeit/tree/v0.5.0) (2020-04-03)
44
+
45
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.4.2...v0.5.0)
46
+
47
+ **Merged pull requests:**
48
+
49
+ - Bump rack from 2.0.7 to 2.0.8 [\#18](https://github.com/JustinFeng/fakeit/pull/18) ([dependabot[bot]](https://github.com/apps/dependabot))
50
+
51
+ ## [v0.4.2](https://github.com/JustinFeng/fakeit/tree/v0.4.2) (2019-11-22)
52
+
53
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.4.1...v0.4.2)
54
+
55
+ ## [v0.4.1](https://github.com/JustinFeng/fakeit/tree/v0.4.1) (2019-11-21)
56
+
57
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.4.0...v0.4.1)
58
+
59
+ **Merged pull requests:**
60
+
61
+ - Bump rack-cors from 1.0.3 to 1.0.6 [\#17](https://github.com/JustinFeng/fakeit/pull/17) ([dependabot[bot]](https://github.com/apps/dependabot))
62
+
63
+ ## [v0.4.0](https://github.com/JustinFeng/fakeit/tree/v0.4.0) (2019-10-09)
64
+
65
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.3.1...v0.4.0)
66
+
67
+ **Implemented enhancements:**
68
+
69
+ - Implement support for watching for changes in spec/yaml file. [\#15](https://github.com/JustinFeng/fakeit/issues/15)
70
+
71
+ ## [v0.3.1](https://github.com/JustinFeng/fakeit/tree/v0.3.1) (2019-09-01)
72
+
73
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.3.0...v0.3.1)
74
+
75
+ ## [v0.3.0](https://github.com/JustinFeng/fakeit/tree/v0.3.0) (2019-08-27)
76
+
77
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.2.1...v0.3.0)
78
+
79
+ **Closed issues:**
80
+
81
+ - Missing "motivation" section in readme [\#14](https://github.com/JustinFeng/fakeit/issues/14)
82
+
83
+ ## [v0.2.1](https://github.com/JustinFeng/fakeit/tree/v0.2.1) (2019-08-04)
84
+
85
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.2.0...v0.2.1)
86
+
87
+ **Implemented enhancements:**
88
+
89
+ - Support .yaml files [\#9](https://github.com/JustinFeng/fakeit/issues/9)
90
+
91
+ **Fixed bugs:**
92
+
93
+ - Adjust integer generation bounds [\#12](https://github.com/JustinFeng/fakeit/issues/12)
94
+
95
+ **Closed issues:**
96
+
97
+ - End to End Tests [\#8](https://github.com/JustinFeng/fakeit/issues/8)
98
+
99
+ **Merged pull requests:**
100
+
101
+ - support .yaml files [\#10](https://github.com/JustinFeng/fakeit/pull/10) ([monsterkrampe](https://github.com/monsterkrampe))
102
+
103
+ ## [v0.2.0](https://github.com/JustinFeng/fakeit/tree/v0.2.0) (2019-07-17)
104
+
105
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.1.5...v0.2.0)
106
+
107
+ **Implemented enhancements:**
108
+
109
+ - Generate more array elements with performance considered [\#7](https://github.com/JustinFeng/fakeit/issues/7)
110
+ - Non random response [\#5](https://github.com/JustinFeng/fakeit/issues/5)
111
+
112
+ **Fixed bugs:**
113
+
114
+ - Invalid example for number type with more than 2 decimal places 'multipleOf' [\#6](https://github.com/JustinFeng/fakeit/issues/6)
115
+
116
+ ## [v0.1.5](https://github.com/JustinFeng/fakeit/tree/v0.1.5) (2019-06-28)
117
+
118
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.1.4...v0.1.5)
119
+
120
+ **Implemented enhancements:**
121
+
122
+ - Allow CORS [\#4](https://github.com/JustinFeng/fakeit/pull/4) ([monsterkrampe](https://github.com/monsterkrampe))
123
+
124
+ ## [v0.1.4](https://github.com/JustinFeng/fakeit/tree/v0.1.4) (2019-06-25)
125
+
126
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.1.3...v0.1.4)
127
+
128
+ **Implemented enhancements:**
129
+
130
+ - W, \[2019-06-25T03:00:29.635993 \#1\] WARN -- : Unknown string format: guid [\#2](https://github.com/JustinFeng/fakeit/issues/2)
131
+
132
+ **Fixed bugs:**
133
+
134
+ - Request not completing [\#3](https://github.com/JustinFeng/fakeit/issues/3)
135
+ - Stack level too deep in docker container [\#1](https://github.com/JustinFeng/fakeit/issues/1)
136
+
137
+ ## [v0.1.3](https://github.com/JustinFeng/fakeit/tree/v0.1.3) (2019-06-16)
138
+
139
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.1.2...v0.1.3)
140
+
141
+ ## [v0.1.2](https://github.com/JustinFeng/fakeit/tree/v0.1.2) (2019-06-05)
142
+
143
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.1.1...v0.1.2)
144
+
145
+ ## [v0.1.1](https://github.com/JustinFeng/fakeit/tree/v0.1.1) (2019-05-26)
146
+
147
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/4f94874eaf87300496e9de632cd659a031cb3ce2...v0.1.1)
148
+
149
+
150
+
151
+ \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fakeit (0.6.3)
4
+ fakeit (0.7.0)
5
5
  faker (= 2.13.0)
6
6
  openapi_parser (= 0.12.1)
7
7
  rack (~> 2.0)
@@ -82,4 +82,4 @@ DEPENDENCIES
82
82
  simplecov (~> 0.18)
83
83
 
84
84
  BUNDLED WITH
85
- 2.1.2
85
+ 2.1.4
data/README.md CHANGED
@@ -48,13 +48,14 @@ Or use the [docker image](https://hub.docker.com/r/realfengjia/fakeit)
48
48
 
49
49
  $ fakeit --spec <Local file or remote url>
50
50
 
51
- Command line options:
51
+ ### Command line options
52
52
 
53
53
  $ fakeit --help
54
54
  usage:
55
55
  --spec spec file uri (required)
56
56
  -p, --port custom port
57
57
  -q, --quiet mute request and response log
58
+ -l, --log-file redirect log to a file
58
59
  --permissive log validation error as warning instead of denying request
59
60
  --use-example use example provided in spec if exists
60
61
  --static generate static response
@@ -65,6 +66,29 @@ Command line options:
65
66
  -v, --version
66
67
  -h, --help
67
68
 
69
+ ### Configuration endpoint
70
+
71
+ Mock server behaviour can be changed on the fly
72
+
73
+ Retrieve current config:
74
+
75
+ GET /__fakeit_config__
76
+
77
+ Update config:
78
+
79
+ PUT /__fakeit_config__
80
+
81
+ Request and response:
82
+ ```json
83
+ {
84
+ 'permissive' => false,
85
+ 'use_example' => true,
86
+ 'static' => false,
87
+ 'static_types' => ['integer'],
88
+ 'static_properties' => ['id']
89
+ }
90
+ ```
91
+
68
92
  **Notes:**
69
93
  * See [here](docs/random.md) for Openapi properties supported in random response generation
70
94
  * See [here](docs/static.md) for default value in static response generation
data/bin/fakeit CHANGED
@@ -10,6 +10,7 @@ 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.string '-l', '--log-file', 'redirect log to a file'
13
14
  o.bool '--permissive', 'log validation error as warning instead of denying request'
14
15
  o.bool '--use-example', 'use example provided in spec if exists'
15
16
  o.bool '--static', 'generate static response'
@@ -53,6 +54,9 @@ options = Fakeit::App::Options.new(
53
54
  )
54
55
  app = Fakeit.build(opts[:spec], options)
55
56
 
57
+ $stderr.reopen(File.new(opts[:log_file], File::WRONLY | File::TRUNC | File::CREAT)) if opts[:log_file]
58
+ $stderr.sync = true
59
+
56
60
  app.use Rack::Cors do
57
61
  allow do
58
62
  origins '*'
@@ -15,7 +15,7 @@ module Fakeit
15
15
  class << self
16
16
  def build(spec_file, options)
17
17
  Rack::Builder.new do
18
- run App.create(spec_file, options)
18
+ run App::AppBuilder.new(spec_file, options).build
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,23 @@
1
+ module Fakeit
2
+ module App
3
+ class AppBuilder
4
+ def initialize(spec_file, options)
5
+ @config_route = Routes::ConfigRoute.new(options)
6
+ @openapi_route = Routes::OpenapiRoute.new(spec_file)
7
+ end
8
+
9
+ def build
10
+ proc do |env|
11
+ request = Rack::Request.new(env)
12
+
13
+ case request.path_info
14
+ when '/__fakeit_config__'
15
+ @config_route.call(request)
16
+ else
17
+ @openapi_route.call(request, @config_route.options)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Fakeit
2
+ module App
3
+ module Helpers
4
+ class BodyParser
5
+ class << self
6
+ def parse(request)
7
+ case request.media_type
8
+ when %r{^application/.*json}
9
+ { media_type: request.media_type, data: parse_json(request.body.read) }
10
+ when 'multipart/form-data'
11
+ { media_type: request.media_type, data: parse_form_data(request.params) }
12
+ else
13
+ { media_type: request.media_type, data: request.body.read }
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def parse_json(body)
20
+ body.empty? ? {} : JSON.parse(body)
21
+ rescue StandardError
22
+ raise Fakeit::Validation::ValidationError, 'Invalid json payload'
23
+ end
24
+
25
+ def parse_form_data(params)
26
+ params.transform_values { |v| v.class == Hash && v[:tempfile] ? v[:tempfile].read : v }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module Fakeit
2
+ module App
3
+ module Helpers
4
+ class ResponseBuilder
5
+ class << self
6
+ def error(code, err)
7
+ [code, { 'Content-Type' => 'application/json' }, [{ message: err.message }.to_json]]
8
+ end
9
+
10
+ def not_found
11
+ [404, {}, ['Not Found']]
12
+ end
13
+
14
+ def method_not_allowed
15
+ [405, {}, ['Method Not Allowed']]
16
+ end
17
+
18
+ def unsupported_media_type
19
+ [415, {}, ['Unsupported Media Type']]
20
+ end
21
+
22
+ def ok(body)
23
+ [200, { 'Content-Type' => 'application/json' }, [body.to_json]]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -14,6 +14,16 @@ module Fakeit
14
14
  def use_static?(type: nil, property: nil)
15
15
  @static || @static_types.include?(type) || @static_properties.include?(property)
16
16
  end
17
+
18
+ def to_hash
19
+ {
20
+ permissive: @permissive,
21
+ use_example: @use_example,
22
+ static: @static,
23
+ static_types: @static_types,
24
+ static_properties: @static_properties
25
+ }
26
+ end
17
27
  end
18
28
  end
19
29
  end
@@ -0,0 +1,38 @@
1
+ module Fakeit
2
+ module App
3
+ module Routes
4
+ class ConfigRoute
5
+ attr_reader :options
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ end
10
+
11
+ def call(request)
12
+ case [request.request_method, request.media_type]
13
+ in ['GET', _]
14
+ Fakeit::App::Helpers::ResponseBuilder.ok(@options.to_hash)
15
+ in ['PUT', 'application/json']
16
+ update(request)
17
+ in ['PUT', _]
18
+ Fakeit::App::Helpers::ResponseBuilder.unsupported_media_type
19
+ else
20
+ Fakeit::App::Helpers::ResponseBuilder.method_not_allowed
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def update(request)
27
+ body = Fakeit::App::Helpers::BodyParser.parse(request)[:data]
28
+ @options = Fakeit::App::Options.new(**body.transform_keys(&:to_sym))
29
+
30
+ Fakeit::App::Helpers::ResponseBuilder.ok(@options.to_hash)
31
+ rescue ArgumentError => e
32
+ Logger.warn(Rainbow(e.message).red)
33
+ Fakeit::App::Helpers::ResponseBuilder.error(422, e)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,56 @@
1
+ module Fakeit
2
+ module App
3
+ module Routes
4
+ class OpenapiRoute
5
+ def initialize(spec_file)
6
+ @specification = Fakeit::Openapi::Specification.new(spec_file)
7
+ end
8
+
9
+ def call(request, options)
10
+ @specification
11
+ .operation(request.request_method.downcase.to_sym, request.path_info, options)
12
+ .then { _1 ? handle(_1, request, options) : Fakeit::App::Helpers::ResponseBuilder.not_found }
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
+ Logger.warn(Rainbow(e.message).red)
22
+ options.permissive ? response(operation) : Fakeit::App::Helpers::ResponseBuilder.error(418, e)
23
+ end
24
+
25
+ def response(operation)
26
+ [operation.status, operation.headers, [operation.body]]
27
+ end
28
+
29
+ def validate(operation, request)
30
+ operation.validate(
31
+ body: Helpers::BodyParser.parse(request),
32
+ params: parse_query(request.query_string),
33
+ headers: headers(request)
34
+ )
35
+ end
36
+
37
+ def headers(request)
38
+ request
39
+ .each_header
40
+ .select { |k, _| k.start_with? 'HTTP_' }
41
+ .map { |k, v| [k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-'), v] }
42
+ .to_h
43
+ end
44
+
45
+ def parse_query(query_string)
46
+ rack_query = Rack::Utils.parse_nested_query(query_string)
47
+ cgi_query = CGI.parse(query_string)
48
+
49
+ rack_query.merge(cgi_query.slice(*rack_query.keys)) do |_, oldval, newval|
50
+ newval.is_a?(Array) && newval.size > 1 ? newval : oldval
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,4 +1,3 @@
1
1
  module Fakeit
2
- $stdout.sync = true
3
- Logger = ::Logger.new($stdout)
2
+ Logger = ::Logger.new($stderr)
4
3
  end
@@ -104,7 +104,7 @@ module Fakeit
104
104
  end
105
105
 
106
106
  def unknown_format
107
- Fakeit::Logger.info("Unknown string format: #{format}")
107
+ Logger.info("Unknown string format: #{format}")
108
108
  'Unknown string format'
109
109
  end
110
110
  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,3 +1,3 @@
1
1
  module Fakeit
2
- VERSION = '0.6.3'.freeze
2
+ VERSION = '0.7.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.6.3
4
+ version: 0.7.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-09-11 00:00:00.000000000 Z
11
+ date: 2020-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -218,6 +218,7 @@ files:
218
218
  - ".gitignore"
219
219
  - ".rspec"
220
220
  - ".rubocop.yml"
221
+ - CHANGELOG.md
221
222
  - Gemfile
222
223
  - Gemfile.lock
223
224
  - LICENSE.txt
@@ -228,9 +229,12 @@ files:
228
229
  - docs/static.md
229
230
  - fakeit.gemspec
230
231
  - lib/fakeit.rb
231
- - lib/fakeit/app/app.rb
232
- - lib/fakeit/app/body_parser.rb
232
+ - lib/fakeit/app/app_builder.rb
233
+ - lib/fakeit/app/helpers/body_parser.rb
234
+ - lib/fakeit/app/helpers/response_builder.rb
233
235
  - lib/fakeit/app/options.rb
236
+ - lib/fakeit/app/routes/config_route.rb
237
+ - lib/fakeit/app/routes/openapi_route.rb
234
238
  - lib/fakeit/core_extensions/schema.rb
235
239
  - lib/fakeit/logger.rb
236
240
  - lib/fakeit/middleware/recorder.rb
@@ -266,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
266
270
  - !ruby/object:Gem::Version
267
271
  version: '0'
268
272
  requirements: []
269
- rubygems_version: 3.1.2
273
+ rubygems_version: 3.1.4
270
274
  signing_key:
271
275
  specification_version: 4
272
276
  summary: Create mock server from Openapi specification
@@ -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