File: test_stub_socket_compatibility.rb

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

class TestStubSocketCompatibility < Test::Unit::TestCase
  def test_sending_timeout_accessors_after_starting_session
    # this tests an HTTPS request because #ssl_timeout= raises otherwise
    FakeWeb.register_uri(:get, "https://example.com", :status => [200, "OK"])
    http = Net::HTTP.new("example.com", 443)
    http.use_ssl = true
    http.get("/")
    timeouts = []
    http.methods.grep(/_timeout=/).each do |setter|
      http.send(setter, 5)
      getter = setter.to_s.sub(/=$/, "")
      timeouts << http.send(getter)
    end
    assert_equal [5], timeouts.uniq
  end

  def test_stub_socket_always_responds_to_read_timeout
    FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
    http = Net::HTTP.new("example.com", 80)
    http.get("/")
    assert_respond_to http.instance_variable_get(:@socket), :read_timeout=
  end

  def test_stub_socket_only_responds_to_continue_timeout_under_193_or_later
    FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
    http = Net::HTTP.new("example.com", 80)
    http.get("/")
    socket = http.instance_variable_get(:@socket)
    assert_equal RUBY_VERSION >= "1.9.3", socket.respond_to?(:continue_timeout=)
  end

  def test_stub_socket_responds_to_close_and_always_returns_true
    FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
    http = Net::HTTP.new("example.com", 80)
    http.get("/")
    socket = http.instance_variable_get(:@socket)
    assert_equal socket.close, true
  end

  def test_stub_socket_tracks_closed_state
    FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
    http = Net::HTTP.new("example.com", 80)
    http.get("/")
    socket = http.instance_variable_get(:@socket)
    assert !socket.closed?
    socket.close
    assert socket.closed?
  end
end