rspec-rails-api 0.3.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -5
- data/.gitlab-ci.yml +1 -1
- data/.rubocop.yml +5 -1
- data/.ruby-version +1 -0
- data/CHANGELOG.md +51 -0
- data/Gemfile.lock +98 -0
- data/README.md +39 -36
- data/Rakefile +3 -0
- data/lib/rspec/rails/api/dsl/example.rb +94 -18
- data/lib/rspec/rails/api/dsl/example_group.rb +134 -48
- data/lib/rspec/rails/api/entity_config.rb +28 -4
- data/lib/rspec/rails/api/field_config.rb +10 -2
- data/lib/rspec/rails/api/matchers.rb +26 -18
- data/lib/rspec/rails/api/metadata.rb +153 -21
- data/lib/rspec/rails/api/open_api_renderer.rb +189 -20
- data/lib/rspec/rails/api/utils.rb +28 -70
- data/lib/rspec/rails/api/validator.rb +211 -0
- data/lib/rspec/rails/api/version.rb +1 -1
- data/lib/rspec_rails_api.rb +4 -0
- data/rspec-rails-api.gemspec +10 -5
- metadata +68 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 232e1ea8b43dd1e3b5258f0029d28a54d886156634e87d4a6854a3543752fb0f
|
4
|
+
data.tar.gz: 71828ab75d08ff900cbe9f018af219bc79b98231118712d127e35c274690d7ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbb37718158f52e0a2056bbe9b8ccde44782cafe915cc146f05333532484b10ae8260e7abbd3f571e657c778b8d8ac37c731adfa452d3e94af253dcf6849aede
|
7
|
+
data.tar.gz: 731f623490f7e15b3f0acb8851955835083249b60e62f722ab9870c6658d3e690ed0b6aca8ac4edcdc3f6b1c892517511937d190459b8020fcb2b3c2ef99047a
|
data/.gitignore
CHANGED
data/.gitlab-ci.yml
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
---
|
2
2
|
require:
|
3
3
|
- rubocop-performance
|
4
|
+
- rubocop-rake
|
5
|
+
- rubocop-rspec
|
4
6
|
|
5
7
|
AllCops:
|
6
|
-
TargetRubyVersion: 2.
|
8
|
+
TargetRubyVersion: 2.7
|
7
9
|
Exclude:
|
8
10
|
- dummy/**/*
|
9
11
|
- vendor/bundle/**/*
|
@@ -33,3 +35,5 @@ Style/TrailingCommaInArrayLiteral:
|
|
33
35
|
Style/TrailingCommaInHashLiteral:
|
34
36
|
EnforcedStyleForMultiline: comma
|
35
37
|
|
38
|
+
RSpec/NestedGroups:
|
39
|
+
Max: 4
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.0
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,57 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
4
|
## Not released
|
5
|
+
|
6
|
+
## 0.5.0 - 2023-01-02
|
7
|
+
|
8
|
+
- Improved error messages
|
9
|
+
- Improved usage of primitive types: use `:string` instead of `:type_string`, and more generally, remove the `type_`
|
10
|
+
prefix
|
11
|
+
- Fixed an error when an object is defined with an attribute named `type`.
|
12
|
+
|
13
|
+
## 0.4.0 - 2021-12-19
|
14
|
+
|
15
|
+
- All parameters attributes are considered required unless specified
|
16
|
+
- Fix object `attributes` key in spec and documentation.
|
17
|
+
When defining object attributes, documentation and tests used `properties` key
|
18
|
+
while the code was waiting for an `attributes` key. The later makes more sense
|
19
|
+
so the spec and documentation were fixed.
|
20
|
+
- Check for arrays of primitives is now a thing:
|
21
|
+
```rb
|
22
|
+
# In entities, when attribute is an array of primitives
|
23
|
+
entity :user,
|
24
|
+
aliases: { type: :array, description: 'Pseudonyms', of: :type_string }
|
25
|
+
|
26
|
+
# In examples, when response is an array of primitives
|
27
|
+
for_code 200, expect_many: :type_string do |url|
|
28
|
+
#...
|
29
|
+
end
|
30
|
+
```
|
31
|
+
- RSpec metadata is now stored in `rra` instead of `rrad` (the gem's first name
|
32
|
+
was RSpec Rails API Doc at the time). Update RSpec configuration accordingly.
|
33
|
+
- OpenApi:
|
34
|
+
- Responses now includes the schema
|
35
|
+
- `on_xxx` methods second parameter is now used as summary instead of description.
|
36
|
+
Description can be defined on the third parameter.
|
37
|
+
- DSL changes:
|
38
|
+
- `visit` is renamed to `test_response_of`
|
39
|
+
- Support for `doc_only` is removed
|
40
|
+
- Response expectations _should_ now be declared with `for_code`, and should be
|
41
|
+
removed from example bodies:
|
42
|
+
```rb
|
43
|
+
# Before:
|
44
|
+
for_code 200 do |url|
|
45
|
+
visit url
|
46
|
+
expect(response).to have_many defined: :post
|
47
|
+
end
|
48
|
+
|
49
|
+
# Now:
|
50
|
+
for_code 200, expect_many: :post do |url|
|
51
|
+
test_response_of url
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
|
5
56
|
## 0.3.4 - 2021-10-20
|
6
57
|
- Add the "required" attribute in parameters
|
7
58
|
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rspec-rails-api (0.5.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (6.1.7)
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
|
+
i18n (>= 1.6, < 2)
|
12
|
+
minitest (>= 5.1)
|
13
|
+
tzinfo (~> 2.0)
|
14
|
+
zeitwerk (~> 2.3)
|
15
|
+
ast (2.4.2)
|
16
|
+
byebug (11.1.3)
|
17
|
+
concurrent-ruby (1.1.10)
|
18
|
+
diff-lcs (1.5.0)
|
19
|
+
docile (1.4.0)
|
20
|
+
i18n (1.12.0)
|
21
|
+
concurrent-ruby (~> 1.0)
|
22
|
+
json (2.6.3)
|
23
|
+
minitest (5.16.3)
|
24
|
+
parallel (1.22.1)
|
25
|
+
parser (3.1.3.0)
|
26
|
+
ast (~> 2.4.1)
|
27
|
+
rack (3.0.3)
|
28
|
+
rainbow (3.1.1)
|
29
|
+
rake (10.5.0)
|
30
|
+
regexp_parser (2.6.1)
|
31
|
+
rexml (3.2.5)
|
32
|
+
rspec (3.12.0)
|
33
|
+
rspec-core (~> 3.12.0)
|
34
|
+
rspec-expectations (~> 3.12.0)
|
35
|
+
rspec-mocks (~> 3.12.0)
|
36
|
+
rspec-core (3.12.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-expectations (3.12.1)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.12.0)
|
41
|
+
rspec-mocks (3.12.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.12.0)
|
44
|
+
rspec-support (3.12.0)
|
45
|
+
rubocop (1.41.1)
|
46
|
+
json (~> 2.3)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 3.1.2.1)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
regexp_parser (>= 1.8, < 3.0)
|
51
|
+
rexml (>= 3.2.5, < 4.0)
|
52
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
55
|
+
rubocop-ast (1.24.1)
|
56
|
+
parser (>= 3.1.1.0)
|
57
|
+
rubocop-performance (1.15.2)
|
58
|
+
rubocop (>= 1.7.0, < 2.0)
|
59
|
+
rubocop-ast (>= 0.4.0)
|
60
|
+
rubocop-rake (0.6.0)
|
61
|
+
rubocop (~> 1.0)
|
62
|
+
rubocop-rspec (2.16.0)
|
63
|
+
rubocop (~> 1.33)
|
64
|
+
ruby-progressbar (1.11.0)
|
65
|
+
simplecov (0.22.0)
|
66
|
+
docile (~> 1.1)
|
67
|
+
simplecov-html (~> 0.11)
|
68
|
+
simplecov_json_formatter (~> 0.1)
|
69
|
+
simplecov-html (0.12.3)
|
70
|
+
simplecov_json_formatter (0.1.4)
|
71
|
+
tzinfo (2.0.5)
|
72
|
+
concurrent-ruby (~> 1.0)
|
73
|
+
unicode-display_width (2.3.0)
|
74
|
+
webrick (1.7.0)
|
75
|
+
yard (0.9.28)
|
76
|
+
webrick (~> 1.7.0)
|
77
|
+
zeitwerk (2.6.6)
|
78
|
+
|
79
|
+
PLATFORMS
|
80
|
+
x86_64-linux
|
81
|
+
|
82
|
+
DEPENDENCIES
|
83
|
+
activesupport (~> 6.0)
|
84
|
+
bundler
|
85
|
+
byebug
|
86
|
+
rack
|
87
|
+
rake (~> 10.0)
|
88
|
+
rspec (~> 3.0)
|
89
|
+
rspec-rails-api!
|
90
|
+
rubocop
|
91
|
+
rubocop-performance
|
92
|
+
rubocop-rake
|
93
|
+
rubocop-rspec
|
94
|
+
simplecov
|
95
|
+
yard
|
96
|
+
|
97
|
+
BUNDLED WITH
|
98
|
+
2.4.1
|
data/README.md
CHANGED
@@ -48,7 +48,7 @@ renderer.api_contact = { name: 'Admin', email: 'admin@example.com', url: 'http:/
|
|
48
48
|
renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' }
|
49
49
|
|
50
50
|
RSpec.configuration.after(:context, type: :acceptance) do |context|
|
51
|
-
renderer.merge_context context.class.metadata[:
|
51
|
+
renderer.merge_context context.class.metadata[:rra].to_h
|
52
52
|
end
|
53
53
|
|
54
54
|
RSpec.configuration.after(:suite) do
|
@@ -125,7 +125,7 @@ end
|
|
125
125
|
#...
|
126
126
|
for_code 200, 'Success' do |url|
|
127
127
|
sing_in #...
|
128
|
-
|
128
|
+
test_response_of url
|
129
129
|
|
130
130
|
#...
|
131
131
|
end
|
@@ -139,14 +139,6 @@ by Arne Hartherz (MIT license).
|
|
139
139
|
|
140
140
|
Write some spec files and run RSpec as you would usually do.
|
141
141
|
|
142
|
-
If you want to generate the documentation without testing the endpoints
|
143
|
-
(and thus, without examples in generated files), use the `DOC_ONLY`
|
144
|
-
environment variable:
|
145
|
-
|
146
|
-
```sh
|
147
|
-
DOC_ONLY=true bundle exec rails spec
|
148
|
-
```
|
149
|
-
|
150
142
|
## Writing specs
|
151
143
|
|
152
144
|
There is a [commented example](dummy/spec/acceptance/posts_spec.rb) available in
|
@@ -171,9 +163,8 @@ RSpec.describe 'Users', type: :acceptance do
|
|
171
163
|
url: { type: :string, description: 'URL to this category' }
|
172
164
|
|
173
165
|
on_get '/api/users/', 'Users list' do
|
174
|
-
for_code 200, 'Success response' do |url|
|
175
|
-
|
176
|
-
expect(response).to have_many defined :user
|
166
|
+
for_code 200, 'Success response', expect_many: :user do |url|
|
167
|
+
test_response_of url
|
177
168
|
end
|
178
169
|
end
|
179
170
|
|
@@ -181,16 +172,15 @@ RSpec.describe 'Users', type: :acceptance do
|
|
181
172
|
path_param id: { type: :integer, description: 'User Id' }
|
182
173
|
|
183
174
|
request_params user: {
|
184
|
-
type: :object,
|
175
|
+
type: :object, attributes: {
|
185
176
|
name: { type: :string, required: false, description: 'New name' },
|
186
177
|
email: { type: :string, required: false, description: 'New email' },
|
187
178
|
role: { type: :string, required: false, description: 'New role' },
|
188
179
|
}
|
189
180
|
}
|
190
181
|
|
191
|
-
for_code 200, 'Success response' do |url|
|
192
|
-
|
193
|
-
expect(response).to have_one defined :user
|
182
|
+
for_code 200, 'Success response', expect_one: :user do |url|
|
183
|
+
test_response_of url
|
194
184
|
end
|
195
185
|
end
|
196
186
|
end
|
@@ -273,6 +263,16 @@ inline.
|
|
273
263
|
Both `:of` and `attributes` may be a hash of fields or a symbol. If they
|
274
264
|
are omitted, they will be documented, but responses won't be validated.
|
275
265
|
|
266
|
+
Arrays of primitives are supported; refer to the [documentation](https://swagger.io/specification/#data-types) for the
|
267
|
+
list. Usage:
|
268
|
+
|
269
|
+
```rb
|
270
|
+
entity :user,
|
271
|
+
favorite_numbers: { type: :array, of: :int32 }
|
272
|
+
```
|
273
|
+
|
274
|
+
Check `lib/rspec_rails_api.rb` for the full list.
|
275
|
+
|
276
276
|
##### `parameters(type, fields)`
|
277
277
|
Describe path or request parameters. The type is only a reference,
|
278
278
|
use whatever makes sense. These parameters will be present in
|
@@ -282,16 +282,16 @@ documentation, only if they are referenced by a `request_params` or
|
|
282
282
|
Fields have the structure of the hash you would give to `request_params`
|
283
283
|
or `path_params` (see each method later in this documentation).
|
284
284
|
|
285
|
-
##### `on_<xxx>(url, description, &block)`
|
285
|
+
##### `on_<xxx>(url, summary = nil, description = nil, &block)`
|
286
286
|
|
287
287
|
Defines an URL.
|
288
288
|
|
289
289
|
- `url` should be a relative URL to an existing endpoint (i.e.:
|
290
290
|
`/api/users`)
|
291
|
-
- `
|
292
|
-
|
291
|
+
- `summary` is a one line description of the endpoint
|
292
|
+
- `description` should be some valid [CommonMark](https://commonmark.org/)
|
293
293
|
|
294
|
-
|
294
|
+
These methods are available:
|
295
295
|
|
296
296
|
- `on_get`
|
297
297
|
- `on_post`
|
@@ -341,7 +341,7 @@ nested elements can be described:
|
|
341
341
|
```ruby
|
342
342
|
on_post '/api/items' do
|
343
343
|
request_params attributes: {
|
344
|
-
item: { type: :object,
|
344
|
+
item: { type: :object, attributes: {
|
345
345
|
name: { type: integer, description: 'The name of the new item', required: true },
|
346
346
|
notes: { type: string, description: 'Additional notes' }
|
347
347
|
} }
|
@@ -353,21 +353,21 @@ end
|
|
353
353
|
An attribute should have the following form:
|
354
354
|
|
355
355
|
```
|
356
|
-
<attr_name>: {type: <type>, desc: <description>, required: <required>,
|
356
|
+
<attr_name>: {type: <type>, desc: <description>, required: <required>, attributes: <another_hash>, of: <another_hash> }
|
357
357
|
```
|
358
358
|
|
359
359
|
- `attr_name` is the attribute name (sic)
|
360
360
|
- `type` is the field type (check _entity definition_ for a list).
|
361
361
|
`type` can be `:object` if the attribute contains other attributes.
|
362
362
|
- `required` is optional an defaults to `false`.
|
363
|
-
- `
|
363
|
+
- `attributes` is a hash of params and is only used if `type: :object`
|
364
364
|
- `of` is a hash of params and is only used if `type: :array`
|
365
365
|
|
366
366
|
Alternative with defined parameters:
|
367
367
|
|
368
368
|
```ruby
|
369
369
|
parameters :item_form_params,
|
370
|
-
item: { type: :object,
|
370
|
+
item: { type: :object, attributes: {
|
371
371
|
name: { type: integer, description: 'The name of the new item', required: true },
|
372
372
|
notes: { type: string, description: 'Additional notes' }
|
373
373
|
}
|
@@ -380,12 +380,12 @@ on_post '/api/items' do
|
|
380
380
|
end
|
381
381
|
```
|
382
382
|
|
383
|
-
##### `for_code(http_status, description = nil,
|
383
|
+
##### `for_code(http_status, description = nil, test_only: false &block)`
|
384
384
|
|
385
385
|
Describes the desired output for a precedently defined URL.
|
386
386
|
|
387
|
-
Block takes one required argument, that should be passed to `
|
388
|
-
This argument will contain the block context and allow `
|
387
|
+
Block takes one required argument, that should be passed to `test_response_of`.
|
388
|
+
This argument will contain the block context and allow `test_response_of` to access
|
389
389
|
the metadatas.
|
390
390
|
|
391
391
|
You can have only one documented code per action/url, unless you use
|
@@ -396,11 +396,9 @@ You can have only one documented code per action/url, unless you use
|
|
396
396
|
- `description` should be some valid
|
397
397
|
[CommonMark](https://commonmark.org/). If not defined, a human readable
|
398
398
|
translation of the `http_status` will be used.
|
399
|
-
- `doc_only` can be set to true to temporarily disable block execution
|
400
|
-
and only create the documentation (without examples).
|
401
399
|
- `test_only` will omit the test from the documentation. Useful when you
|
402
400
|
need to test things _around_ the call (response content, db,...)
|
403
|
-
- `block` where additional tests can be performed. If `
|
401
|
+
- `block` where additional tests can be performed. If `test_response_of` is
|
404
402
|
called within the block, its output will be used in documentation
|
405
403
|
examples, and the response type and code will actually be tested.
|
406
404
|
|
@@ -409,17 +407,17 @@ examples. This can be useful to document endpoints that are impossible
|
|
409
407
|
to test.
|
410
408
|
|
411
409
|
Once again, you have to pass an argument to the block if you use
|
412
|
-
`
|
410
|
+
`test_response_of`.
|
413
411
|
|
414
412
|
```ruby
|
415
413
|
# ...
|
416
414
|
for_code 200, 'A successful response' do |url|
|
417
|
-
|
415
|
+
test_response_of url
|
418
416
|
# ...
|
419
417
|
end
|
420
418
|
|
421
419
|
for_code 200, 'Side test', test_only: true do |url|
|
422
|
-
|
420
|
+
test_response_of url
|
423
421
|
# ...
|
424
422
|
end
|
425
423
|
# ...
|
@@ -429,7 +427,7 @@ Once again, you have to pass an argument to the block if you use
|
|
429
427
|
|
430
428
|
Example methods are available in `for_code` blocks
|
431
429
|
|
432
|
-
##### `
|
430
|
+
##### `test_response_of(example, path_params: {}, payload: {}, headers: {})`
|
433
431
|
|
434
432
|
Visits the described URL and:
|
435
433
|
|
@@ -446,12 +444,17 @@ Visits the described URL and:
|
|
446
444
|
|
447
445
|
```ruby
|
448
446
|
for_code 200, 'Success' do |url|
|
449
|
-
|
447
|
+
test_response_of url
|
450
448
|
end
|
451
449
|
```
|
452
450
|
|
453
451
|
#### Matchers
|
454
452
|
|
453
|
+
The matchers _should not_ be used in acceptance specs unless the default DSL is
|
454
|
+
not adapted for a particular use case (and I can't imagine one now).
|
455
|
+
|
456
|
+
For the sake of comprehension, the two custom matchers still described.
|
457
|
+
|
455
458
|
##### `have_one(type)`
|
456
459
|
|
457
460
|
Expects the compared content to be a hash with the same keys as a
|
data/Rakefile
CHANGED
@@ -6,7 +6,16 @@ module RSpec
|
|
6
6
|
module DSL
|
7
7
|
# These methods will be available in examples (i.e.: 'for_code')
|
8
8
|
module Example
|
9
|
-
|
9
|
+
##
|
10
|
+
# Visits the current example and tests the response
|
11
|
+
#
|
12
|
+
# @param example [Hash] Current example
|
13
|
+
# @param path_params [Hash] Path parameters definition
|
14
|
+
# @param payload [Hash] Request body
|
15
|
+
# @param headers [Hash] Custom headers
|
16
|
+
#
|
17
|
+
# @return [void]
|
18
|
+
def test_response_of(example, path_params: {}, payload: {}, headers: {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
10
19
|
raise 'Missing context. Call visit with for_code context.' unless example
|
11
20
|
|
12
21
|
status_code = prepare_status_code example.class.description
|
@@ -23,37 +32,70 @@ module RSpec
|
|
23
32
|
|
24
33
|
return if example.class.description.match?(/-> test (\d+)(.*)/)
|
25
34
|
|
26
|
-
set_request_example example.class.metadata[:
|
35
|
+
set_request_example example.class.metadata[:rra], request_params, status_code, response.body
|
27
36
|
end
|
28
37
|
|
38
|
+
private
|
39
|
+
|
40
|
+
##
|
41
|
+
# Searches for a defined entity in metadata
|
42
|
+
#
|
43
|
+
# @param entity [Symbol] Entity reference
|
44
|
+
#
|
45
|
+
# @return [RSpec::Rails::Api::EntityConfig, Hash] Defined entity
|
29
46
|
def defined(entity)
|
30
|
-
|
31
|
-
raise '@current_resource is unset' unless current_resource
|
47
|
+
return { type: entity } if PRIMITIVES.include? entity
|
32
48
|
|
33
|
-
|
49
|
+
current_resource = rra_metadata.current_resource
|
50
|
+
raise '@current_resource is unset' unless current_resource
|
34
51
|
|
35
|
-
|
36
|
-
raise "
|
52
|
+
entities = rra_metadata.resources[current_resource][:entities]
|
53
|
+
raise "Unknown entity '#{entity}' in resource '#{current_resource}'" unless entities.key? entity.to_sym
|
37
54
|
|
38
|
-
|
55
|
+
entities[entity.to_sym].expand_with(entities)
|
39
56
|
end
|
40
57
|
|
41
|
-
|
42
|
-
|
43
|
-
|
58
|
+
##
|
59
|
+
# Performs various tests on the response
|
60
|
+
#
|
61
|
+
# @param response [ActionDispatch::TestResponse] The response
|
62
|
+
# @param expected_code [Number] Code to test for
|
63
|
+
def check_response(response, expected_code) # rubocop:disable Metrics/AbcSize
|
44
64
|
expect(response.status).to eq expected_code
|
45
65
|
expect(response.headers['Content-Type']).to eq 'application/json; charset=utf-8' if expected_code != 204
|
66
|
+
expectations = rra_current_example[:expectations]
|
67
|
+
expect(response).to have_many defined(expectations[:many]) if expectations[:many]
|
68
|
+
expect(response).to have_one defined(expectations[:one]) if expectations[:one]
|
69
|
+
expect(response.body).to eq '' if expectations[:none]
|
46
70
|
end
|
47
71
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
72
|
+
##
|
73
|
+
# Adds a request example information to metadata
|
74
|
+
#
|
75
|
+
# @param rra_metadata [RSpec::Rails::Api::Metadata]
|
76
|
+
# @param request_params [Hash]
|
77
|
+
# @param status_code [Integer, nil]
|
78
|
+
# @param response [String, nil]
|
79
|
+
#
|
80
|
+
# @return [void]
|
81
|
+
def set_request_example(rra_metadata, request_params, status_code = nil, response = nil)
|
82
|
+
rra_metadata.add_request_example(url: request_params[:example_url],
|
83
|
+
action: request_params[:action],
|
84
|
+
status_code: status_code,
|
85
|
+
response: response,
|
86
|
+
path_params: request_params[:path_params],
|
87
|
+
params: request_params[:params])
|
55
88
|
end
|
56
89
|
|
90
|
+
##
|
91
|
+
# Prepares the options for a request
|
92
|
+
#
|
93
|
+
# @param description [String] RSpec example description
|
94
|
+
# @param request_params [Hash] Request parameters
|
95
|
+
# @param payload [Hash] Request body
|
96
|
+
# @param request_headers [Hash] Custom headers
|
97
|
+
#
|
98
|
+
# @return [Hash] Options for the request
|
57
99
|
def prepare_request_params(description, request_params = {}, payload = {}, request_headers = {})
|
58
100
|
example_params = description.split
|
59
101
|
|
@@ -66,7 +108,13 @@ module RSpec
|
|
66
108
|
}
|
67
109
|
end
|
68
110
|
|
111
|
+
##
|
69
112
|
# Replace path params by values
|
113
|
+
#
|
114
|
+
# @param url [String] Url definition
|
115
|
+
# @param request_params [Hash] Request parameters
|
116
|
+
#
|
117
|
+
# @return [String] Actual path to visit
|
70
118
|
def prepare_request_url(url, request_params)
|
71
119
|
url.gsub(/(?::(\w*))/) do |e|
|
72
120
|
symbol = e.sub(':', '').to_sym
|
@@ -80,6 +128,12 @@ module RSpec
|
|
80
128
|
end
|
81
129
|
end
|
82
130
|
|
131
|
+
##
|
132
|
+
# Prepares request headers
|
133
|
+
#
|
134
|
+
# @param headers [Hash] Custom headers
|
135
|
+
#
|
136
|
+
# @return [Hash] Headers to use in request
|
83
137
|
def prepare_request_headers(headers = {})
|
84
138
|
{
|
85
139
|
'Accept' => 'application/json',
|
@@ -87,6 +141,12 @@ module RSpec
|
|
87
141
|
}.merge headers
|
88
142
|
end
|
89
143
|
|
144
|
+
##
|
145
|
+
# Extracts status code from RSpec example description
|
146
|
+
#
|
147
|
+
# @param description [String] RSpec example description
|
148
|
+
#
|
149
|
+
# @return [Integer] Status code
|
90
150
|
def prepare_status_code(description)
|
91
151
|
code_match = /->(?: test)? (\d+) - .*/.match description
|
92
152
|
|
@@ -94,6 +154,22 @@ module RSpec
|
|
94
154
|
|
95
155
|
code_match[1].to_i
|
96
156
|
end
|
157
|
+
|
158
|
+
##
|
159
|
+
# Returns the current example configuration from metadata
|
160
|
+
#
|
161
|
+
# @return [Hash] Current example configuration
|
162
|
+
def rra_current_example
|
163
|
+
self.class.metadata[:rra_current_example]
|
164
|
+
end
|
165
|
+
|
166
|
+
##
|
167
|
+
# Returns the whole Rspec-rails-API metadata object
|
168
|
+
#
|
169
|
+
# @return [RSpec::Rails::Api::Metadata] Rspec-rails-API metadata object
|
170
|
+
def rra_metadata
|
171
|
+
self.class.metadata[:rra]
|
172
|
+
end
|
97
173
|
end
|
98
174
|
end
|
99
175
|
end
|