lib/bearcat/spec_helpers.rb in bearcat-1.4.13 vs lib/bearcat/spec_helpers.rb in bearcat-1.5.0.beta1
- old
+ new
@@ -14,41 +14,15 @@
# Helper method to Stub Bearcat requests.
# Automagically parses the Bearcat method source to determine the correct URL to stub.
# Accepts optional keyword parameters to interpolate specific values into the URL.
# Returns a mostly-normal Webmock stub (:to_return has been overridden to allow :body to be set to a Hash)
def stub_bearcat(endpoint, prefix: nil, method: :auto, **kwargs)
- url = case endpoint
- when Symbol
- ruby_method = Bearcat::Client.instance_method(endpoint)
- match = SOURCE_REGEX.match(ruby_method.source)
- bits = []
- url = match[:url].gsub(/\/$/, '')
- lend = 0
- url.scan(/#\{(?<key>.*?)\}/) do |key|
- m = Regexp.last_match
- between = url[lend..m.begin(0)-1]
- bits << between if between.present? && (m.begin(0) > lend)
- lend = m.end(0)
- bits << (kwargs[m[:key].to_sym] || /\w+/)
- end
- between = url[lend..-1]
- bits << between if between.present?
+ cfg = _bearcat_resolve_config(endpoint, method: method)
- bits.map do |bit|
- next bit.source if bit.is_a?(Regexp)
- bit = bit.canvas_id if bit.respond_to?(:canvas_id)
- Regexp.escape(bit.to_s)
- end.join
- when String
- Regexp.escape(endpoint)
- when Regexp
- endpoint.source
- end
+ url = Regexp.escape(_bearcat_resolve_prefix(prefix)) + cfg[:url]
+ stub = stub_request(cfg[:method], Regexp.new(url))
- url = Regexp.escape(resolve_prefix(prefix)) + url
- stub = stub_request(method == :auto ? (match ? match[:method].to_sym : :get) : method, Regexp.new(url))
-
# Override the to_return method to accept a Hash as body:
stub.define_singleton_method(:to_return, ->(*resps, &blk) {
if blk
super do |*args|
resp = blk.call(*args)
@@ -70,11 +44,68 @@
stub
end
private
- def resolve_prefix(prefix)
+ def _bearcat_resolve_config(endpoint, url_context, method: :auto)
+ case endpoint
+ when Symbol
+ if (cfg = Bearcat::Client.registered_endpoints[endpoint]).present?
+ bits = []
+ url = cfg[:url]
+ lend = 0
+ url.scan(/:(?<key>\w+)/) do |key|
+ m = Regexp.last_match
+ between = url[lend..m.begin(0)-1]
+ bits << between if between.present? && (m.begin(0) > lend)
+ lend = m.end(0)
+ bits << (url_context[m[:key].to_sym] || /\w+/)
+ end
+ between = url[lend..-1]
+ bits << between if between.present?
+
+ url = bits.map do |bit|
+ next bit.source if bit.is_a?(Regexp)
+ bit = bit.canvas_id if bit.respond_to?(:canvas_id)
+ Regexp.escape(bit.to_s)
+ end.join
+
+ { method: method == :auto ? cfg[:method] : method, url: url }
+ else
+ ruby_method = Bearcat::Client.instance_method(endpoint)
+ match = SOURCE_REGEX.match(ruby_method.source)
+ bits = []
+ url = match[:url].gsub(/\/$/, '')
+ lend = 0
+ url.scan(/#\{(?<key>.*?)\}/) do |key|
+ m = Regexp.last_match
+ between = url[lend..m.begin(0)-1]
+ bits << between if between.present? && (m.begin(0) > lend)
+ lend = m.end(0)
+ bits << (url_context[m[:key].to_sym] || /\w+/)
+ end
+ between = url[lend..-1]
+ bits << between if between.present?
+
+ url = bits.map do |bit|
+ next bit.source if bit.is_a?(Regexp)
+ bit = bit.canvas_id if bit.respond_to?(:canvas_id)
+ Regexp.escape(bit.to_s)
+ end.join
+
+ { method: method == :auto ? (match ? match[:method].to_sym : :get) : method, url: url }
+ end
+ when String
+ raise "Cannot use method :auto when passing string endpoint" if method == :auto
+ { method: method, url: Regexp.escape(endpoint) }
+ when Regexp
+ raise "Cannot use method :auto when passing regex endpoint" if method == :auto
+ { method: method, url: Regexp.escape(endpoint) }
+ end
+ end
+
+ def _bearcat_resolve_prefix(prefix)
if prefix == true
prefix = canvas_api_client if defined? canvas_api_client
prefix = canvas_sync_client if defined? canvas_sync_client
end
prefix = case prefix
@@ -88,6 +119,6 @@
prefix.prefix
when String
prefix
end
end
-end
\ No newline at end of file
+end