File: test_fake_web_open_uri.rb

package info (click to toggle)
ruby-fakeweb 1.3.0%2Bgit20170806%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 452 kB
  • sloc: ruby: 2,057; sh: 24; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 2,265 bytes parent folder | download | duplicates (2)
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
require 'test_helper'

class TestFakeWebOpenURI < Test::Unit::TestCase

  def test_content_for_registered_uri
    FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
    assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
  end

  def test_mock_open
    FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
    assert_equal 'test example content', URI.open('http://mock/test_example.txt').read
  end

  def test_mock_open_with_string_as_registered_uri
    FakeWeb.register_uri(:get, 'http://mock/test_string.txt', :body => 'foo')
    assert_equal 'foo', URI.open('http://mock/test_string.txt').string
  end

  def _test_real_open
    FakeWeb.allow_net_connect = true
    setup_expectations_for_real_apple_hot_news_request
    resp = URI.open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
    assert_equal "200", resp.status.first
    body = resp.read
    assert body.include?('Apple')
    assert body.include?('News')
  end

  def test_mock_open_that_raises_exception
    FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => StandardError)
    assert_raises(StandardError) do
      URI.open('http://mock/raising_exception.txt')
    end
  end

  def test_mock_open_that_raises_an_http_error
    FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
    assert_raises(OpenURI::HTTPError) do
      URI.open('http://mock/raising_exception.txt')
    end
  end

  def test_mock_open_that_raises_an_http_error_with_a_specific_status
    FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
    exception = assert_raises(OpenURI::HTTPError) do
      URI.open('http://mock/raising_exception.txt')
    end
    assert_equal '123', exception.io.code
    assert_equal 'jodel', exception.io.message
  end

  def test_mock_open_with_block
    FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
    body = URI.open('http://mock/test_example.txt') { |f| f.readlines }
    assert_equal 'test example content', body.first
  end
end