File: http_client_test.rb

package info (click to toggle)
ruby-samuel 0.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 735; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 2,812 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
89
90
91
92
93
94
95
96
97
require 'test_helper'

class HttpClientTest < Test::Unit::TestCase
  context "making an HTTPClient request" do
    setup    { setup_test_logger
               start_test_server
               Samuel.reset_config }
    teardown { teardown_test_logger }

    context "to GET http://localhost:8000/, responding with a 200 in 53ms" do
      setup do
        now = Time.now
        Samuel::Diary.stubs(:current_time).returns(now, now + 0.053)
        HTTPClient.get("http://localhost:8000/")
      end

      should_log_lines     1
      should_log_at_level  :info
      should_log_including "HTTP request"
      should_log_including "(53ms)"
      should_log_including "[200 OK]"
      should_log_including "GET http://localhost:8000/"
    end

    context "using PUT" do
      setup do
        HTTPClient.put("http://localhost:8000/books/1", "test=true")
      end

      should_log_including "PUT http://localhost:8000/books/1"
    end

    context "using an asynchronous POST" do
      setup do
        body = "title=Infinite%20Jest"
        client = HTTPClient.new
        connection = client.post_async("http://localhost:8000/books", body)
        sleep 0.1 until connection.finished?
      end

      should_log_including "POST http://localhost:8000/books"
    end

    context "that raises" do
      setup do
        begin
          HTTPClient.get("http://localhost:8001/")
        rescue Errno::ECONNREFUSED => @exception
        end
      end

      should_log_at_level    :warn
      should_log_including   "HTTP request"
      should_log_including   "GET http://localhost:8001/"
      should_log_including   "Errno::ECONNREFUSED"
      should_log_including   %r|\d+ms|
      should_raise_exception Errno::ECONNREFUSED
    end

    context "using an asynchronous GET that raises" do
      setup do
        begin
          client = HTTPClient.new
          connection = client.get_async("http://localhost:8001/")
          sleep 0.1 until connection.finished?
          connection.pop
        rescue Errno::ECONNREFUSED => @exception
        end
      end

      should_log_at_level    :warn
      should_log_including   "HTTP request"
      should_log_including   "GET http://localhost:8001/"
      should_log_including   "Errno::ECONNREFUSED"
      should_log_including   %r|\d+ms|
      should_raise_exception Errno::ECONNREFUSED
    end

    context "that responds with a 400-level code" do
      setup do
        HTTPClient.get("http://localhost:8000/test?404")
      end

      should_log_at_level :warn
      should_log_including "[404 Not Found]"
    end

    context "that responds with a 500-level code" do
      setup do
        HTTPClient.get("http://localhost:8000/test?500")
      end

      should_log_at_level :warn
      should_log_including "[500 Internal Server Error]"
    end
  end
end