Skip to content

Commit

Permalink
Add request_head option to http scout. Tests are somewhat sloppy, but…
Browse files Browse the repository at this point in the history
… it works.
  • Loading branch information
Alex Rozumey committed Jan 17, 2012
1 parent c942764 commit f55fe0b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/outpost/scouts/http.rb
Expand Up @@ -34,13 +34,18 @@ def setup(options)
@port = options[:port] || 80
@path = options[:path] || '/'
@http_class = options[:http_class] || Net::HTTP
@request_head = options.has_key?(:request_head) ? options[:request_head] : false
end

# Runs the scout, connecting to the host and getting the response code,
# body and time.
def execute
previous_time = Time.now
response = @http_class.get_response(@host, @path, @port)
if @request_head
response = @http_class.request_head(@host, @path, @port)
else
response = @http_class.get_response(@host, @path, @port)
end

@response_time = (Time.now - previous_time) * 1000 # Miliseconds
@response_code = response.code.to_i
Expand Down
27 changes: 27 additions & 0 deletions test/outpost/scouts/http_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
require 'ostruct'

describe Outpost::Scouts::Http do
class NetHttpStub
Expand All @@ -8,6 +9,22 @@ def response(&block); @response = block; end
def get_response(*args)
@response.call
end

def request_head(*args)
throw "Unexpected call"
end
end
end

class NetHttpHeadStub < NetHttpStub
class << self
def request_head(*args)
OpenStruct.new :code => 200, :body => 'Body'
end

def get_response(*args)
throw "Unexpected call"
end
end
end

Expand Down Expand Up @@ -47,6 +64,16 @@ def get_response(*args)
refute @subject.response_body
end

it "should make HEAD request if option is passed" do
config_stub = config_stub(:host => 'localhost', :http_class => NetHttpHeadStub, :request_head => true)
@subject = Outpost::Scouts::Http.new("description", config_stub)

@subject.run

assert_equal 200 , @subject.response_code
assert_equal 'Body', @subject.response_body
end

private

def config_stub(options={})
Expand Down

0 comments on commit f55fe0b

Please sign in to comment.