rack_doubles 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.md +90 -0
- data/Rakefile +1 -0
- data/config.ru +14 -0
- data/features/step_definitions/stub_responses_steps.rb +44 -0
- data/features/stub_responses.feature +95 -0
- data/features/support/env.rb +14 -0
- data/lib/rack_doubles/client.rb +43 -0
- data/lib/rack_doubles/middleware.rb +100 -0
- data/lib/rack_doubles/version.rb +3 -0
- data/lib/rack_doubles.rb +11 -0
- data/rack_doubles.gemspec +32 -0
- data/spec/rack_doubles/client_spec.rb +34 -0
- data/spec/rack_doubles/middleware_spec.rb +56 -0
- data/spec/spec_helper.rb +3 -0
- metadata +179 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Rack Doubles
|
2
|
+
|
3
|
+
Rack Doubles is a simpler clone of [rack-stubs](https://github.com/featurist/rack-stubs) a rack middleware for stubbing reponses to GET requests on HTTP web services
|
4
|
+
Rack Doubles uses the HTTP PUT request method to create stubs
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
gem install rack_doubles
|
9
|
+
|
10
|
+
## Rack Doubles middleware
|
11
|
+
|
12
|
+
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:
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'rack_doubles'
|
16
|
+
|
17
|
+
use RackDoubles::Middleware
|
18
|
+
run lambda { |e| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
|
19
|
+
|
20
|
+
Then you can run the rackup binary:
|
21
|
+
|
22
|
+
rackup
|
23
|
+
|
24
|
+
...and you'll have a server with Rack Doubles running at http://127.0.0.1:9292
|
25
|
+
|
26
|
+
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.
|
27
|
+
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"`.
|
28
|
+
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
|
29
|
+
Using a HTTP request tool such as [rest-client](https://github.com/archiloque/rest-client) or [httpparty](http://johnnunemaker.com/httparty) you make the following request:
|
30
|
+
|
31
|
+
method : PUT
|
32
|
+
action : http://127.0.0.1:9292/some-service
|
33
|
+
body : [200, { "Content-Type": "text/plain" }, ["Hello World"]]
|
34
|
+
headers : Content-Type=application/json
|
35
|
+
|
36
|
+
All subsequent GET requests to http://127.0.0.1:9292/some-service should now return the resonse:
|
37
|
+
|
38
|
+
HTTP 200 status,
|
39
|
+
Content-Type=text/plain
|
40
|
+
"Hello World"
|
41
|
+
|
42
|
+
The body of the PUT request is json, and is implemented as a hash of simple rack-style response tuples.
|
43
|
+
|
44
|
+
A list of all stubbed responses can be obtainined as a JSON representation by sending a GET request to '/'.
|
45
|
+
|
46
|
+
put /example, [200, { "Content-Type": "text/plain" }, ["Hello World"]]
|
47
|
+
|
48
|
+
then
|
49
|
+
|
50
|
+
get /
|
51
|
+
|
52
|
+
and the response body is:
|
53
|
+
|
54
|
+
{'/example: [200, { "Content-Type": "text/plain" }, ["Hello World"]] }
|
55
|
+
|
56
|
+
Individual stub responses can be removed by sending a DELETE request to their resource location, to remove all stubs send a DELETE request to '/'.
|
57
|
+
|
58
|
+
## Logging
|
59
|
+
|
60
|
+
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':
|
61
|
+
|
62
|
+
put /example, [200, { "Content-Type": "text/plain" }, ["Hello World"]]
|
63
|
+
|
64
|
+
get /example
|
65
|
+
get /another-example
|
66
|
+
get /another-example
|
67
|
+
|
68
|
+
then
|
69
|
+
|
70
|
+
get /log
|
71
|
+
|
72
|
+
and the response body is:
|
73
|
+
|
74
|
+
{"/example": {"200": 1}, "/another-example": {"404": 2}}
|
75
|
+
|
76
|
+
## Rack Doubles client
|
77
|
+
|
78
|
+
To use Rack Doubles from Ruby without invoking a HTTP PUT request, Rack Doubles includes a small client library.
|
79
|
+
To set up the same stub on a GET request to 'some-service' you would use the Rack Doubles client like this:
|
80
|
+
|
81
|
+
client = RackDoubles::Client.new("http://127.0.0.1:9292")
|
82
|
+
client.stub("/some-service").to_return(200, { "Content-Type" => "text/plain" }, ["Hello World"])
|
83
|
+
|
84
|
+
To delete individual stubs use:
|
85
|
+
|
86
|
+
client.delete some-service
|
87
|
+
|
88
|
+
and to delete all stub responses use:
|
89
|
+
|
90
|
+
client.delete_all
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/config.ru
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rack_doubles'
|
2
|
+
|
3
|
+
root=File.dirname(__FILE__)
|
4
|
+
|
5
|
+
map '/' do
|
6
|
+
run lambda { |e| [200, { 'Content-Type' => 'text/plain' }, ["Rack service is up"]] }
|
7
|
+
end
|
8
|
+
|
9
|
+
map '/rack-doubles' do
|
10
|
+
use RackDoubles::Middleware do
|
11
|
+
map to: '/rack-doubles'
|
12
|
+
end
|
13
|
+
run lambda { |e| [404, { 'Content-Type' => 'text/plain' }, ["404: Rack Doubles Stub Not Found"]] }
|
14
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
Transform /^(\d\d\d)$/ do |number|
|
2
|
+
number.to_i
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^all responses have been cleared$/ do
|
6
|
+
RackDoubles::Middleware.responses.clear
|
7
|
+
RackDoubles::Middleware.requests.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
Given /^a GET request to "([^"]*)" returns "(\d+)" with the body "([^"]*)"$/ do |path, status, body|
|
11
|
+
get path
|
12
|
+
last_response.body.should == body
|
13
|
+
last_response.status.should == status
|
14
|
+
end
|
15
|
+
|
16
|
+
When /^I make a PUT request to "([^"]*)" with the body:$/ do |path, body|
|
17
|
+
put path, body, { 'Content-Type' => 'application/json' }
|
18
|
+
end
|
19
|
+
|
20
|
+
When /^I make a DELETE request to "([^"]*)"$/ do |path|
|
21
|
+
delete path
|
22
|
+
end
|
23
|
+
|
24
|
+
Then /^the response returns "([^"]*)" with the body:$/ do |status, body|
|
25
|
+
last_response.status.should == status
|
26
|
+
JSON.parse(last_response.body).should == JSON.parse(body)
|
27
|
+
end
|
28
|
+
|
29
|
+
Then /^the response returns "([^"]*)" with the body "([^"]*)"$/ do |status, body|
|
30
|
+
last_response.status.should == status
|
31
|
+
JSON.parse(last_response.body).should == JSON.parse(body)
|
32
|
+
end
|
33
|
+
|
34
|
+
Then /^a GET request to "([^"]*)" returns "([^"]*)" with the body:$/ do |path, status, body|
|
35
|
+
get path
|
36
|
+
last_response.status.should == status
|
37
|
+
JSON.parse(last_response.body).should == JSON.parse(body)
|
38
|
+
end
|
39
|
+
|
40
|
+
Then /^a GET request to "([^"]*)" returns "([^"]*)" with the head:$/ do |path, status, head|
|
41
|
+
get path
|
42
|
+
last_response.status.should == status
|
43
|
+
last_response.header.should include({"Location"=>"http://www.example.com"})
|
44
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
Feature: Stub Responses
|
2
|
+
In order to simulate behaviour of third-party web services
|
3
|
+
As a test for my application
|
4
|
+
I want to simulate http service behaviour
|
5
|
+
|
6
|
+
Background:
|
7
|
+
Given all responses have been cleared
|
8
|
+
Given a GET request to "/example-service" returns "404" with the body "Not Found"
|
9
|
+
And a GET request to "/another-example-service" returns "404" with the body "Not Found"
|
10
|
+
|
11
|
+
Scenario: Stub a url end point
|
12
|
+
When I make a PUT request to "/example-service" with the body:
|
13
|
+
"""
|
14
|
+
[200, { "Content-Type": "text/plain" }, ["results"]]
|
15
|
+
"""
|
16
|
+
Then the response returns "201" with the body:
|
17
|
+
"""
|
18
|
+
{ "url": "/example-service", "response": [200, { "Content-Type": "text/plain" }, ["results"]] }
|
19
|
+
"""
|
20
|
+
And a GET request to "/example-service" returns "200" with the body "results"
|
21
|
+
And a GET request to "/" returns "200" with the body:
|
22
|
+
"""
|
23
|
+
{ "/example-service": [200, { "Content-Type": "text/plain" }, ["results"]] }
|
24
|
+
"""
|
25
|
+
And a GET request to "/log" returns "200" with the body:
|
26
|
+
"""
|
27
|
+
{"/example-service": {"404": 1, "200": 1}, "/another-example-service": {"404": 1}}
|
28
|
+
"""
|
29
|
+
|
30
|
+
Scenario: Returns 400 if the stub request fails to contain a Content-Type pair
|
31
|
+
When I make a PUT request to "/example-service" with the body:
|
32
|
+
"""
|
33
|
+
[200, { }, ["results"]]
|
34
|
+
"""
|
35
|
+
Then the response returns "400" with the body:
|
36
|
+
"""
|
37
|
+
{"reason": "Subbing a response requires the body of the PUT request to contain a tuple with the response status and header Content-Type"}
|
38
|
+
"""
|
39
|
+
|
40
|
+
Scenario: Stub a url end point to 302
|
41
|
+
When I make a PUT request to "/redirect-this-request" with the body:
|
42
|
+
"""
|
43
|
+
[302, { "Content-Type": "text/plain", "Location": "http://www.example.com" }, []]
|
44
|
+
"""
|
45
|
+
Then the response returns "201" with the body:
|
46
|
+
"""
|
47
|
+
{ "url": "/redirect-this-request", "response": [302, { "Content-Type": "text/plain", "Location": "http://www.example.com" }, []] }
|
48
|
+
"""
|
49
|
+
And a GET request to "/redirect-this-request" returns "302" with the head:
|
50
|
+
"""
|
51
|
+
{ "Content-Type": "text/plain", "Location": "http://www.example.com" }
|
52
|
+
"""
|
53
|
+
|
54
|
+
Scenario: Clear all stub responses
|
55
|
+
When I make a PUT request to "/example-service" with the body:
|
56
|
+
"""
|
57
|
+
[200, {}, ["Another response"]]
|
58
|
+
"""
|
59
|
+
When I make a DELETE request to "/"
|
60
|
+
Then the response returns "200" with the body "{}"
|
61
|
+
And a GET request to "/" returns "200" with the body "{}"
|
62
|
+
And a GET request to "/example-service" returns "404" with the body "Not Found"
|
63
|
+
And a GET request to "/log" returns "200" with the body:
|
64
|
+
"""
|
65
|
+
{"/example-service": {"404": 2}, "/another-example-service": {"404": 1}}
|
66
|
+
"""
|
67
|
+
|
68
|
+
Scenario: Clear single stub responses
|
69
|
+
And I make a PUT request to "/example-service" with the body:
|
70
|
+
"""
|
71
|
+
[200, { "Content-Type": "text/plain" }, ["Example response"]]
|
72
|
+
"""
|
73
|
+
And I make a PUT request to "/another-example-service" with the body:
|
74
|
+
"""
|
75
|
+
[200, { "Content-Type": "text/plain" }, ["Another response"]]
|
76
|
+
"""
|
77
|
+
When I make a DELETE request to "/example-service"
|
78
|
+
Then the response returns "200" with the body:
|
79
|
+
"""
|
80
|
+
{"/another-example-service": [200, { "Content-Type": "text/plain" }, ["Another response"]]}
|
81
|
+
"""
|
82
|
+
And a GET request to "/example-service" returns "404" with the body "Not Found"
|
83
|
+
And a GET request to "/" returns "200" with the body:
|
84
|
+
"""
|
85
|
+
{"/another-example-service": [200, { "Content-Type": "text/plain" }, ["Another response"]]}
|
86
|
+
"""
|
87
|
+
And a GET request to "/log" returns "200" with the body:
|
88
|
+
"""
|
89
|
+
{"/example-service": {"404": 2}, "/another-example-service": {"404": 1}}
|
90
|
+
"""
|
91
|
+
|
92
|
+
Scenario: Clear the log
|
93
|
+
When I make a DELETE request to "/log"
|
94
|
+
Then the response returns "200" with the body "{}"
|
95
|
+
Then a GET request to "/log" returns "200" with the body "{}"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$: << './lib/rack_doubles'
|
2
|
+
require 'rack'
|
3
|
+
require 'middleware'
|
4
|
+
require 'rack/test'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
|
8
|
+
module RackDoubles
|
9
|
+
def app
|
10
|
+
RackDoubles::Middleware.new(lambda{ |env| [404, {}, 'Not Found'] })
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
World(Rack::Test::Methods, RackDoubles)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module RackDoubles
|
4
|
+
|
5
|
+
class Client
|
6
|
+
|
7
|
+
def initialize(service, http_client = RestClient)
|
8
|
+
@service = service
|
9
|
+
@http_client = http_client
|
10
|
+
end
|
11
|
+
|
12
|
+
def stub resource
|
13
|
+
@stub = Stub.new url_for(resource), @http_client
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete resource
|
17
|
+
@http_client.delete url_for(resource)
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete_all
|
21
|
+
@http_client.delete url_for('/')
|
22
|
+
end
|
23
|
+
|
24
|
+
def url_for path
|
25
|
+
@service.gsub(/\/+$/, '') + '/' + path.gsub(/^\/+/, '')
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class Stub
|
31
|
+
|
32
|
+
def initialize url, http_client
|
33
|
+
@url = url
|
34
|
+
@http_client = http_client
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_return status, headers, body
|
38
|
+
@http_client.put(@url, [200, headers, [body]].to_json, { 'Content-Type' => 'application/json' })
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RackDoubles
|
4
|
+
|
5
|
+
class Middleware
|
6
|
+
|
7
|
+
@responses = {}
|
8
|
+
@requests = Hash.new { |hash, key| hash[key] = Hash.new(0) }
|
9
|
+
|
10
|
+
class << self; attr_accessor :responses, :requests, :errors; end
|
11
|
+
|
12
|
+
def responses
|
13
|
+
self.class.responses
|
14
|
+
end
|
15
|
+
|
16
|
+
def requests
|
17
|
+
self.class.requests
|
18
|
+
end
|
19
|
+
|
20
|
+
def errors
|
21
|
+
self.class.errors
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(app, &block)
|
25
|
+
@app = app
|
26
|
+
@path = '/'
|
27
|
+
yield self if block_given?
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_accessor :path
|
31
|
+
|
32
|
+
def call(env)
|
33
|
+
request = Rack::Request.new(env)
|
34
|
+
if request.get?
|
35
|
+
case request.path
|
36
|
+
when /#{path}\/?$/
|
37
|
+
[200, {"Content-Type" => "application/json"}, [responses.to_json]]
|
38
|
+
when /log\/?$/
|
39
|
+
[200, {"Content-Type" => "application/json"}, [requests.to_json]]
|
40
|
+
else
|
41
|
+
begin
|
42
|
+
response = responses.fetch(request.path)
|
43
|
+
record_success request.path
|
44
|
+
response
|
45
|
+
rescue KeyError
|
46
|
+
record_error request.path
|
47
|
+
@app.call(env)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
elsif request.delete?
|
51
|
+
case request.path
|
52
|
+
when path
|
53
|
+
responses.clear
|
54
|
+
[200, {"Content-Type" => "application/json"}, [responses.to_json]]
|
55
|
+
when /\/log/
|
56
|
+
requests.clear
|
57
|
+
[200, {"Content-Type" => "application/json"}, [requests.to_json]]
|
58
|
+
else
|
59
|
+
responses.delete request.path
|
60
|
+
[200, {"Content-Type" => "application/json"}, [responses.to_json]]
|
61
|
+
end
|
62
|
+
elsif request.put?
|
63
|
+
body_of_request = JSON.parse(request.body.read.to_s)
|
64
|
+
status, headers, body = body_of_request
|
65
|
+
begin
|
66
|
+
raise ArgumentError if [status, headers].any?(&:nil?) || headers['Content-Type'].nil?
|
67
|
+
responses.merge!({request.path => body_of_request})
|
68
|
+
[201, { 'Content-Type' => 'application/json' }, [%|{ "url": "#{request.path}", "response": #{responses.fetch(request.path).to_json} }|]]
|
69
|
+
rescue ArgumentError
|
70
|
+
[400, { 'Content-Type' => 'application/json' }, [%|{ "reason": "Subbing a response requires the body of the PUT request to contain a tuple with the response status and header Content-Type" }|]]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def stub path
|
76
|
+
Response.new(responses, path)
|
77
|
+
end
|
78
|
+
|
79
|
+
def record_success request_url
|
80
|
+
requests[request_url]['200'] += 1
|
81
|
+
end
|
82
|
+
|
83
|
+
def record_error request_url
|
84
|
+
requests[request_url]['404'] += 1
|
85
|
+
end
|
86
|
+
|
87
|
+
class Response
|
88
|
+
|
89
|
+
def initialize(responses, path)
|
90
|
+
@responses, @path = responses, path
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_return(status, headers, body)
|
94
|
+
@responses[@path] = [status, headers, body]
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
data/lib/rack_doubles.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rack_doubles/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rack_doubles"
|
7
|
+
spec.version = RackDoubles::VERSION
|
8
|
+
spec.author = "Anthony Green"
|
9
|
+
spec.email = "anthony.green@bbc.co.uk"
|
10
|
+
spec.homepage = "https://github.com/anthonygreen/rack_doubles"
|
11
|
+
spec.summary = "Rack middleware for stubbing responses from HTTP web services"
|
12
|
+
spec.description = "Rack middleware for creating faking responses for HTTP web services that an application under test communicates with"
|
13
|
+
|
14
|
+
spec.rubyforge_project = "rack_doubles"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split("\n")
|
17
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 1.9.3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'cucumber'
|
25
|
+
spec.add_development_dependency 'rack-test'
|
26
|
+
spec.add_development_dependency 'rest-client'
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'json'
|
29
|
+
spec.add_runtime_dependency 'rack'
|
30
|
+
spec.add_runtime_dependency 'rest-client'
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module RackDoubles
|
5
|
+
|
6
|
+
describe Client do
|
7
|
+
|
8
|
+
def http_client_should_stub(url, response)
|
9
|
+
@http_client.should_receive(:put).with(url, response, {'Content-Type' => 'application/json'})
|
10
|
+
end
|
11
|
+
|
12
|
+
before do
|
13
|
+
@http_client = mock("http_client")
|
14
|
+
@client = Client.new("http://path-to-service", @http_client)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "stubs url end points" do
|
18
|
+
http_client_should_stub("http://path-to-service/resource", [200, {}, ["This should be the response body"]].to_json)
|
19
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "deletes a stub" do
|
23
|
+
@http_client.should_receive(:delete).with("http://path-to-service/some-resource")
|
24
|
+
@client.delete 'some-resource'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "deletes all stubs" do
|
28
|
+
@http_client.should_receive(:delete).with("http://path-to-service/")
|
29
|
+
@client.delete_all
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RackDoubles
|
4
|
+
|
5
|
+
describe Middleware do
|
6
|
+
|
7
|
+
it "allows stub behaviour to be specified using the client interface" do
|
8
|
+
app = mock('app')
|
9
|
+
env = Rack::MockRequest.env_for('/some-service')
|
10
|
+
middleware = Middleware.new(app)
|
11
|
+
middleware.stub('/some-service').to_return(200, {}, 'Successful')
|
12
|
+
middleware.call(env).should == [200, {}, 'Successful']
|
13
|
+
end
|
14
|
+
|
15
|
+
it "deletes all stubbed responses" do
|
16
|
+
app = mock('app')
|
17
|
+
middleware = Middleware.new(app)
|
18
|
+
middleware.stub('/service').to_return(200, {}, 'Success')
|
19
|
+
middleware.stub('/another_service').to_return(200, {}, 'OK')
|
20
|
+
middleware.responses.clear
|
21
|
+
get_env = Rack::MockRequest.env_for('/service')
|
22
|
+
get_env2 = Rack::MockRequest.env_for('/another_service')
|
23
|
+
app.should_receive('call').with(get_env)
|
24
|
+
app.should_receive('call').with(get_env2)
|
25
|
+
middleware.call(get_env)
|
26
|
+
middleware.call(get_env2)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "deletes a single stubbed response" do
|
30
|
+
app = mock('app')
|
31
|
+
middleware = Middleware.new(app)
|
32
|
+
middleware.stub('/service').to_return(200, {}, 'Success')
|
33
|
+
middleware.stub('/another-service').to_return(200, {}, 'OK')
|
34
|
+
middleware.responses.delete('/service')
|
35
|
+
middleware.responses.delete('/service-that-does-not-exist')
|
36
|
+
get_env = Rack::MockRequest.env_for('/service')
|
37
|
+
get_env2 = Rack::MockRequest.env_for('/another-service')
|
38
|
+
app.should_receive('call').with(get_env)
|
39
|
+
app.should_not_receive('call').with(get_env2)
|
40
|
+
middleware.call(get_env)
|
41
|
+
middleware.call(get_env2)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "can map to a base path" do
|
45
|
+
app = mock('app')
|
46
|
+
env = Rack::MockRequest.env_for('/rack-doubles')
|
47
|
+
middleware = Middleware.new(app) do |app|
|
48
|
+
app.path = '/rack-doubles'
|
49
|
+
end
|
50
|
+
middleware.stub('/rack-doubles/some-service').to_return(200, {}, 'Successful')
|
51
|
+
middleware.call(env).should == [200, { "Content-Type"=>"application/json" }, [%|{"/another-service":[200,{},"OK"],"/rack-doubles/some-service":[200,{},"Successful"]}|]]
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_doubles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anthony Green
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: cucumber
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rack-test
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rest-client
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: json
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rack
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rest-client
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Rack middleware for creating faking responses for HTTP web services that
|
127
|
+
an application under test communicates with
|
128
|
+
email: anthony.green@bbc.co.uk
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- Gemfile
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- config.ru
|
138
|
+
- features/step_definitions/stub_responses_steps.rb
|
139
|
+
- features/stub_responses.feature
|
140
|
+
- features/support/env.rb
|
141
|
+
- lib/rack_doubles.rb
|
142
|
+
- lib/rack_doubles/client.rb
|
143
|
+
- lib/rack_doubles/middleware.rb
|
144
|
+
- lib/rack_doubles/version.rb
|
145
|
+
- rack_doubles.gemspec
|
146
|
+
- spec/rack_doubles/client_spec.rb
|
147
|
+
- spec/rack_doubles/middleware_spec.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
homepage: https://github.com/anthonygreen/rack_doubles
|
150
|
+
licenses: []
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
157
|
+
requirements:
|
158
|
+
- - ! '>='
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: 1.9.3
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
none: false
|
163
|
+
requirements:
|
164
|
+
- - ! '>='
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project: rack_doubles
|
169
|
+
rubygems_version: 1.8.24
|
170
|
+
signing_key:
|
171
|
+
specification_version: 3
|
172
|
+
summary: Rack middleware for stubbing responses from HTTP web services
|
173
|
+
test_files:
|
174
|
+
- features/step_definitions/stub_responses_steps.rb
|
175
|
+
- features/stub_responses.feature
|
176
|
+
- features/support/env.rb
|
177
|
+
- spec/rack_doubles/client_spec.rb
|
178
|
+
- spec/rack_doubles/middleware_spec.rb
|
179
|
+
- spec/spec_helper.rb
|