Skip to content

Commit

Permalink
Allow request logging using Rack::CommonLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeredpath committed Jun 25, 2011
1 parent f2a6181 commit fc63928
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
13 changes: 13 additions & 0 deletions features/logging_requests.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Logging incoming requests
In order to check that Mimic is working properly or provide further debug information
As a developer
I want to be able to tell Mimic to log incoming requests

Scenario: Logging to STDOUT
Given I have a mimic specification with:
"""
Mimic.mimic(:port => 11988, :log => $stdout).get("/some/path")
"""
When I make an HTTP GET request to "http://localhost:11988/some/path"
Then I should see "GET /some/path HTTP/1.1" written to STDOUT

3 changes: 3 additions & 0 deletions features/steps/logging_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Then /^I should see "([^"]*)" written to STDOUT$/ do |output|
TEST_STDOUT.tap { |io| io.rewind }.read.should include(output)
end
10 changes: 9 additions & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. .. lib]))

TEST_STDOUT = StringIO.new

Before do
if test_proxy = ENV["MIMIC_TEST_PROXY"]
HttpClient.use_proxy(test_proxy)
end
end

$stdout = TEST_STDOUT
end

After do
$stdout = STDOUT
end
5 changes: 3 additions & 2 deletions lib/mimic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ module Mimic
:hostname => 'localhost',
:port => MIMIC_DEFAULT_PORT,
:remote_configuration_path => nil,
:fork => true
:fork => true,
:log => nil
}

def self.mimic(options = {}, &block)
options = MIMIC_DEFAULT_OPTIONS.merge(options)

host = FakeHost.new(options[:hostname], options[:remote_configuration_path]).tap do |host|
host = FakeHost.new(options).tap do |host|
host.instance_eval(&block) if block_given?
Server.instance.serve(host, options)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/mimic/fake_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
module Mimic
class FakeHost
attr_reader :hostname, :url_map
attr_accessor :log

def initialize(hostname, remote_configuration_path = nil)
@hostname = hostname
@remote_configuration_path = remote_configuration_path
def initialize(options = {})
@hostname = options[:hostname]
@remote_configuration_path = options[:remote_configuration_path]
@log = options[:log]
@imports = []
clear
build_url_map!
Expand Down Expand Up @@ -50,6 +52,7 @@ def call(env)
def clear
@stubs = []
@app = Sinatra.new
@app.use Rack::CommonLogger, self.log if self.log
@app.not_found do
[404, {}, ""]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fake_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "Mimic::FakeHost" do
before do
@host = Mimic::FakeHost.new("www.example.com")
@host = Mimic::FakeHost.new(:hostname => "www.example.com")
end

it "should handle stubbed requests" do
Expand Down

0 comments on commit fc63928

Please sign in to comment.