summaryrefslogtreecommitdiff
path: root/test/unit/adapters/http_test.rb
blob: 5bf34688c7094481f868c30e9a6ccfe715d481ba (plain)
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
require 'test_helper'
require 'http'
require 'nickserver/adapters/http'

class HttpAdapterTest < Minitest::Test
  def test_normal_raise
    stub_http_to_raise do
      assert_raises HTTP::ConnectionError do
        adapter.get ''
      end
    end
  end

  def test_rescueing
    stub_http_to_raise do
      assert_nil adapter.get('', rescue: 'some reason')
    end
  end

  def test_raise_not_rescued
    stub_http_to_raise do
      assert_raises Http::ConnectionError do
        assert adapter.get('', rescue: 'some other reason')
      end
    end
  end

  protected

  def stub_http_to_raise(&block)
    raises_exception = lambda { |*_args|
      raise HTTP::ConnectionError, 'for some reason'
    }
    HTTP.stub :get, raises_exception, &block
  end

  def adapter
    Nickserver::Adapters::Http.new
  end
end