File: http_spec.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (3)
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
require "spec/helper/all"

URL = "http://localhost:8081/"
CONNECTION_ERROR_URL = "http://random-domain-blah.com/"
DELAY = 0.25

describe EventMachine::HttpRequest do
  it "should perform a synchronous fetch" do
    EM.synchrony do
      s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY)

      r = EventMachine::HttpRequest.new(URL).get
      r.response.should == 'Foo'

      s.stop
      EventMachine.stop
    end
  end

  it "should fire sequential requests" do
    EventMachine.synchrony do
      s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY)

      start = now
      order = []
      order.push :get  if EventMachine::HttpRequest.new(URL).get
      order.push :post if EventMachine::HttpRequest.new(URL).post
      order.push :head if EventMachine::HttpRequest.new(URL).head
      order.push :post if EventMachine::HttpRequest.new(URL).delete
      order.push :put  if EventMachine::HttpRequest.new(URL).put
      order.push :options if EventMachine::HttpRequest.new(URL).options
      order.push :patch  if EventMachine::HttpRequest.new(URL).patch

      (now - start.to_f).should be_within(DELAY * order.size * 0.15).of(DELAY * order.size)
      order.should == [:get, :post, :head, :post, :put, :options, :patch]

      s.stop
      EventMachine.stop
    end
  end

  it "should fire simultaneous requests via Multi interface" do
    EventMachine.synchrony do
      s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY)

      start = now

      multi = EventMachine::Synchrony::Multi.new
      multi.add :a, EventMachine::HttpRequest.new(URL).aget
      multi.add :b, EventMachine::HttpRequest.new(URL).apost
      multi.add :c, EventMachine::HttpRequest.new(URL).ahead
      multi.add :d, EventMachine::HttpRequest.new(URL).adelete
      multi.add :e, EventMachine::HttpRequest.new(URL).aput
      multi.add :f, EventMachine::HttpRequest.new(URL).aoptions
      multi.add :g, EventMachine::HttpRequest.new(URL).apatch
      res = multi.perform

      (now - start.to_f).should be_within(DELAY * 0.15).of(DELAY)
      res.responses[:callback].size.should == 7
      res.responses[:errback].size.should == 0

      s.stop
      EventMachine.stop
    end
  end

  it "should terminate immediately in case of connection errors" do
    EventMachine.synchrony do
      response = EventMachine::HttpRequest.new(CONNECTION_ERROR_URL, :connection_timeout => 0.1).get
      response.error.should_not be_nil

      EventMachine.stop
    end
  end

  it "should process inactivity timeout correctly" do
    EventMachine.synchrony do
      s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", 5)

      start = now
      r = EventMachine::HttpRequest.new(URL, :inactivity_timeout => 0.1).get
      (now - start.to_f).should be_within(0.2).of(0.1)

      s.stop
      EventMachine.stop
    end
  end
end