lib/bearcat/spec_helpers.rb in bearcat-1.3.47 vs lib/bearcat/spec_helpers.rb in bearcat-1.3.48
- old
+ new
@@ -11,10 +11,60 @@
# 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, **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?
+
+ bits.map do |bit|
+ case bit
+ when Regexp
+ bit.source
+ when String
+ Regexp.escape(bit)
+ end
+ end.join
+ when String
+ Regexp.escape(endpoint)
+ when Regexp
+ endpoint.source
+ end
+
+ url = Regexp.escape(resolve_prefix(prefix)) + url
+ stub = stub_request(match[:method].to_sym, Regexp.new(url))
+
+ # Override the to_return method to accept a Hash as body:
+ stub.define_singleton_method(:to_return, ->(*resps) {
+ resps.map do |resp|
+ resp[:headers] ||= {}
+ resp[:headers]["Content-Type"] ||= "application/json"
+ resp[:body] = resp[:body].to_json
+ end
+ super(*resps)
+ })
+
+ stub
+ end
+
+ private
+
+ def 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
@@ -27,35 +77,7 @@
when Bearcat::Client
prefix.prefix
when String
prefix
end
-
- ruby_method = Bearcat::Client.instance_method(endpoint)
- match = SOURCE_REGEX.match(ruby_method.source)
- url = match[:url]
- url = url.gsub(/#\{(?<key>.*?)\}/) do |match|
- match = Regexp.last_match
- v = kwargs[match[:key].to_sym]
- case v
- when nil
- '\\w+'
- when Regexp
- v.source
- when String
- Regexp.escape(v)
- end
- end
- url = prefix + url if prefix.present?
- url_pattern = Regexp.new(url)
- stub = stub_request(match[:method].to_sym, url_pattern)
- stub.define_singleton_method(:to_return, ->(*resps) {
- resps.map do |resp|
- resp[:headers] ||= {}
- resp[:headers]["Content-Type"] ||= "application/json"
- resp[:body] = resp[:body].to_json
- end
- super(*resps)
- })
- stub
end
end
\ No newline at end of file