Created
July 10, 2013 13:49
-
-
Save hsanjuan/5966414 to your computer and use it in GitHub Desktop.
EphemeralResponse monkey patch. This patches ephemeralResponse so that request are saved with an associated request number and replayed in order afterwards. The usecase is to support that requests to the same endpoint return different values in different moments.
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
# Standard ephemeralResponse, although very simple idea, does not respond to our needs | |
# because it expects that an endpoint will always return the same data | |
# whether our endpoints return different data because the data is modified in the tests | |
# We monkey patch fixtures so that data is saved and loaded with a request number | |
# attached. This allows us to store data for every request | |
module EphemeralResponse | |
$requestnumber = 0 | |
class Fixture | |
def self.load_all | |
clear | |
if File.directory?(Configuration.effective_directory) | |
Dir.glob("#{Configuration.effective_directory}/*.yml").each do |f| | |
number = f.split('_').last().split('.').first() | |
load_fixture(f, number) | |
end | |
end | |
fixtures | |
end | |
def self.respond_to(uri, request, request_block) | |
$requestnumber += 1 | |
fixture = find_or_initialize(uri, request) | |
if fixture.new? | |
fixture.response = yield | |
fixture.response.instance_variable_set(:@body, fixture.response.body.to_s) | |
fixture.register | |
elsif request_block | |
request_block.call fixture.response | |
end | |
fixture.response | |
end | |
def identifier | |
#registered_identifier | |
$requestnumber.to_s | |
end | |
def self.load_fixture(file_name, id) | |
if fixture = YAML.load_file(file_name) | |
register fixture, id | |
else | |
EphemeralResponse::Configuration.debug_output.puts "EphemeralResponse couldn't load fixture: #{file_name}" | |
end | |
end | |
def self.register(fixture, id=nil) | |
id ||= $requestnumber | |
if fixture.expired? | |
FileUtils.rm_f fixture.path | |
else | |
fixtures[id] = fixture | |
end | |
end | |
end | |
end | |
EphemeralResponse.configure do |config| | |
config.skip_expiration = true | |
config.register('localhost') do |request| | |
"#{$requestnumber}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment