summaryrefslogtreecommitdiff
path: root/spec/ruby/library/net-http/http/get_spec.rb
blob: e64a61c52cfceaf131ee4d1e6ee03e22227d9258 (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
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
require_relative '../../../spec_helper'
require 'net/http'
require_relative 'fixtures/http_server'

describe "Net::HTTP.get" do
  before :each do
    NetHTTPSpecs.start_server
    @port = NetHTTPSpecs.port
  end

  after :each do
    NetHTTPSpecs.stop_server
  end

  describe "when passed URI" do
    it "returns the body of the specified uri" do
      Net::HTTP.get(URI.parse("http://localhost:#{@port}/")).should == "This is the index page."
    end
  end

  describe "when passed host, path, port" do
    it "returns the body of the specified host-path-combination" do
      Net::HTTP.get('localhost', "/", @port).should == "This is the index page."
    end
  end
end

quarantine! do # These specs fail frequently with CHECK_LEAKS=true
describe "Net::HTTP.get" do
  describe "when reading gzipped contents" do
    def start_threads
      require 'zlib'
      require 'stringio'

      server = nil
      server_thread = Thread.new do
        server = TCPServer.new("127.0.0.1", 0)
        begin
          c = server.accept
        ensure
          server.close
        end
        c.print "HTTP/1.1 200\r\n"
        c.print "Content-Type: text/plain\r\n"
        c.print "Content-Encoding: gzip\r\n"
        s = StringIO.new
        z = Zlib::GzipWriter.new(s)
        begin
          z.write 'Hello World!'
        ensure
          z.close
        end
        c.print "Content-Length: #{s.length}\r\n\r\n"
        # Write partial gzip content
        c.write s.string.byteslice(0..-2)
        c.flush
        c
      end
      Thread.pass until server && server_thread.stop?

      client_thread = Thread.new do
        Thread.current.report_on_exception = false
        Net::HTTP.get("127.0.0.1", '/', server.connect_address.ip_port)
      end

      socket = server_thread.value
      Thread.pass until client_thread.stop?

      [socket, client_thread]
    end

    it "propagates exceptions interrupting the thread and does not replace it with Zlib::BufError" do
      my_exception = Class.new(RuntimeError)
      socket, client_thread = start_threads
      begin
        client_thread.raise my_exception, "my exception"
        -> { client_thread.value }.should raise_error(my_exception)
      ensure
        socket.close
      end
    end

    it "lets the kill Thread exception goes through and does not replace it with Zlib::BufError" do
      socket, client_thread = start_threads
      begin
        client_thread.kill
        client_thread.value.should == nil
      ensure
        socket.close
      end
    end
  end
end
end