stub_requests 0.1.3 → 0.1.4
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 -0
- data/README.md +2 -2
- data/lib/stub_requests/api.rb +8 -22
- data/lib/stub_requests/callback.rb +58 -0
- data/lib/stub_requests/callback_registry.rb +188 -0
- data/lib/stub_requests/core_ext/all.rb +1 -0
- data/lib/stub_requests/core_ext/array/extract_options.rb +4 -6
- data/lib/stub_requests/core_ext/class/attribute.rb +2 -3
- data/lib/stub_requests/core_ext/kernel/singleton_class.rb +2 -3
- data/lib/stub_requests/core_ext/module/redefine_method.rb +6 -0
- data/lib/stub_requests/core_ext/object/blank.rb +21 -22
- data/lib/stub_requests/core_ext/string/to_route_param.rb +35 -0
- data/lib/stub_requests/dsl/define_method.rb +49 -0
- data/lib/stub_requests/dsl/method_definition.rb +72 -0
- data/lib/stub_requests/dsl.rb +94 -0
- data/lib/stub_requests/endpoint.rb +48 -36
- data/lib/stub_requests/endpoint_stub.rb +89 -0
- data/lib/stub_requests/endpoints.rb +147 -0
- data/lib/stub_requests/observable.rb +0 -44
- data/lib/stub_requests/request_stub.rb +80 -0
- data/lib/stub_requests/service.rb +77 -0
- data/lib/stub_requests/service_registry.rb +174 -0
- data/lib/stub_requests/stub_registry.rb +159 -0
- data/lib/stub_requests/uri/builder.rb +27 -34
- data/lib/stub_requests/uri.rb +31 -4
- data/lib/stub_requests/version.rb +1 -1
- data/lib/stub_requests/webmock/stub_registry_extension.rb +1 -1
- data/lib/stub_requests.rb +12 -12
- data/lib/tasks/changelog.rake +2 -1
- metadata +14 -16
- data/gemfiles/webmock_2.3.gemfile.lock +0 -206
- data/gemfiles/webmock_3.5.gemfile.lock +0 -206
- data/gemfiles/webmock_develop.gemfile.lock +0 -211
- data/lib/stub_requests/metrics/endpoint.rb +0 -98
- data/lib/stub_requests/metrics/registry.rb +0 -132
- data/lib/stub_requests/metrics/request.rb +0 -89
- data/lib/stub_requests/metrics.rb +0 -33
- data/lib/stub_requests/observable/registry.rb +0 -152
- data/lib/stub_requests/observable/subscription.rb +0 -58
- data/lib/stub_requests/registration/endpoint.rb +0 -107
- data/lib/stub_requests/registration/endpoints.rb +0 -156
- data/lib/stub_requests/registration/registry.rb +0 -112
- data/lib/stub_requests/registration/service.rb +0 -85
- data/lib/stub_requests/registration.rb +0 -87
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Abstraction over WebMock to reduce duplication
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
7
|
+
# @since 0.1.0
|
8
|
+
#
|
9
|
+
module StubRequests
|
10
|
+
#
|
11
|
+
# Class Service provides details for a registered service
|
12
|
+
#
|
13
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
+
#
|
15
|
+
class Service
|
16
|
+
include Comparable
|
17
|
+
include Property
|
18
|
+
|
19
|
+
# @!attribute [rw] id
|
20
|
+
# @return [Symbol] the id of the service
|
21
|
+
property :id, type: Symbol
|
22
|
+
|
23
|
+
# @!attribute [rw] uri
|
24
|
+
# @return [String] the base uri to the service
|
25
|
+
property :uri, type: String
|
26
|
+
|
27
|
+
# @!attribute [rw] endpoints
|
28
|
+
# @return [Endpoints] a list with defined endpoints
|
29
|
+
attr_reader :endpoints
|
30
|
+
|
31
|
+
#
|
32
|
+
# Initializes a new instance of a Service
|
33
|
+
#
|
34
|
+
# @param [Symbol] service_id the id of this service
|
35
|
+
# @param [String] service_uri the base uri to reach the service
|
36
|
+
#
|
37
|
+
def initialize(service_id, service_uri)
|
38
|
+
self.id = service_id
|
39
|
+
self.uri = service_uri
|
40
|
+
@endpoints = Endpoints.new(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Check if the endpoint registry has endpoints
|
45
|
+
#
|
46
|
+
# @return [true,false]
|
47
|
+
#
|
48
|
+
def endpoints?
|
49
|
+
endpoints.any?
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Returns a nicely formatted string with this service
|
54
|
+
#
|
55
|
+
# @return [String]
|
56
|
+
#
|
57
|
+
def to_s
|
58
|
+
[
|
59
|
+
+"#<#{self.class}",
|
60
|
+
+" id=#{id}",
|
61
|
+
+" uri=#{uri}",
|
62
|
+
+" endpoints=#{endpoints.endpoints_string}",
|
63
|
+
+">",
|
64
|
+
].join("")
|
65
|
+
end
|
66
|
+
|
67
|
+
def <=>(other)
|
68
|
+
id <=> other.id
|
69
|
+
end
|
70
|
+
|
71
|
+
def hash
|
72
|
+
[id, self.class].hash
|
73
|
+
end
|
74
|
+
|
75
|
+
alias eql? ==
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Abstraction over WebMock to reduce duplication
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
7
|
+
# @since 0.1.0
|
8
|
+
#
|
9
|
+
module StubRequests
|
10
|
+
#
|
11
|
+
# Class Registry provides registration of services
|
12
|
+
#
|
13
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
+
#
|
15
|
+
class ServiceRegistry
|
16
|
+
include Singleton
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
# Register a service in the service registry
|
20
|
+
#
|
21
|
+
#
|
22
|
+
# @param [Symbol] service_id a descriptive id for the service
|
23
|
+
# @param [Symbol] service_uri the uri used to call the service
|
24
|
+
#
|
25
|
+
# @example Register a service with endpoints
|
26
|
+
# register_service(:documents, "https://company.com/api/v1") do
|
27
|
+
# register(:show, :get, "documents/:id")
|
28
|
+
# register(:index, :get, "documents")
|
29
|
+
# register(:create, :post, "documents")
|
30
|
+
# register(:update, :patch, "documents/:id")
|
31
|
+
# register(:destroy, :delete, "documents/:id")
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# @return [Service] a new service or a previously registered service
|
35
|
+
#
|
36
|
+
def self.register_service(service_id, service_uri, &block)
|
37
|
+
service = instance.register(service_id, service_uri)
|
38
|
+
Docile.dsl_eval(service.endpoints, &block) if block.present?
|
39
|
+
service
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Stub a request to a registered service endpoint
|
44
|
+
#
|
45
|
+
#
|
46
|
+
# @param [Symbol] service_id the id of a registered service
|
47
|
+
# @param [Symbol] endpoint_id the id of a registered endpoint
|
48
|
+
# @param [Hash<Symbol>] route_params a map with route parameters
|
49
|
+
#
|
50
|
+
# @note the kind of timeout error raised by webmock is depending on the HTTP client used
|
51
|
+
#
|
52
|
+
# @example Stub a request to a registered service endpoint
|
53
|
+
# register_stub(
|
54
|
+
# :google_api,
|
55
|
+
# :get_map_location,
|
56
|
+
# {}, # No URI replacements needed for this endpoint
|
57
|
+
# )
|
58
|
+
# .to_return(body: "No content", status: 204)
|
59
|
+
#
|
60
|
+
# @example Stub a request to a registered service endpoint using block
|
61
|
+
# register_stub(:documents, :index) do
|
62
|
+
# with(headers: { "Accept" => "application/json" }}})
|
63
|
+
# to_return(body: "No content", status: 204)
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# @see #stub_http_request
|
67
|
+
# @return [WebMock::RequestStub] a mocked request
|
68
|
+
#
|
69
|
+
# :reek:UtilityFunction
|
70
|
+
# :reek:LongParameterList { max_params: 5 }
|
71
|
+
def self.stub_endpoint(service_id, endpoint_id, route_params = {}, &callback)
|
72
|
+
service, endpoint, uri = StubRequests::URI.for_service_endpoint(service_id, endpoint_id, route_params)
|
73
|
+
endpoint_stub = WebMock::Builder.build(endpoint.verb, uri, {}, &callback)
|
74
|
+
|
75
|
+
StubRegistry.record(service, endpoint, endpoint_stub)
|
76
|
+
::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub)
|
77
|
+
end
|
78
|
+
|
79
|
+
# @api private
|
80
|
+
# Used only for testing purposes
|
81
|
+
# :reek:LongParameterList { max_params: 4 }
|
82
|
+
def self.__stub_endpoint(service_id, endpoint_id, route_params = {})
|
83
|
+
_service, endpoint, uri = StubRequests::URI.for_service_endpoint(service_id, endpoint_id, route_params)
|
84
|
+
endpoint_stub = WebMock::Builder.build(endpoint.verb, uri)
|
85
|
+
|
86
|
+
::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub)
|
87
|
+
end
|
88
|
+
|
89
|
+
#
|
90
|
+
# @!attribute [rw] services
|
91
|
+
# @return [Concurrent::Map<Symbol, Service>] a map with services
|
92
|
+
attr_reader :services
|
93
|
+
|
94
|
+
def initialize
|
95
|
+
@services = Concurrent::Map.new
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Resets the map with registered services
|
100
|
+
#
|
101
|
+
#
|
102
|
+
# @api private
|
103
|
+
def reset
|
104
|
+
services.clear
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# Required by Enumerable
|
109
|
+
#
|
110
|
+
#
|
111
|
+
# @return [Concurrent::Map<Symbol, Service>] an map with services
|
112
|
+
#
|
113
|
+
# @yield used by Enumerable
|
114
|
+
#
|
115
|
+
def each(&block)
|
116
|
+
services.each(&block)
|
117
|
+
end
|
118
|
+
|
119
|
+
#
|
120
|
+
# Registers a service in the registry
|
121
|
+
#
|
122
|
+
#
|
123
|
+
# @param [Symbol] service_id a symbolic id of the service
|
124
|
+
# @param [String] service_uri a string with a base_uri to the service
|
125
|
+
#
|
126
|
+
# @return [Service] the service that was just registered
|
127
|
+
#
|
128
|
+
def register(service_id, service_uri)
|
129
|
+
if (service = find(service_id))
|
130
|
+
StubRequests.logger.warn("Service already registered #{service}")
|
131
|
+
raise ServiceHaveEndpoints, service if service.endpoints?
|
132
|
+
end
|
133
|
+
services[service_id] = Service.new(service_id, service_uri)
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Removes a service from the registry
|
138
|
+
#
|
139
|
+
#
|
140
|
+
# @param [Symbol] service_id the service_id to remove
|
141
|
+
#
|
142
|
+
# @raise [ServiceNotFound] when the service was not removed
|
143
|
+
#
|
144
|
+
def remove(service_id)
|
145
|
+
services.delete(service_id) || raise(ServiceNotFound, service_id)
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Fetches a service from the registry
|
150
|
+
#
|
151
|
+
#
|
152
|
+
# @param [Symbol] service_id id of the service to remove
|
153
|
+
#
|
154
|
+
# @return [Service] the found service
|
155
|
+
#
|
156
|
+
def find(service_id)
|
157
|
+
services[service_id]
|
158
|
+
end
|
159
|
+
|
160
|
+
#
|
161
|
+
# Fetches a service from the registry or raises {ServiceNotFound}
|
162
|
+
#
|
163
|
+
#
|
164
|
+
# @param [Symbol] service_id the id of a service
|
165
|
+
#
|
166
|
+
# @raise [ServiceNotFound] when an endpoint couldn't be found
|
167
|
+
#
|
168
|
+
# @return [Service]
|
169
|
+
#
|
170
|
+
def find!(service_id)
|
171
|
+
find(service_id) || raise(ServiceNotFound, service_id)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Abstraction over WebMock to reduce duplication
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
7
|
+
# @since 0.1.0
|
8
|
+
#
|
9
|
+
module StubRequests
|
10
|
+
#
|
11
|
+
# Class Registry maintains a registry of stubbed endpoints.
|
12
|
+
# Also allows provides querying capabilities for said entities.
|
13
|
+
#
|
14
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
15
|
+
# @since 0.1.2
|
16
|
+
#
|
17
|
+
# :reek:DataClump
|
18
|
+
class StubRegistry
|
19
|
+
# includes "Singleton"
|
20
|
+
# @!parse include Singleton
|
21
|
+
include Singleton
|
22
|
+
# includes "Enumerable"
|
23
|
+
# @!parse include Enumerable
|
24
|
+
include Enumerable
|
25
|
+
|
26
|
+
#
|
27
|
+
# Records metrics about stubbed endpoints
|
28
|
+
#
|
29
|
+
#
|
30
|
+
# @param [Service] service a Service
|
31
|
+
# @param [Endpoint] endpoint an Endpoint
|
32
|
+
# @param [WebMock::RequestStub] webmock_stub the stubbed webmock request
|
33
|
+
#
|
34
|
+
# @note the class method of record validates that
|
35
|
+
# configuration option :collect_metrics is true.
|
36
|
+
#
|
37
|
+
# @return [EndpointStub] the stub that was recorded
|
38
|
+
#
|
39
|
+
def self.record(service, endpoint, webmock_stub)
|
40
|
+
# Note: The class method v
|
41
|
+
return unless StubRequests.config.record_metrics?
|
42
|
+
|
43
|
+
instance.record(service, endpoint, webmock_stub)
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Mark a {RequestStub} as having responded
|
48
|
+
#
|
49
|
+
# @note Called when webmock responds successfully
|
50
|
+
#
|
51
|
+
# @param [WebMock::RequestStub] webmock_stub the stubbed webmock request
|
52
|
+
#
|
53
|
+
# @return [void]
|
54
|
+
#
|
55
|
+
def self.mark_as_responded(webmock_stub)
|
56
|
+
instance.mark_as_responded(webmock_stub)
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# @!attribute [rw] services
|
61
|
+
# @return [Concurrent::Array<Endpoint>] a map with stubbed endpoints
|
62
|
+
attr_reader :endpoints
|
63
|
+
|
64
|
+
#
|
65
|
+
# Initialize a new registry
|
66
|
+
#
|
67
|
+
#
|
68
|
+
def initialize
|
69
|
+
@endpoints = Concurrent::Array.new
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# Resets the map with stubbed endpoints
|
74
|
+
#
|
75
|
+
#
|
76
|
+
# @api private
|
77
|
+
def reset
|
78
|
+
endpoints.clear
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Required by Enumerable
|
83
|
+
#
|
84
|
+
#
|
85
|
+
# @return [Concurrent::Array<Endpoint>] an array with stubbed endpoints
|
86
|
+
#
|
87
|
+
# @yield used by Enumerable
|
88
|
+
#
|
89
|
+
def each(&block)
|
90
|
+
endpoints.each(&block)
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
# Records metrics about stubbed endpoints
|
95
|
+
#
|
96
|
+
#
|
97
|
+
# @param [Service] service a symbolic id of the service
|
98
|
+
# @param [Endpoint] endpoint a string with a base_uri to the service
|
99
|
+
# @param [WebMock::RequestStub] webmock_stub the stubbed request
|
100
|
+
#
|
101
|
+
# @return [Service] the service that was just registered
|
102
|
+
#
|
103
|
+
def record(service, endpoint, webmock_stub)
|
104
|
+
endpoint = find_or_initialize_endpoint_stub(service, endpoint)
|
105
|
+
endpoint.record(webmock_stub)
|
106
|
+
|
107
|
+
endpoints.push(endpoint)
|
108
|
+
endpoint
|
109
|
+
end
|
110
|
+
|
111
|
+
#
|
112
|
+
# Mark a {RequestStub} as having responded
|
113
|
+
#
|
114
|
+
# @note Called when webmock responds successfully
|
115
|
+
#
|
116
|
+
# @param [WebMock::RequestStub] webmock_stub the stubbed webmock request
|
117
|
+
#
|
118
|
+
# @return [void]
|
119
|
+
#
|
120
|
+
def mark_as_responded(webmock_stub)
|
121
|
+
return unless (request_stub = find_request_stub(webmock_stub))
|
122
|
+
|
123
|
+
request_stub.mark_as_responded
|
124
|
+
CallbackRegistry.invoke_callbacks(request_stub)
|
125
|
+
request_stub
|
126
|
+
end
|
127
|
+
|
128
|
+
#
|
129
|
+
# Finds a {RequestStub} amongst the endpoint stubs
|
130
|
+
#
|
131
|
+
#
|
132
|
+
# @param [WebMock::RequestStub] webmock_stub a stubbed webmock response
|
133
|
+
#
|
134
|
+
# @return [RequestStub] the request_stubbed matching the request stub
|
135
|
+
#
|
136
|
+
def find_request_stub(webmock_stub)
|
137
|
+
map do |endpoint|
|
138
|
+
endpoint.find_by(attribute: :request_stub, value: webmock_stub)
|
139
|
+
end.compact.first
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
def find_or_initialize_endpoint_stub(service, endpoint)
|
145
|
+
find_endpoint_stub(service, endpoint) || initialize_endpoint_stub(service, endpoint)
|
146
|
+
end
|
147
|
+
|
148
|
+
# :reek:UtilityFunction
|
149
|
+
# :reek:FeatureEnvy
|
150
|
+
def find_endpoint_stub(service, endpoint)
|
151
|
+
find { |ep| ep.service_id == service.id && ep.endpoint_id == endpoint.id }
|
152
|
+
end
|
153
|
+
|
154
|
+
# :reek:UtilityFunction
|
155
|
+
def initialize_endpoint_stub(service, endpoint)
|
156
|
+
EndpointStub.new(service, endpoint)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -33,12 +33,12 @@ module StubRequests
|
|
33
33
|
#
|
34
34
|
# @param [String] host the URI used to reach the service
|
35
35
|
# @param [String] template the endpoint template
|
36
|
-
# @param [Hash<Symbol>]
|
36
|
+
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
|
37
37
|
#
|
38
38
|
# @return [String] a validated URI string
|
39
39
|
#
|
40
|
-
def self.build(host, template,
|
41
|
-
new(host, template,
|
40
|
+
def self.build(host, template, route_params = {})
|
41
|
+
new(host, template, route_params).build
|
42
42
|
end
|
43
43
|
|
44
44
|
#
|
@@ -54,12 +54,12 @@ module StubRequests
|
|
54
54
|
# @return [String] a valid URI path
|
55
55
|
attr_reader :path
|
56
56
|
#
|
57
|
-
# @!attribute [r]
|
57
|
+
# @!attribute [r] route_params
|
58
58
|
# @return [Hash<Symbol] a hash with keys matching the {#template}
|
59
|
-
attr_reader :
|
59
|
+
attr_reader :route_params
|
60
60
|
#
|
61
61
|
# @!attribute [r] unused
|
62
|
-
# @return [Array<String>] a list with unused {#
|
62
|
+
# @return [Array<String>] a list with unused {#route_params}
|
63
63
|
attr_reader :unused
|
64
64
|
#
|
65
65
|
# @!attribute [r] unreplaced
|
@@ -72,13 +72,13 @@ module StubRequests
|
|
72
72
|
#
|
73
73
|
# @param [String] host the URI used to reach the service
|
74
74
|
# @param [String] template the endpoint template
|
75
|
-
# @param [Hash<Symbol>]
|
75
|
+
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
|
76
76
|
#
|
77
|
-
def initialize(host, template,
|
77
|
+
def initialize(host, template, route_params = {})
|
78
78
|
@host = +host
|
79
79
|
@template = +template
|
80
80
|
@path = +@template.dup
|
81
|
-
@
|
81
|
+
@route_params = route_params.to_route_param
|
82
82
|
end
|
83
83
|
|
84
84
|
#
|
@@ -109,38 +109,35 @@ module StubRequests
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def run_validations
|
112
|
-
|
113
|
-
|
112
|
+
validate_route_params_used
|
113
|
+
validate_route_keys_replaced
|
114
114
|
validate_uri
|
115
115
|
end
|
116
116
|
|
117
117
|
#
|
118
|
-
# Replaces the URI segments with the arguments in
|
118
|
+
# Replaces the URI segments with the arguments in route_params
|
119
119
|
#
|
120
120
|
#
|
121
|
-
# @return [Array] an list with unused
|
121
|
+
# @return [Array] an list with unused route_params
|
122
122
|
#
|
123
123
|
def replace_segments
|
124
|
-
@unused =
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
else
|
130
|
-
uri_segment
|
131
|
-
end
|
124
|
+
@unused = route_params.map do |key, value|
|
125
|
+
next key unless path.include?(key)
|
126
|
+
|
127
|
+
path.gsub!(key, value.to_s)
|
128
|
+
next
|
132
129
|
end.compact
|
133
130
|
end
|
134
131
|
|
135
132
|
#
|
136
|
-
# Validates that all
|
133
|
+
# Validates that all route_params have been used
|
137
134
|
#
|
138
135
|
#
|
139
|
-
# @raise [UriSegmentMismatch] when there are unused
|
136
|
+
# @raise [UriSegmentMismatch] when there are unused route_params
|
140
137
|
#
|
141
138
|
# @return [void]
|
142
139
|
#
|
143
|
-
def
|
140
|
+
def validate_route_params_used
|
144
141
|
return if replacents_used?
|
145
142
|
|
146
143
|
raise UriSegmentMismatch,
|
@@ -148,7 +145,7 @@ module StubRequests
|
|
148
145
|
end
|
149
146
|
|
150
147
|
#
|
151
|
-
# Checks that no
|
148
|
+
# Checks that no route_params are left
|
152
149
|
#
|
153
150
|
#
|
154
151
|
# @return [true,false]
|
@@ -165,26 +162,22 @@ module StubRequests
|
|
165
162
|
#
|
166
163
|
# @return [void]
|
167
164
|
#
|
168
|
-
def
|
169
|
-
return if
|
165
|
+
def validate_route_keys_replaced
|
166
|
+
return if route_keys_replaced?
|
170
167
|
|
171
168
|
raise UriSegmentMismatch,
|
172
169
|
"The URI segment(s) [#{unreplaced.join(',')}]" \
|
173
170
|
" were not replaced in template (#{path})." \
|
174
|
-
" Given
|
175
|
-
end
|
176
|
-
|
177
|
-
def segment_keys
|
178
|
-
@segment_keys ||= replacements.keys.map { |segment_key| ":#{segment_key}" }
|
171
|
+
" Given route_params=[#{route_params.keys.join(',')}]"
|
179
172
|
end
|
180
173
|
|
181
174
|
#
|
182
|
-
# Checks that all URI
|
175
|
+
# Checks that all URI keys were replaced
|
183
176
|
#
|
184
177
|
#
|
185
178
|
# @return [true,false]
|
186
179
|
#
|
187
|
-
def
|
180
|
+
def route_keys_replaced?
|
188
181
|
unreplaced.none?
|
189
182
|
end
|
190
183
|
|
data/lib/stub_requests/uri.rb
CHANGED
@@ -14,19 +14,46 @@ module StubRequests
|
|
14
14
|
# @since 0.1.2
|
15
15
|
#
|
16
16
|
module URI
|
17
|
+
#
|
18
|
+
# @return [Regexp] A pattern for matching route parameters
|
19
|
+
ROUTE_PARAM = %r{/:(\w+)/?}.freeze
|
20
|
+
|
21
|
+
#
|
22
|
+
# Extracts route parameters from a string
|
23
|
+
#
|
24
|
+
# @param [String] string a regular string to scan for route parameters
|
25
|
+
#
|
26
|
+
# @return [Array<Symbol>] an array with all route parameter keys
|
27
|
+
#
|
28
|
+
def self.route_params(string)
|
29
|
+
string.scan(ROUTE_PARAM).flatten.map(&:to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Safely joins two string without any extra ///
|
34
|
+
#
|
35
|
+
# @param [String] host the host of the URI
|
36
|
+
# @param [String] path the path of the URI
|
37
|
+
#
|
38
|
+
# @return [String] the full URI
|
39
|
+
#
|
40
|
+
def self.safe_join(host, path)
|
41
|
+
[host.chomp("/"), path.sub(%r{\A/}, "")].join("/")
|
42
|
+
end
|
43
|
+
|
17
44
|
#
|
18
45
|
# UtilityFunction to construct the full URI for a service endpoint
|
19
46
|
#
|
20
47
|
# @param [Symbol] service_id the id of a service
|
21
48
|
# @param [Symbol] endpoint_id the id of an endpoint
|
22
|
-
# @param [Hash<Symbol>]
|
49
|
+
# @param [Hash<Symbol>] route_params hash with route_params
|
23
50
|
#
|
24
51
|
# @return [Array<Service, Endpoint, String] the service, endpoint and full URI
|
25
52
|
#
|
26
|
-
def self.for_service_endpoint(service_id, endpoint_id,
|
27
|
-
service =
|
53
|
+
def self.for_service_endpoint(service_id, endpoint_id, route_params)
|
54
|
+
service = ServiceRegistry.instance.find!(service_id)
|
28
55
|
endpoint = service.endpoints.find!(endpoint_id)
|
29
|
-
uri = URI::Builder.build(service.uri, endpoint.
|
56
|
+
uri = URI::Builder.build(service.uri, endpoint.path, route_params)
|
30
57
|
|
31
58
|
[service, endpoint, uri]
|
32
59
|
end
|
@@ -36,7 +36,7 @@ module StubRequests
|
|
36
36
|
request_stub = request_stub_for_orig(request_signature)
|
37
37
|
return request_stub unless request_stub
|
38
38
|
|
39
|
-
|
39
|
+
StubRequests::StubRegistry.mark_as_responded(request_stub)
|
40
40
|
request_stub
|
41
41
|
end
|
42
42
|
end
|
data/lib/stub_requests.rb
CHANGED
@@ -26,23 +26,23 @@ require "stub_requests/uri/validator"
|
|
26
26
|
require "stub_requests/uri/builder"
|
27
27
|
require "stub_requests/configuration"
|
28
28
|
|
29
|
-
require "stub_requests/
|
30
|
-
require "stub_requests/
|
31
|
-
require "stub_requests/observable/registry"
|
29
|
+
require "stub_requests/callback"
|
30
|
+
require "stub_requests/callback_registry"
|
32
31
|
|
33
|
-
require "stub_requests/
|
34
|
-
require "stub_requests/
|
35
|
-
require "stub_requests/
|
36
|
-
require "stub_requests/metrics/registry"
|
32
|
+
require "stub_requests/endpoint_stub"
|
33
|
+
require "stub_requests/request_stub"
|
34
|
+
require "stub_requests/stub_registry"
|
37
35
|
|
38
|
-
require "stub_requests/
|
39
|
-
require "stub_requests/
|
40
|
-
require "stub_requests/
|
41
|
-
require "stub_requests/
|
42
|
-
require "stub_requests/registration/registry"
|
36
|
+
require "stub_requests/endpoints"
|
37
|
+
require "stub_requests/endpoint"
|
38
|
+
require "stub_requests/service"
|
39
|
+
require "stub_requests/service_registry"
|
43
40
|
|
44
41
|
require "stub_requests/webmock/builder"
|
45
42
|
require "stub_requests/webmock/stub_registry_extension"
|
46
43
|
|
47
44
|
require "stub_requests/api"
|
48
45
|
require "stub_requests/stub_requests"
|
46
|
+
require "stub_requests/dsl/method_definition"
|
47
|
+
require "stub_requests/dsl/define_method"
|
48
|
+
require "stub_requests/dsl"
|
data/lib/tasks/changelog.rake
CHANGED
@@ -13,7 +13,7 @@ task :changelog do
|
|
13
13
|
--token
|
14
14
|
]
|
15
15
|
CHECKOUT_CHANGELOG_CMD ||= "git checkout -B update-changelog"
|
16
|
-
|
16
|
+
ADD_CHANGELOG_CMD ||= "git add --all"
|
17
17
|
COMMIT_CHANGELOG_CMD ||= "git commit -a -m 'Update changelog'"
|
18
18
|
GIT_PUSH_CMD ||= "git push -u origin update-changelog"
|
19
19
|
OPEN_PR_CMD ||= "hub pull-request -b master -m 'Update Changelog' -a mhenrixon -l changelog"
|
@@ -21,6 +21,7 @@ task :changelog do
|
|
21
21
|
|
22
22
|
sh(*CHANGELOG_CMD.push(ENV["CHANGELOG_GITHUB_TOKEN"]))
|
23
23
|
sh(CHECKOUT_CHANGELOG_CMD)
|
24
|
+
sh(ADD_CHANGELOG_CMD)
|
24
25
|
sh(COMMIT_CHANGELOG_CMD)
|
25
26
|
sh(GIT_PUSH_CMD)
|
26
27
|
sh(OPEN_PR_CMD)
|