Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 123 lines (103 sloc) 3.802 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
require File.dirname(__FILE__) + '/helper'

class SenderTest < Test::Unit::TestCase

  def setup
    reset_config
  end

  def build_sender(opts = {})
    config = HoptoadNotifier::Configuration.new
    opts.each {|opt, value| config.send(:"#{opt}=", value) }
    HoptoadNotifier::Sender.new(config)
  end

  def send_exception(args = {})
    notice = args.delete(:notice) || build_notice_data
    sender = args.delete(:sender) || build_sender(args)
    sender.send_to_hoptoad(notice)
    sender
  end

  def stub_http
    response = stub(:body => 'body')
    http = stub(:post => response,
                :read_timeout= => nil,
                :open_timeout= => nil,
                :use_ssl= => nil)
    Net::HTTP.stubs(:new => http)
    http
  end

  should "post to Hoptoad when using an HTTP proxy" do
    response = stub(:body => 'body')
    http = stub(:post => response,
                    :read_timeout= => nil,
                    :open_timeout= => nil,
                    :use_ssl= => nil)
    proxy = stub(:new => http)
    Net::HTTP.stubs(:Proxy => proxy)

    url = "http://hoptoadapp.com:80#{HoptoadNotifier::Sender::NOTICES_URI}"
    uri = URI.parse(url)

    proxy_host = 'some.host'
    proxy_port = 88
    proxy_user = 'login'
    proxy_pass = 'passwd'

    send_exception(:proxy_host => proxy_host,
                   :proxy_port => proxy_port,
                   :proxy_user => proxy_user,
                   :proxy_pass => proxy_pass)
    assert_received(http, :post) do |expect|
      expect.with(uri.path, anything, HoptoadNotifier::HEADERS)
    end
    assert_received(Net::HTTP, :Proxy) do |expect|
      expect.with(proxy_host, proxy_port, proxy_user, proxy_pass)
    end
  end

  should "post to the right url for non-ssl" do
    http = stub_http
    url = "http://hoptoadapp.com:80#{HoptoadNotifier::Sender::NOTICES_URI}"
    uri = URI.parse(url)
    send_exception(:secure => false)
    assert_received(http, :post) {|expect| expect.with(uri.path, anything, HoptoadNotifier::HEADERS) }
  end

  should "post to the right path for ssl" do
    http = stub_http
    send_exception(:secure => true)
    assert_received(http, :post) {|expect| expect.with(HoptoadNotifier::Sender::NOTICES_URI, anything, HoptoadNotifier::HEADERS) }
  end

  should "default the open timeout to 2 seconds" do
    http = stub_http
    send_exception
    assert_received(http, :open_timeout=) {|expect| expect.with(2) }
  end

  should "default the read timeout to 5 seconds" do
    http = stub_http
    send_exception
    assert_received(http, :read_timeout=) {|expect| expect.with(5) }
  end

  should "allow override of the open timeout" do
    http = stub_http
    send_exception(:http_open_timeout => 4)
    assert_received(http, :open_timeout=) {|expect| expect.with(4) }
  end

  should "allow override of the read timeout" do
    http = stub_http
    send_exception(:http_read_timeout => 10)
    assert_received(http, :read_timeout=) {|expect| expect.with(10) }
  end

  should "connect to the right port for ssl" do
    stub_http
    send_exception(:secure => true)
    assert_received(Net::HTTP, :new) {|expect| expect.with("hoptoadapp.com", 443) }
  end

  should "connect to the right port for non-ssl" do
    stub_http
    send_exception(:secure => false)
    assert_received(Net::HTTP, :new) {|expect| expect.with("hoptoadapp.com", 80) }
  end

  should "use ssl if secure" do
    stub_http
    send_exception(:secure => true, :host => 'example.org')
    assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 443) }
  end

  should "not use ssl if not secure" do
    stub_http
    send_exception(:secure => false, :host => 'example.org')
    assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 80) }
  end

end
Something went wrong with that request. Please try again.