require File.join(File.dirname(__FILE__), 'test_helper')
require 'hoptoad'
class HoptoadConnectionFunctionalTest < Test::Unit::TestCase
context "Sending data over the connection" do
setup do
@logger = stub_everything('Logger')
Hoptoad.configure do |config|
config.logger = @logger
end
@connection = Hoptoad::Connection.from_config(Hoptoad.config)
end
should "POST the data as YAML to Hoptoad's server" do
Net::HTTP.any_instance.expects(:post).with("/notices/", {"field1" => "data", "field2" => "data"}.to_yaml, anything)
@connection.send(:field1 => "data", :field2 => "data")
end
should "log success if POST is successful" do
@logger.expects(:info).with(regexp_matches(/hoptoad success/i))
Net::HTTP.any_instance.stubs(:post).returns(http_response(Net::HTTPSuccess))
@connection.send(:field1 => "data", :field2 => "data")
end
should "log failure if POST is not successful" do
@logger.expects(:error).with(regexp_matches(/hoptoad failure/i))
Net::HTTP.any_instance.stubs(:post).returns(http_response(Net::HTTPInternalServerError))
@connection.send(:field1 => "data", :field2 => "data")
end
end
def http_response(klass)
returning klass.new(nil, nil, nil) do |response|
response.stubs(:body)
end
end
end
class HoptoadConnectionTest < Test::Unit::TestCase
context "A Hoptoad connection" do
setup do
@url = URI.parse("http://example.com:3000/notices/")
@connection = Hoptoad::Connection.new(@url, stub_everything('Logger'))
end
should "open a HTTP connection to the specified URL's host" do
Net::HTTP.expects(:start).with("example.com", 3000).yields(stub_everything('Net::HTTP'))
@connection.send({})
end
should "POST data to the specified URL path" do
http_connection = stub_everything('Net::HTTP')
Net::HTTP.stubs(:start).yields(http_connection)
http_connection.expects(:post).with('/notices/', anything, anything)
@connection.send({})
end
should "specify its HTTP content type as YAML" do
http_connection = stub_everything('Net::HTTP')
Net::HTTP.stubs(:start).yields(http_connection)
http_connection.expects(:post).with(anything, anything, has_entry("Content-type", "application/x-yaml"))
@connection.send({})
end
should "ask for XML in its response" do
http_connection = stub_everything('Net::HTTP')
Net::HTTP.stubs(:start).yields(http_connection)
http_connection.expects(:post).with(anything, anything, has_entry("Accept", "text/xml, application/xml"))
@connection.send({})
end
should "convert data keys to strings before sending" do
http_connection = stub_everything('Net::HTTP')
Net::HTTP.stubs(:start).yields(http_connection)
http_connection.expects(:post).with('/notices/', {"foo" => "bar"}.to_yaml, anything)
@connection.send({:foo => "bar"})
end
end
end