ryquest 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/ryquest/configuration.rb +24 -0
- data/lib/ryquest/context.rb +79 -0
- data/lib/ryquest/version.rb +1 -2
- data/lib/ryquest.rb +9 -62
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a77be824e336c393322a2ca0612d66a2a422ec2188bc8e9c69843f7679ac845
|
4
|
+
data.tar.gz: a2b08ab7d1dd13480fee42dbadeeb0bdecf4fa4879f29f45adeb35cb09581462
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec077bc542b01e5d8f32761bd07a4b5c14d9dbe93ba8ca2bab9199caecc0e6d651c83aa37fe1d25463ad56afb0e1935c07c876a0925afdc22adbcbb4beed6f2e
|
7
|
+
data.tar.gz: ba2cb2a88461ae68c648a6e217e4ca70133687248ed30b68fc1c67478f44d60f48595e8c4dd45146dd034019b73ec02f07ade4972a378bfe1b2cc05d658c8ea2
|
data/README.md
CHANGED
@@ -95,6 +95,9 @@ To use specific headers and params with `response!`, put their value in `let` va
|
|
95
95
|
end
|
96
96
|
```
|
97
97
|
|
98
|
+
Params are converted by default to form-urlencoded format.
|
99
|
+
`Ryquest.configuration.content_type` can change this behaviour.
|
100
|
+
|
98
101
|
### With route parameters
|
99
102
|
|
100
103
|
`response!` handle route parameters by looking for a `let` variable with the same parameter name.
|
@@ -133,3 +136,15 @@ To use specific headers and params with `response!`, put their value in `let` va
|
|
133
136
|
end
|
134
137
|
end
|
135
138
|
```
|
139
|
+
|
140
|
+
### Configuration
|
141
|
+
|
142
|
+
Configuration can be change with `Ryquest.configuration` or `Ryquest.configure` method.
|
143
|
+
|
144
|
+
* `content_type` change how params are converted and sent. Note that it can change CONTENT_TYPE header
|
145
|
+
* `:form` (default value) convert to form-urlencoded
|
146
|
+
* `:json` convert to JSON, change CONTENT-TYPE to application/json
|
147
|
+
|
148
|
+
### TODO
|
149
|
+
|
150
|
+
* Permit to pass params and headers into response rather than update params directly
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ryquest
|
4
|
+
# Hold / handle writable configuration variables
|
5
|
+
# @attr content_type [Symbol] Must be :form or :json.
|
6
|
+
# Define in which format response! send data. Accepted value:
|
7
|
+
# * :form (default) - x-www-form-urlencoded
|
8
|
+
# * :json - JSON (only affect post, put, patch))
|
9
|
+
# Note that the value will affect CONTENT-TYPE header
|
10
|
+
class Configuration
|
11
|
+
attr_reader :content_type
|
12
|
+
|
13
|
+
def content_type= value
|
14
|
+
raise ArgumentError, 'content_type configuration value must be form or json' if %i[form json].exclude? value
|
15
|
+
|
16
|
+
@content_type = value
|
17
|
+
end
|
18
|
+
|
19
|
+
# Define default configuration value
|
20
|
+
def initialize
|
21
|
+
@content_type = :form
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# Context to include to get request DSL improvment
|
2
|
+
RSpec.shared_context 'ryquest helper' do # rubocop:disable Metrics/BlockLength
|
3
|
+
let(:headers) { nil }
|
4
|
+
let(:params) { nil }
|
5
|
+
|
6
|
+
# Execute request described in example description and return #response value.
|
7
|
+
# Example description must be formated as: "VERB /path".
|
8
|
+
#
|
9
|
+
# Request will not be launch if it not exist or response already have a value.
|
10
|
+
#
|
11
|
+
# VERB can be: GET, POST, PUT, PATCH, DELETE
|
12
|
+
#
|
13
|
+
# To add specific body or headers define them with `let(:headers/:params) { { key: :value } }
|
14
|
+
#
|
15
|
+
# Params path can be send as: 'VERB /path/:param',
|
16
|
+
# :param will be replaced by `param` value (let, method, ...) or their `id` value.
|
17
|
+
#
|
18
|
+
# @return [ActionDispatch::TestResponse] the #response value
|
19
|
+
def response!
|
20
|
+
match = match_verb
|
21
|
+
return response if response || match.nil?
|
22
|
+
|
23
|
+
verb = match[1]
|
24
|
+
path = match[2]
|
25
|
+
content = build_content verb
|
26
|
+
|
27
|
+
send verb.downcase, build_path(path), params: content[:params], headers: content[:headers]
|
28
|
+
response
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Hash] the #response!.body parsed from json
|
32
|
+
def json!; JSON.parse response!.body end
|
33
|
+
|
34
|
+
private
|
35
|
+
# Convert params and header to the request content (body, params, headers, ...)
|
36
|
+
# @return [Hash] with params and headers key
|
37
|
+
def build_content verb
|
38
|
+
if Ryquest.configuration.content_type == :form || %w[GET DELETE].include?(verb)
|
39
|
+
return { params: params, headers: headers }
|
40
|
+
end
|
41
|
+
|
42
|
+
{ params: params, headers: headers || {} }.tap do |result|
|
43
|
+
result[:params] = result[:params].to_json
|
44
|
+
result[:headers] = result[:headers].merge 'CONTENT_TYPE' => 'application/json'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Find request http verb and path in the parents example description.
|
49
|
+
# @return [MatchData] verb in [0] path in [1]
|
50
|
+
def match_verb
|
51
|
+
match = nil
|
52
|
+
|
53
|
+
self.class.parent_groups.each do |parent|
|
54
|
+
match = parent.description.match /^(GET|POST|PATCH|PUT|DELETE) (.+)/
|
55
|
+
break if match
|
56
|
+
end
|
57
|
+
|
58
|
+
match
|
59
|
+
end
|
60
|
+
|
61
|
+
# Replace params path with their value.
|
62
|
+
# Use param path #id if present.
|
63
|
+
# @return [String]
|
64
|
+
def build_path path
|
65
|
+
return path if path.exclude? ':'
|
66
|
+
|
67
|
+
path.scan(/:(\w+\b)/).flatten.each do |match|
|
68
|
+
value = send match
|
69
|
+
value = value.id if value.respond_to? :id
|
70
|
+
|
71
|
+
path = path.gsub ":#{match}", value.to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
path
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Include the context when using the type: :request option
|
79
|
+
RSpec.configure { |config| config.include_context 'ryquest helper', type: :request }
|
data/lib/ryquest/version.rb
CHANGED
data/lib/ryquest.rb
CHANGED
@@ -1,68 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
let(:headers) { nil }
|
6
|
-
let(:params) { nil }
|
3
|
+
require 'ryquest/configuration'
|
4
|
+
require 'ryquest/context'
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
|
12
|
-
#
|
13
|
-
# VERB can be: GET, POST, PUT, PATCH, DELETE
|
14
|
-
#
|
15
|
-
# To add specific body or headers define them with `let(:headers/:params) { { key: :value } }
|
16
|
-
#
|
17
|
-
# Params path can be send as: 'VERB /path/:param',
|
18
|
-
# :param will be replaced by `param` value (let, method, ...) or their `id` value.
|
19
|
-
#
|
20
|
-
# @return [ActionDispatch::TestResponse] the #response value
|
21
|
-
def response!
|
22
|
-
match = match_verb
|
23
|
-
return response if response || match.nil?
|
6
|
+
# Root module
|
7
|
+
module Ryquest
|
8
|
+
# @return [Ryquest::Configuration] Global configuration instance (create it if not present)
|
9
|
+
def self.configuration; @configuration ||= Configuration.new end
|
24
10
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
send verb.downcase, build_path(path), params: params, headers: headers
|
29
|
-
response
|
30
|
-
end
|
31
|
-
|
32
|
-
# @return [Hash] the #response!.body parsed from json
|
33
|
-
def json!; JSON.parse response!.body end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
# Find request http verb and path in the parents example description.
|
38
|
-
# @return [MatchData] verb in [0] path in [1]
|
39
|
-
def match_verb
|
40
|
-
match = nil
|
41
|
-
|
42
|
-
self.class.parent_groups.each do |parent|
|
43
|
-
match = parent.description.match /^(GET|POST|PATCH|PUT|DELETE) (.+)/
|
44
|
-
break if match
|
45
|
-
end
|
46
|
-
|
47
|
-
match
|
48
|
-
end
|
49
|
-
|
50
|
-
# Replace params path with their value.
|
51
|
-
# Use param path #id if present.
|
52
|
-
# @return [String]
|
53
|
-
def build_path path
|
54
|
-
return path if path.exclude? ':'
|
55
|
-
|
56
|
-
path.scan(/:(\w+\b)/).flatten.each do |match|
|
57
|
-
value = send match
|
58
|
-
value = value.id if value.respond_to? :id
|
59
|
-
|
60
|
-
path = path.gsub ":#{match}", value.to_s
|
61
|
-
end
|
62
|
-
|
63
|
-
path
|
64
|
-
end
|
11
|
+
# Yields #configuration to a block
|
12
|
+
# @yield [Ryquest::Configuration]
|
13
|
+
def self.configure; yield configuration end
|
65
14
|
end
|
66
15
|
|
67
|
-
# Include the context when using the type: :request option
|
68
|
-
RSpec.configure { |config| config.include_context 'ryquest helper', type: :request }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ryquest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Guego
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -27,6 +27,8 @@ files:
|
|
27
27
|
- Makefile
|
28
28
|
- README.md
|
29
29
|
- lib/ryquest.rb
|
30
|
+
- lib/ryquest/configuration.rb
|
31
|
+
- lib/ryquest/context.rb
|
30
32
|
- lib/ryquest/version.rb
|
31
33
|
- ryquest.gemspec
|
32
34
|
homepage: https://github.com/faljie/ryquest
|