stub_requests 0.1.10 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.reek.yml +1 -0
- data/CHANGELOG.md +2 -3
- data/Rakefile +1 -1
- data/lib/stub_requests/api.rb +2 -2
- data/lib/stub_requests/exceptions.rb +50 -1
- data/lib/stub_requests/uri/builder.rb +31 -112
- data/lib/stub_requests/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 790cf23df8e8bd3f6532cb0e3008bf41150c189135ec7eed33ac795523325f20
|
4
|
+
data.tar.gz: 7c546bc75dc7aeb88aa7d92addc7455f1d240feca299427c4da970eceb20116b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68a87e36d03c65705c587417600ec9dd798c92ecfd56df45da50fce6b8471a8f460e27a8a1c34a5787daff77c54a383caf79bff35eb98c6da937ec0255659083
|
7
|
+
data.tar.gz: 92fccfea4f37480e5fb4cfff7863288897b7741fcd606848eb702b1a73459d66a1acf862ffa27fd171913defd513e2e702769a02f809285b5a48ff7818c1021e
|
data/.reek.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
-
## [
|
4
|
-
|
5
|
-
[Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.9...HEAD)
|
3
|
+
## [v0.1.10](https://github.com/mhenrixon/stub_requests/tree/v0.1.10) (2019-02-07)
|
4
|
+
[Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.9...v0.1.10)
|
6
5
|
|
7
6
|
**Implemented enhancements:**
|
8
7
|
|
data/Rakefile
CHANGED
data/lib/stub_requests/api.rb
CHANGED
@@ -72,7 +72,7 @@ module StubRequests
|
|
72
72
|
#
|
73
73
|
def stub_endpoint(endpoint_id, route_params = {}, &callback)
|
74
74
|
endpoint = EndpointRegistry.instance.find!(endpoint_id)
|
75
|
-
uri = URI::Builder.build(endpoint.
|
75
|
+
uri = URI::Builder.build(endpoint.uri, route_params)
|
76
76
|
webmock_stub = WebMock::Builder.build(endpoint.verb, uri, &callback)
|
77
77
|
|
78
78
|
StubRegistry.instance.record(endpoint.id, webmock_stub)
|
@@ -82,7 +82,7 @@ module StubRequests
|
|
82
82
|
# :nodoc:
|
83
83
|
def __stub_endpoint(endpoint_id, route_params = {})
|
84
84
|
endpoint = EndpointRegistry.instance.find!(endpoint_id)
|
85
|
-
uri = URI::Builder.build(endpoint.
|
85
|
+
uri = URI::Builder.build(endpoint.uri, route_params)
|
86
86
|
endpoint_stub = WebMock::Builder.build(endpoint.verb, uri)
|
87
87
|
|
88
88
|
::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub)
|
@@ -94,5 +94,54 @@ module StubRequests
|
|
94
94
|
#
|
95
95
|
# UriSegmentMismatch is raised when a segment cannot be replaced
|
96
96
|
#
|
97
|
-
class UriSegmentMismatch < Error
|
97
|
+
class UriSegmentMismatch < Error
|
98
|
+
attr_reader :uri, :expected_keys, :received_keys
|
99
|
+
def initialize(uri:, expected_keys:, received_keys:)
|
100
|
+
@uri = uri
|
101
|
+
@expected_keys = expected_keys
|
102
|
+
@received_keys = received_keys
|
103
|
+
|
104
|
+
super(message_parts.join("\n "))
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def message_parts
|
110
|
+
[].tap do |arr|
|
111
|
+
arr << default_part
|
112
|
+
arr << expected_keys_part
|
113
|
+
arr << received_keys_part
|
114
|
+
arr << missing_keys_part if missing_keys.any?
|
115
|
+
arr << invalid_keys_part if invalid_keys.any?
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def default_part
|
120
|
+
"The URI (#{uri}) received unexpected route parameters"
|
121
|
+
end
|
122
|
+
|
123
|
+
def expected_keys_part
|
124
|
+
"Expected: [#{expected_keys.join(',')}]"
|
125
|
+
end
|
126
|
+
|
127
|
+
def received_keys_part
|
128
|
+
"Received: [#{received_keys.join(',')}]"
|
129
|
+
end
|
130
|
+
|
131
|
+
def missing_keys_part
|
132
|
+
"Missing: [#{missing_keys.join(',')}]"
|
133
|
+
end
|
134
|
+
|
135
|
+
def invalid_keys_part
|
136
|
+
"Invalid: [#{invalid_keys.join(',')}]"
|
137
|
+
end
|
138
|
+
|
139
|
+
def missing_keys
|
140
|
+
@missing_keys ||= expected_keys - received_keys
|
141
|
+
end
|
142
|
+
|
143
|
+
def invalid_keys
|
144
|
+
@invalid_keys ||= received_keys - expected_keys
|
145
|
+
end
|
146
|
+
end
|
98
147
|
end
|
@@ -21,7 +21,7 @@ module StubRequests
|
|
21
21
|
class Builder
|
22
22
|
#
|
23
23
|
# @return [Regexp] A pattern for matching url segment keys
|
24
|
-
|
24
|
+
URI_KEY = /(:[a-zA-Z_]+)/.freeze
|
25
25
|
|
26
26
|
#
|
27
27
|
# Convenience method to avoid .new.build
|
@@ -30,54 +30,44 @@ module StubRequests
|
|
30
30
|
# @raise [UriSegmentMismatch] when there are unused URI segments
|
31
31
|
# @raise [UriSegmentMismatch] when the template have unplaced URI segments
|
32
32
|
#
|
33
|
-
# @param [String]
|
34
|
-
# @param [String] template the endpoint template
|
33
|
+
# @param [String] the URI to the service endpoint
|
35
34
|
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
|
36
35
|
#
|
37
36
|
# @return [String] a validated URI string
|
38
37
|
#
|
39
|
-
def self.build(
|
40
|
-
new(
|
38
|
+
def self.build(uri, route_params = {})
|
39
|
+
new(uri, route_params).build
|
41
40
|
end
|
42
41
|
|
43
42
|
#
|
44
43
|
# @!attribute [r] uri
|
45
|
-
# @return [String] the
|
46
|
-
attr_reader :
|
47
|
-
#
|
48
|
-
# @!attribute [r] template
|
49
|
-
# @return [String] a string template for the endpoint
|
50
|
-
attr_reader :template
|
51
|
-
#
|
52
|
-
# @!attribute [r] path
|
53
|
-
# @return [String] a valid URI path
|
54
|
-
attr_reader :path
|
44
|
+
# @return [String] the URI to the service endpoint
|
45
|
+
attr_reader :uri
|
55
46
|
#
|
56
47
|
# @!attribute [r] route_params
|
57
48
|
# @return [Hash<Symbol] a hash with keys matching the {#template}
|
58
49
|
attr_reader :route_params
|
59
50
|
#
|
60
|
-
# @!attribute [r]
|
61
|
-
# @return [Array<String>] a list with
|
62
|
-
attr_reader :
|
51
|
+
# @!attribute [r] received_keys
|
52
|
+
# @return [Array<String>] a list with actual {#route_params} keys
|
53
|
+
attr_reader :received_keys
|
63
54
|
#
|
64
|
-
# @!attribute [r]
|
65
|
-
# @return [Array<String>] a list of
|
66
|
-
attr_reader :
|
55
|
+
# @!attribute [r] expected_keys
|
56
|
+
# @return [Array<String>] a list of expected route keys
|
57
|
+
attr_reader :expected_keys
|
67
58
|
|
68
59
|
#
|
69
60
|
# Initializes a new Builder
|
70
61
|
#
|
71
62
|
#
|
72
|
-
# @param [String]
|
73
|
-
# @param [String] template the endpoint template
|
63
|
+
# @param [String] uri the URI used to reach the service
|
74
64
|
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
|
75
65
|
#
|
76
|
-
def initialize(
|
77
|
-
@
|
78
|
-
@
|
79
|
-
@
|
80
|
-
@
|
66
|
+
def initialize(uri, route_params = {})
|
67
|
+
@uri = +uri
|
68
|
+
@route_params = route_params.to_route_param
|
69
|
+
@received_keys = @route_params.keys
|
70
|
+
@expected_keys = uri.scan(URI_KEY).flatten.uniq
|
81
71
|
end
|
82
72
|
|
83
73
|
#
|
@@ -90,104 +80,33 @@ module StubRequests
|
|
90
80
|
# @return [String] a validated URI string
|
91
81
|
#
|
92
82
|
def build
|
83
|
+
validate_uri_has_route_params!
|
93
84
|
build_uri
|
94
|
-
|
85
|
+
validate_uri
|
95
86
|
|
96
87
|
uri
|
97
88
|
end
|
98
89
|
|
99
90
|
private
|
100
91
|
|
101
|
-
def
|
102
|
-
|
103
|
-
parse_unreplaced_segments
|
104
|
-
end
|
105
|
-
|
106
|
-
def uri
|
107
|
-
@uri ||= [host, path].join("/")
|
108
|
-
end
|
109
|
-
|
110
|
-
def run_validations
|
111
|
-
validate_route_params_used
|
112
|
-
validate_route_keys_replaced
|
113
|
-
validate_uri
|
114
|
-
end
|
115
|
-
|
116
|
-
#
|
117
|
-
# Replaces the URI segments with the arguments in route_params
|
118
|
-
#
|
119
|
-
#
|
120
|
-
# @return [Array] an list with unused route_params
|
121
|
-
#
|
122
|
-
def replace_segments
|
123
|
-
@unused = route_params.map do |key, value|
|
124
|
-
next key unless path.include?(key)
|
125
|
-
|
126
|
-
path.gsub!(key, value.to_s)
|
127
|
-
next
|
128
|
-
end.compact
|
129
|
-
end
|
130
|
-
|
131
|
-
#
|
132
|
-
# Validates that all route_params have been used
|
133
|
-
#
|
134
|
-
#
|
135
|
-
# @raise [UriSegmentMismatch] when there are unused route_params
|
136
|
-
#
|
137
|
-
# @return [void]
|
138
|
-
#
|
139
|
-
def validate_route_params_used
|
140
|
-
return if replacents_used?
|
141
|
-
|
142
|
-
raise UriSegmentMismatch,
|
143
|
-
"The URI segment(s) [#{unused.join(',')}] are missing in template (#{path})"
|
144
|
-
end
|
92
|
+
def validate_uri_has_route_params!
|
93
|
+
return if validate_uri_has_route_params
|
145
94
|
|
146
|
-
|
147
|
-
# Checks that no route_params are left
|
148
|
-
#
|
149
|
-
#
|
150
|
-
# @return [true,false]
|
151
|
-
#
|
152
|
-
def replacents_used?
|
153
|
-
unused.none?
|
95
|
+
raise UriSegmentMismatch, uri: uri, expected_keys: expected_keys, received_keys: received_keys
|
154
96
|
end
|
155
97
|
|
156
|
-
|
157
|
-
|
158
|
-
#
|
159
|
-
#
|
160
|
-
# @raise [UriSegmentMismatch] when the path have unplaced URI segments
|
161
|
-
#
|
162
|
-
# @return [void]
|
163
|
-
#
|
164
|
-
def validate_route_keys_replaced
|
165
|
-
return if route_keys_replaced?
|
166
|
-
|
167
|
-
raise UriSegmentMismatch,
|
168
|
-
"The URI segment(s) [#{unreplaced.join(',')}]" \
|
169
|
-
" were not replaced in template (#{path})." \
|
170
|
-
" Given route_params=[#{route_params.keys.join(',')}]"
|
98
|
+
def validate_uri_has_route_params
|
99
|
+
expected_keys.sort == received_keys.sort
|
171
100
|
end
|
172
101
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
# @return [true,false]
|
178
|
-
#
|
179
|
-
def route_keys_replaced?
|
180
|
-
unreplaced.none?
|
102
|
+
def build_uri
|
103
|
+
route_params.each do |key, value|
|
104
|
+
replace_key(key, value)
|
105
|
+
end
|
181
106
|
end
|
182
107
|
|
183
|
-
|
184
|
-
|
185
|
-
#
|
186
|
-
#
|
187
|
-
# @return [Array<String>] a list of not replaced uri_segments
|
188
|
-
#
|
189
|
-
def parse_unreplaced_segments
|
190
|
-
@unreplaced = URL_SEGMENT_REGEX.match(path).to_a.uniq
|
108
|
+
def replace_key(key, value)
|
109
|
+
uri.gsub!(key, value.to_s)
|
191
110
|
end
|
192
111
|
|
193
112
|
#
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stub_requests
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikael Henriksson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: docile
|