summaryrefslogtreecommitdiff
path: root/spec/ruby/library/net/http/http/start_spec.rb
blob: a2768eed18080956cade30c66115940e0f16f668 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require_relative '../../../../spec_helper'
require 'net/http'
require_relative 'fixtures/http_server'

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

  after :each do
    NetHTTPSpecs.stop_server
  end

  describe "when not passed a block" do
    before :each do
      @http = Net::HTTP.start("localhost", @port)
    end

    after :each do
      @http.finish if @http.started?
    end

    it "returns a new Net::HTTP object for the passed address and port" do
      @http.should be_kind_of(Net::HTTP)
      @http.address.should == "localhost"
      @http.port.should == @port
    end

    it "opens the tcp connection" do
      @http.started?.should be_true
    end
  end

  describe "when passed a block" do
    it "returns the blocks return value" do
      Net::HTTP.start("localhost", @port) { :test }.should == :test
    end

    it "yields the new Net::HTTP object to the block" do
      yielded = false
      Net::HTTP.start("localhost", @port) do |net|
        yielded = true
        net.should be_kind_of(Net::HTTP)
      end
      yielded.should be_true
    end

    it "opens the tcp connection before yielding" do
      Net::HTTP.start("localhost", @port) { |http| http.started?.should be_true }
    end

    it "closes the tcp connection after yielding" do
      net = nil
      Net::HTTP.start("localhost", @port) { |x| net = x }
      net.started?.should be_false
    end
  end
end

describe "Net::HTTP#start" do
  before :each do
    NetHTTPSpecs.start_server
    @http = Net::HTTP.new("localhost", NetHTTPSpecs.port)
  end

  after :each do
    @http.finish if @http.started?
    NetHTTPSpecs.stop_server
  end

  it "returns self" do
    @http.start.should equal(@http)
  end

  it "opens the tcp connection" do
    @http.start
    @http.started?.should be_true
  end

  describe "when self has already been started" do
    it "raises an IOError" do
      @http.start
      -> { @http.start }.should raise_error(IOError)
    end
  end

  describe "when passed a block" do
    it "returns the blocks return value" do
      @http.start { :test }.should == :test
    end

    it "yields the new Net::HTTP object to the block" do
      yielded = false
      @http.start do |http|
        yielded = true
        http.should equal(@http)
      end
      yielded.should be_true
    end

    it "opens the tcp connection before yielding" do
      @http.start { |http| http.started?.should be_true }
    end

    it "closes the tcp connection after yielding" do
      @http.start { }
      @http.started?.should be_false
    end
  end
end