Created
February 8, 2010 11:14
-
-
Save pete-otaqui/298057 to your computer and use it in GitHub Desktop.
Sinatra app for loading YAML serialized HTTP responses (404s 500s 302s etc)
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
# Usage: | |
# rake response:capture url=http://google.com/ filename=google_200 | |
# rake response:capture url=http://google.com/404 filename=google_404 | |
require 'rubygems' | |
require 'net/http' | |
require 'uri' | |
namespace :response do | |
desc "Response management" | |
task :capture, :url, :filename do |t,args| | |
filepath = '../responses/' | |
filename = args.filename | |
filename += '.http' unless filename =~ /\.http$/ | |
puts "Capturing '#{args.url}'" | |
puts "Will save as '#{filepath}#{filename}'" | |
uri = URI.parse(args.url) | |
proxy = Net::HTTP::Proxy('www-cache.reith.bbc.co.uk',80) | |
proxy.start(uri.host) do |h| | |
req = Net::HTTP::Get.new(uri.path) | |
response = h.request(req) | |
serial = response.to_yaml | |
file = File.new("#{filepath}#{filename}","w") | |
file.write(serial) | |
file.close | |
puts "Saved #{filepath}#{filename}" | |
response | |
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
# Sinatra app for loading serialized | |
# YAML responses from a directory mapped | |
# to a base URL. | |
# | |
# Useful for faking service call responses | |
# where you can swap out the text files | |
# without restarting Sinatra | |
require 'rubygems' | |
require 'sinatra' | |
get %r{(.*)} do | |
path = params[:captures].first | |
path = 'index' unless path != '/' | |
response = YAML.load_file("responses/#{path}") | |
status response.code | |
headers response.instance_variable_get('@header') | |
response.instance_variable_get('@body') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment