Skip to content

Instantly share code, notes, and snippets.

@brandonhilkert
Created October 19, 2013 21:53
Show Gist options
  • Save brandonhilkert/7062021 to your computer and use it in GitHub Desktop.
Save brandonhilkert/7062021 to your computer and use it in GitHub Desktop.
require 'sinatra/base'
class FakeGitHub < Sinatra::Base
def simulate_error!
@@simulate_error = true
end
def reset!
@@simulate_error = false
end
def simulate_error?
@@simulate_error
end
before_do
if simulate_error?
halt 403, { errors: { first_name: "can't be blank" } }.to_json
end
end
get '/repos/:organization/:project/contributors' do
json_response 200, 'contributors.json'
end
get '/repos/:organization/:project/issues' do
json_response 200, 'issues.json'
end
private
def json_response(response_code, file_name)
content_type :json
status response_code
File.open(File.dirname(__FILE__) + '/fixtures/' + file_name, 'rb').read
end
end
feature 'External request' do
it 'recovers from GitHub errors' do
FakeGithub.simulate_error!
uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')
response = JSON.load(Net::HTTP.get(uri))
# some assertions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment