Rack Doubles
Rack Doubles is a simpler clone of rack-stubs a rack middleware for stubbing reponses to GET requests on HTTP web services Rack Doubles uses the HTTP PUT request method to create stubs
Installation
gem install rack_doubles
Rack Doubles middleware
Rack Doubles is implemented as rack middleware, so you would typically host it as part of a rack web app. You can write a rack configuration (config.ru
) like this if you want to get started:
require 'rubygems'
require 'rack_doubles'
use RackDoubles::Middleware
run lambda { |e| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
Then you can run the rackup binary:
rackup
...and you'll have a server with Rack Doubles running at http://127.0.0.1:9292
You create a stubbed response for a resource by issuing a PUT request to that resource location with a body that represents the tuple of http response you want to receive with a subsequent GET or POST request.
For example you want a HTTP GET requests to /some-service
to return an HTTP 200
status, with the body "Hello World"
and the Content-Type:"text/plain"
.
Rack requires a Content-Type
to be set for all responses so failure to include one in the stub request will generate a 400
response
Using a HTTP request tool such as rest-client or httpparty you make the following request:
method : PUT
action : http://127.0.0.1:9292/some-service
body : [200, { "Content-Type": "text/plain" }, ["Hello World"]]
headers : Content-Type=application/json
All subsequent GET requests to http://127.0.0.1:9292/some-service should now return the resonse:
HTTP 200 status,
Content-Type=text/plain
"Hello World"
The body of the PUT request is json, and is implemented as a hash of simple rack-style response tuples.
A list of all stubbed responses can be obtainined as a JSON representation by sending a GET request to '/'.
put /example, [200, { "Content-Type": "text/plain" }, ["Hello World"]]
then
get /
and the response body is:
{'/example: [200, { "Content-Type": "text/plain" }, ["Hello World"]] }
Individual stub responses can be removed by sending a DELETE request to their resource location, to remove all stubs send a DELETE request to '/'.
Logging
For basic mocking purposes individual counts for all the requests made to the stub service can be obtained by making a GET request to the url '/log':
put /example, [200, { "Content-Type": "text/plain" }, ["Hello World"]]
get /example
get /another-example
get /another-example
then
get /log
and the response body is:
{"/example": {"200": 1}, "/another-example": {"404": 2}}
Rack Doubles client
To use Rack Doubles from Ruby without invoking a HTTP PUT request, Rack Doubles includes a small client library.
The Client takes as it's first argument an object that responds to #url
.
Alternatively the #create
method will attempt to construct a Client from a range of argument types including String, Hash, and Objects that respond to #hostname
.
To set up the a stub on a GET request to '/some-service' instantiate a client and stub the path to the resource:
client = RackDoubles::Client.new(OpenStruct.new({ url: "http://127.0.0.1:9292" }))
client.stub("/some-service").to_return(200, { "Content-Type" => "text/plain" }, ["Hello World"])
or alternatively
client = RackDoubles::Client.create("http://127.0.0.1:9292")
client.stub("/some-service").to_return(200, { "Content-Type" => "text/plain" }, ["That's all folks"])
To delete individual stubs use:
client.delete '/some-service'
and to delete all stub responses use:
client.delete_all