Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created November 6, 2010 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cheeyeo/665582 to your computer and use it in GitHub Desktop.
Save cheeyeo/665582 to your computer and use it in GitHub Desktop.
Example on how to mock out an API call using RSpec and EM:Http
before :each do
@url = 'http://sns.us-east-1.amazonaws.com:80/?Action=ListTopics&Signature=ItTAjeexIPC43pHMZLCL7utnpK8j8AbTUZ3KGUSMzNc%3D&AWSAccessKeyId=123456&Timestamp=123&SignatureVersion=2&SignatureMethod=HmacSHA256'
EventMachine::MockHttpRequest.reset_registry!
EventMachine::MockHttpRequest.reset_counts!
EventMachine::MockHttpRequest.pass_through_requests = false #set to false to not hit the actual API endpoint
end
it 'should be able to access the API endpoint' do
EventMachine::MockHttpRequest.use {
data = <<-RESPONSE.gsub(/^ +/, '')
HTTP/1.0 200 OK
Date: Mon, 16 Nov 2009 20:39:15 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Via: 1.0 .:80 (squid)
Connection: close
This is the response back from the server; fill in as you deem fit
RESPONSE
EventMachine::MockHttpRequest.register(@url,:get,{},data)
EM.run{
d = < my method here >
d.callback{
EM::HttpRequest.count(@url, :get).should == 1
EM.stop
}
d.errback{|error|
EM.stop
}
}
} #end EM:MockHttpRequest block
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment