Permalink
Browse files

Added a feature where stubs have unique identifiers (returned by the …

…API), which can then be

used with a new API call to see if those requests were made.
  • Loading branch information...
1 parent 0932cb0 commit 9a3734d2c0a4dc8be49f911a1a3ee477da282d02 @lukeredpath committed Mar 9, 2012
Showing with 109 additions and 10 deletions.
  1. +6 −1 Gemfile.lock
  2. +39 −0 features/checking_requests_were_made.feature
  3. +30 −8 lib/mimic/api.rb
  4. +17 −1 lib/mimic/fake_host.rb
  5. +17 −0 spec/fake_host_spec.rb
View
@@ -25,6 +25,9 @@ GEM
mocha (0.9.12)
plist (3.1.0)
rack (1.3.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
rest-client (1.6.3)
mime-types (>= 1.16)
rspec (2.4.0)
@@ -37,7 +40,7 @@ GEM
rspec-mocks (2.4.0)
sinatra (1.2.6)
rack (~> 1.1)
- tilt (< 2.0, >= 1.2.2)
+ tilt (>= 1.2.2, < 2.0)
term-ansicolor (1.0.5)
tilt (1.3.2)
@@ -48,5 +51,7 @@ DEPENDENCIES
cucumber
mimic!
mocha
+ rake
+ rdoc
rest-client
rspec (~> 2.4.0)
@@ -0,0 +1,39 @@
+Feature: Checking requests were made
+ In order to verify requests were made to mimic (like mock expectations)
+ As a developer
+ I want to be able to ask the Mimic API what requests were made
+
+ Scenario: Configuring a request and verifying it was called
+ Given that Mimic is running and accepting remote configuration on "/api"
+ When I make an HTTP POST request to "http://localhost:11988/api/get" with the payload:
+ """
+ {"path": "/anything"}
+ """
+ Then I should receive an HTTP 201 response with a body containing:
+ """
+ {"stubs":["41f660b868e1308e8d87afbb84532b71"]}
+ """
+ And I make an HTTP GET request to "http://localhost:11988/anything"
+ And I make an HTTP GET request to "http://localhost:11988/api/requests"
+ Then I should receive an HTTP 200 response with a body containing:
+ """
+ {"requests":["41f660b868e1308e8d87afbb84532b71"]}
+ """
+
+ Scenario: Configuring a request and verifying it was not called
+ Given that Mimic is running and accepting remote configuration on "/api"
+ When I make an HTTP POST request to "http://localhost:11988/api/get" with the payload:
+ """
+ {"path": "/anything"}
+ """
+ Then I should receive an HTTP 201 response with a body containing:
+ """
+ {"stubs":["41f660b868e1308e8d87afbb84532b71"]}
+ """
+ And I make an HTTP GET request to "http://localhost:11988/api/requests"
+ Then I should receive an HTTP 200 response with a body containing:
+ """
+ {"requests":[]}
+ """
+
+
View
@@ -15,14 +15,14 @@ def host
post "/#{verb}" do
api_request = APIRequest.from_request(request, verb)
api_request.setup_stubs_on(host)
- [201, {}, api_request.to_s]
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
end
end
post "/multi" do
api_request = APIRequest.from_request(request)
api_request.setup_stubs_on(host)
- [201, {}, api_request.to_s]
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
end
post "/clear" do
@@ -39,18 +39,39 @@ def host
[200, {}, self.host.inspect]
end
+ get "/requests" do
+ [200, {"Content-Type" => "application/json"}, {"requests" => host.received_requests.map(&:to_hash)}.to_json]
+ end
+
private
class APIRequest
- def initialize(data, method = nil)
+ attr_reader :request_content_type
+
+ def initialize(data, method = nil, request_content_type)
@data = data
@method = (method || "GET")
+ @stubs = []
+ @request_content_type = request_content_type
end
def to_s
@data.inspect
end
+ def response
+ response = {"stubs" => @stubs.map(&:to_hash)}
+
+ case request_content_type
+ when /json/
+ response.to_json
+ when /plist/
+ response.to_plist
+ else
+ response.to_json
+ end
+ end
+
def self.from_request(request, method = nil)
case request.content_type
when /json/
@@ -60,12 +81,12 @@ def self.from_request(request, method = nil)
else
data = JSON.parse(request.body.string)
end
- new(data, method)
+ new(data, method, request.content_type)
end
def setup_stubs_on(host)
(@data["stubs"] || [@data]).each do |stub_data|
- Stub.new(stub_data, stub_data['method'] || @method).on(host)
+ @stubs << Stub.new(stub_data, stub_data['method'] || @method).on(host)
end
end
@@ -76,9 +97,10 @@ def initialize(data, method = nil)
end
def on(host)
- stub = host.send(@method.downcase.to_sym, path).returning(body, code, headers)
- stub.with_query_parameters(params)
- stub.echo_request!(echo_format)
+ host.send(@method.downcase.to_sym, path).returning(body, code, headers).tap do |stub|
+ stub.with_query_parameters(params)
+ stub.echo_request!(echo_format)
+ end
end
def echo_format
@@ -15,6 +15,10 @@ def initialize(options = {})
build_url_map!
end
+ def received_requests
+ @stubs.select { |s| s.received }
+ end
+
def get(path, &block)
request("GET", path, &block)
end
@@ -140,13 +144,21 @@ def env_without_rack_env
end
class StubbedRequest
+ attr_accessor :received
+
def initialize(app, method, path)
@method, @path = method, path
@code = 200
@headers = {}
@params = {}
@body = ""
@app = app
+ @received = false
+ end
+
+ def to_hash
@lukeredpath
lukeredpath Mar 11, 2012 owner

Should rename this to something like #token as it's confusing (its not returning a Hash).

+ token = "#{@method} #{@path}"
+ Digest::MD5.hexdigest(token)
end
def returning(body, code = 200, headers = {})
@@ -193,7 +205,11 @@ def response_for_request(request)
def build
stub = self
- @app.send(@method.downcase, @path) { stub.response_for_request(request) }
+
+ @app.send(@method.downcase, @path) do
+ stub.received = true
+ stub.response_for_request(request)
+ end
end
end
end
@@ -69,6 +69,23 @@
}.should raise_error
end
+ it "returns a StubbedRequest" do
+ @host.get("/some/path").should be_kind_of(Mimic::FakeHost::StubbedRequest)
+ end
+
+ describe "StubbedRequest" do
+ it "has a unique hash based on it's parameters" do
+ host = Mimic::FakeHost::StubbedRequest.new(stub, "GET", "/path")
+ host.to_hash.should == Digest::MD5.digest("GET /path")
+ end
+
+ it "has the same hash as an equivalent request" do
+ host_one = Mimic::FakeHost::StubbedRequest.new(stub, "GET", "/path")
+ host_two = Mimic::FakeHost::StubbedRequest.new(stub, "GET", "/path")
+ host_one.to_hash.should == host_two.to_hash
+ end
+ end
+
private
def request_for(path, options={})

0 comments on commit 9a3734d

Please sign in to comment.