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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
require 'test_helper'
class RequestTest < Test::Unit::TestCase
context "making an HTTP request" do
setup { setup_test_logger
FakeWeb.clean_registry
Samuel.reset_config }
teardown { teardown_test_logger }
context "to GET http://example.com/test, responding with a 200 in 53ms" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
now = Time.now
Samuel::Diary.stubs(:current_time).returns(now, now + 0.053)
open "http://example.com/test"
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://example.com/test"
end
context "on a non-standard port" do
setup do
FakeWeb.register_uri(:get, "http://example.com:8080/test", :status => [200, "OK"])
open "http://example.com:8080/test"
end
should_log_including "GET http://example.com:8080/test"
end
context "with SSL" do
setup do
FakeWeb.register_uri(:get, "https://example.com/test", :status => [200, "OK"])
open "https://example.com/test"
end
should_log_including "HTTP request"
should_log_including "GET https://example.com/test"
end
context "with SSL on a non-standard port" do
setup do
FakeWeb.register_uri(:get, "https://example.com:80/test", :status => [200, "OK"])
open "https://example.com:80/test"
end
should_log_including "HTTP request"
should_log_including "GET https://example.com:80/test"
end
context "that raises" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :exception => Errno::ECONNREFUSED)
begin
Net::HTTP.start("example.com") { |http| http.get("/test") }
rescue Exception => @exception
end
end
should_log_at_level :warn
should_log_including "HTTP request"
should_log_including "GET http://example.com/test"
should_log_including "Errno::ECONNREFUSED"
should_log_including %r|\d+ms|
should_raise_exception Errno::ECONNREFUSED
end
context "that raises a SocketError when connecting" do
setup do
FakeWeb.allow_net_connect = true
begin
http = Net::HTTP.new("example.com")
# This is an implementation-dependent hack; it would be more correct
# to stub out TCPSocket.open, but I can't get Mocha to make it raise
# correctly. Maybe related to TCPSocket being native code?
http.stubs(:connect_without_samuel).raises(SocketError)
http.start { |h| h.get("/test") }
rescue Exception => @exception
end
end
teardown do
FakeWeb.allow_net_connect = false
end
should_log_at_level :warn
should_log_including "HTTP request"
should_log_including "CONNECT http://example.com"
should_log_including "SocketError"
should_log_including %r|\d+ms|
should_raise_exception SocketError
end
context "that responds with a 500-level code" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [502, "Bad Gateway"])
Net::HTTP.start("example.com") { |http| http.get("/test") }
end
should_log_at_level :warn
end
context "that responds with a 400-level code" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [404, "Not Found"])
Net::HTTP.start("example.com") { |http| http.get("/test") }
end
should_log_at_level :warn
end
context "inside a configuration block with :label => 'Example'" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.with_config :label => "Example" do
open "http://example.com/test"
end
end
should_log_including "Example request"
should_have_config_afterwards_including :labels => {}, :label => nil
end
context "inside a configuration block with :filter_params" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test?password=secret&username=chrisk",
:status => [200, "OK"])
@uri = "http://example.com/test?password=secret&username=chrisk"
end
context "=> :password" do
setup { Samuel.with_config(:filtered_params => :password) { open @uri } }
should_log_including "http://example.com/test?password=[FILTERED]&username=chrisk"
end
context "=> :as" do
setup { Samuel.with_config(:filtered_params => :ass) { open @uri } }
should_log_including "http://example.com/test?password=[FILTERED]&username=chrisk"
end
context "=> ['pass', 'name']" do
setup { Samuel.with_config(:filtered_params => %w(pass name)) { open @uri } }
should_log_including "http://example.com/test?password=[FILTERED]&username=[FILTERED]"
end
end
context "with a global config including :label => 'Example'" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.config[:label] = "Example"
open "http://example.com/test"
end
should_log_including "Example request"
should_have_config_afterwards_including :labels => {}, :label => "Example"
end
context "with a global config including :label => 'Example' but inside config block that changes it to 'Example 2'" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.config[:label] = "Example"
Samuel.with_config(:label => "Example 2") { open "http://example.com/test" }
end
should_log_including "Example 2 request"
should_have_config_afterwards_including :labels => {}, :label => "Example"
end
context "inside a config block of :label => 'Example 2' nested inside a config block of :label => 'Example'" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.with_config :label => "Example" do
Samuel.with_config :label => "Example 2" do
open "http://example.com/test"
end
end
end
should_log_including "Example 2 request"
should_have_config_afterwards_including :labels => {}, :label => nil
end
context "wth a global config including :labels => {'example.com' => 'Example'} but inside a config block of :label => 'Example 3' nested inside a config block of :label => 'Example 2'" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.config[:labels] = {'example.com' => 'Example'}
Samuel.with_config :label => "Example 2" do
Samuel.with_config :label => "Example 3" do
open "http://example.com/test"
end
end
end
should_log_including "Example 3 request"
should_have_config_afterwards_including :labels => {'example.com' => 'Example'},
:label => nil
end
context "with a global config including :labels => {'example.com' => 'Example API'}" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.config[:labels] = {'example.com' => 'Example API'}
open "http://example.com/test"
end
should_log_including "Example API request"
end
context "with a global config including :labels => {'example.org' => 'Example API'} but making a request to example.com" do
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
Samuel.config[:labels] = {'example.org' => 'Example API'}
open "http://example.com/test"
end
should_log_including "HTTP request"
end
end
end
|