public
Fork of thoughtbot/hoptoad_notifier
Description: Reports exceptions to Hoptoad
Homepage: http://www.hoptoadapp.com/
Clone URL: git://github.com/lukeredpath/hoptoad_notifier.git
hoptoad_notifier / test / hoptoad_connection_test.rb
100644 80 lines (68 sloc) 3.013 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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