-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nepalez/v0.1.0
Add stubbing of http requests via webmock
- Loading branch information
Showing
15 changed files
with
289 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Gem::Specification.new do |gem| | ||
gem.name = "fixturama" | ||
gem.version = "0.0.7" | ||
gem.version = "0.1.0" | ||
gem.author = "Andrew Kozin (nepalez)" | ||
gem.email = "[email protected]" | ||
gem.homepage = "https://github.com/nepalez/fixturama" | ||
|
@@ -15,9 +15,10 @@ Gem::Specification.new do |gem| | |
|
||
gem.add_runtime_dependency "factory_bot", "~> 4.0" | ||
gem.add_runtime_dependency "rspec", "~> 3.0" | ||
gem.add_runtime_dependency "hashie", "~> 3.6" | ||
gem.add_runtime_dependency "hashie", "~> 3.0" | ||
gem.add_runtime_dependency "webmock", "~> 3.0" | ||
|
||
gem.add_development_dependency "rake", "~> 10" | ||
gem.add_development_dependency "rspec-its", "~> 1.2" | ||
gem.add_development_dependency "rspec-its", "~> 1.0" | ||
gem.add_development_dependency "rubocop", "~> 0.49" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# | ||
# Stubbed request | ||
# | ||
class Fixturama::Stubs::Request | ||
require_relative "request/response" | ||
require_relative "request/responses" | ||
|
||
def to_s | ||
"#{http_method.upcase} #{uri.to_s == "" ? "*" : uri}" | ||
end | ||
alias to_str to_s | ||
|
||
# every stub is unique | ||
alias key hash | ||
def update!(_); end | ||
|
||
def apply!(example) | ||
stub = example.stub_request(http_method, uri) | ||
stub = stub.with(request) if request.any? | ||
stub.to_return { |_| responses.next } | ||
end | ||
|
||
private | ||
|
||
attr_reader :options | ||
|
||
def initialize(options) | ||
@options = options | ||
with_error { @options = Hash(options).symbolize_keys } | ||
end | ||
|
||
HTTP_METHODS = %i[get post put patch delete head options any].freeze | ||
|
||
def http_method | ||
value = with_error("method") { options[:method]&.to_sym&.downcase } || :any | ||
return value if HTTP_METHODS.include?(value) | ||
|
||
raise ArgumentError, "Invalid HTTP method #{value} in #{@optons}" | ||
end | ||
|
||
def uri | ||
with_error("uri") { maybe_regexp(options[:uri] || options[:url]) } | ||
end | ||
|
||
def headers | ||
with_error("headers") do | ||
Hash(options[:headers]).transform_keys(&:to_s) if options.key?(:headers) | ||
end | ||
end | ||
|
||
def query | ||
with_error("query") do | ||
Hash(options[:query]).transform_keys(&:to_s) if options.key?(:query) | ||
end | ||
end | ||
|
||
def body | ||
with_error("body") do | ||
case options[:body] | ||
when NilClass then nil | ||
when Hash then options[:body] | ||
else maybe_regexp(options[:body]) | ||
end | ||
end | ||
end | ||
|
||
def basic_auth | ||
with_error("basic auth") do | ||
value = options[:auth] || options[:basic_auth] | ||
Hash(value).transform_keys(&:to_s).values_at("user", "pass") if value | ||
end | ||
end | ||
|
||
def request | ||
@request ||= { | ||
headers: headers, | ||
body: body, | ||
query: query, | ||
basic_auth: basic_auth | ||
}.select { |_, val| val } | ||
end | ||
|
||
def responses | ||
@responses ||= Responses.new(options[:response] || options[:responses]) | ||
end | ||
|
||
def with_error(part = nil) | ||
yield | ||
rescue RuntimeError | ||
message = ["Cannot extract a request", part, "from #{options}"].join(" ") | ||
raise ArgumentError, message, __FILE__, __LINE__ - 1 | ||
end | ||
|
||
def maybe_regexp(str) | ||
str = str.to_s | ||
str[%r{\A/.*/\z}] ? Regexp.new(str[1..-2]) : str | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Fixturama::Stubs::Request | ||
class Response | ||
def to_h | ||
{ status: status, body: body, headers: headers }.select { |_, val| val } | ||
end | ||
|
||
private | ||
|
||
def initialize(options) | ||
@options = options | ||
@options = with_error { Hash(options).transform_keys(&:to_sym) } | ||
end | ||
|
||
attr_reader :options | ||
|
||
def status | ||
with_error("status") { options[:status]&.to_i } || 200 | ||
end | ||
|
||
def body | ||
with_error("body") do | ||
case options[:body] | ||
when NilClass then nil | ||
when Hash then JSON.dump(options[:body]) | ||
else options[:body].to_s | ||
end | ||
end | ||
end | ||
|
||
def headers | ||
with_error("headers") do | ||
Hash(options[:headers]).map { |k, v| [k.to_s, v.to_s] }.to_h | ||
end | ||
end | ||
|
||
def with_error(part = nil) | ||
yield | ||
rescue RuntimeError | ||
text = ["Cannot extract a response", part, "from #{options}"].join(" ") | ||
raise ArgumentError, text, __FILE__, __LINE__ - 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Fixturama::Stubs::Request | ||
class Responses | ||
def next | ||
list.count > @count ? list[@count].tap { @count += 1 } : list.last | ||
end | ||
|
||
private | ||
|
||
def initialize(list) | ||
@count = 0 | ||
list ||= [{ status: 200 }] | ||
@list = case list | ||
when Array then list.map { |item| Response.new(item).to_h } | ||
else [Response.new(list).to_h] | ||
end | ||
end | ||
|
||
attr_reader :list | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.