Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 366 lines (310 sloc) 15.514 kb
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
class MyException < StandardError; end;

shared_context "declared responses" do |*adapter_info|
  describe "when request stub declares that request should raise exception" do
    it "should raise exception" do
      stub_request(:get, "www.example.com").to_raise(MyException)
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error(MyException, "Exception from WebMock")
    end

    it "should raise exception if declared as and exception instance" do
      stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error(MyException, "hello world")
    end

    it "should raise exception if declared as an exception instance" do
      stub_request(:get, "www.example.com").to_raise("hello world")
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error("hello world")
    end

    it "should raise exception after returning declared successful response first" do
      stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)
      http_request(:get, "http://www.example.com/").body.should == "abc"
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error(MyException, "Exception from WebMock")
    end
  end

  describe "when request stub declares that request should timeout" do
    it "should timeout" do
      stub_request(:get, "www.example.com").to_timeout
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error(client_timeout_exception_class)
    end

    it "should timeout after returning declared successful response" do
      stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_timeout
      http_request(:get, "http://www.example.com/").body.should == "abc"
      lambda {
        http_request(:get, "http://www.example.com/")
      }.should raise_error(client_timeout_exception_class)
    end
  end

  describe "when request stub declares that request should return a response" do
    it "should return response with declared body" do
      stub_request(:get, "www.example.com").to_return(:body => "abc")
      http_request(:get, "http://www.example.com/").body.should == "abc"
    end

    it "should return response with declared headers" do
      stub_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
      response = http_request(:get, "http://www.example.com/")
      response.headers["Content-Length"].should == "8888"
    end

    it "should return response with declared headers even if there are multiple headers with the same key" do
      stub_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
      response = http_request(:get, "http://www.example.com/")
      response.headers["A"].should == "b, c"
    end

    it "should return response with declared status code" do
      stub_request(:get, "www.example.com").to_return(:status => 500)
      http_request(:get, "http://www.example.com/").status.should == "500"
    end

    it "should return response with declared status message", :unless => (adapter_info.include?(:no_status_message)) do
      stub_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
      response = http_request(:get, "http://www.example.com/")
      response.message.should == "Internal Server Error"
    end

    it "should return response with a default status code" do
      stub_request(:get, "www.example.com")
      http_request(:get, "http://www.example.com/").status.should == "200"
    end

    it "should return default response with empty message if response was not declared", :unless => (adapter_info.include?(:no_status_message)) do
      stub_request(:get, "www.example.com")
      response = http_request(:get, "http://www.example.com/")
      response.message.should == ""
    end

    describe "when response body was declared as IO" do
      it "should return response body" do
        stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
        http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
      end

      it "should return response body if requested many times" do
        stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
        2.times do
          http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
        end
      end

      it "should close IO after request" do
        stub_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
        @file.should be_closed
      end
    end

    describe "when response parts were declared as lambdas" do
      it "should return evaluated response body" do
        stub_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
        http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
      end

      it "should return evaluated response headers" do
        stub_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
        http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
      end
    end

    describe "when response was declared as lambda" do
      class Responder
        def call(request)
          {:body => request.body}
        end
      end

      it "should return evaluated response body" do
        stub_request(:post, "www.example.com").to_return(lambda {|request|
                                                                {:body => request.body}
        })
        http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
      end

      it "should return evaluated response headers" do
        stub_request(:get, "www.example.com").to_return(lambda { |request|
                                                               {:headers => request.headers}
        })
        http_request(:get, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
      end

      it "should return dynamic response declared as a block" do
        stub_request(:post, "www.example.com").to_return do |request|
          {:body => request.body}
        end
        http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
      end

      it "should return dynamic response declared as an object responding to call" do
        stub_request(:post, "www.example.com").to_return(Responder.new)
        http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
      end
    end


    describe "when response was declared as a file with a raw response" do
      before(:each) do
        @file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
        stub_request(:get, "www.example.com").to_return(@file)
        @response = http_request(:get, "http://www.example.com/")
      end

      it "should return recorded headers" do
        @response.headers.should == {
          "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
          "Content-Type"=>"text/html; charset=UTF-8",
          "Content-Length"=>"419",
          "Connection"=>"Keep-Alive",
          "Accept"=>"image/jpeg, image/png"
        }
      end

      it "should return recorded body" do
        @response.body.size.should == 419
      end

      it "should return recorded status" do
        @response.status.should == "202"
      end

      it "should return recorded status message", :unless => (adapter_info.include?(:no_status_message)) do
        @response.message.should == "OK"
      end

      it "should ensure file is closed" do
        @file.should be_closed
      end
    end

    describe "when response was declared as a string with a raw response" do
      before(:each) do
        @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
        stub_request(:get, "www.example.com").to_return(@input)
        @response = http_request(:get, "http://www.example.com/")
      end

      it "should return recorded headers" do
        @response.headers.should == {
          "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
          "Content-Type"=>"text/html; charset=UTF-8",
          "Content-Length"=>"419",
          "Connection"=>"Keep-Alive",
          "Accept"=>"image/jpeg, image/png"
        }
      end

      it "should return recorded body" do
        @response.body.size.should == 419
      end

      it "should return recorded status" do
        @response.status.should == "202"
      end

      it "should return recorded status message", :unless => (adapter_info.include?(:no_status_message)) do
        @response.message.should == "OK"
      end
    end

    describe "when response was declared as lambda evaluating to a string with a raw response" do
      before(:each) do
        @files = {
          "www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
        }
      end

      it "should return response from evaluated file" do
        stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
        http_request(:get, "http://www.example.com/").body.size.should == 419
      end

      it "should return response from evaluated string" do
        stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
        http_request(:get, "http://www.example.com/").body.size.should == 419
      end
    end

    describe "when response is declared as an Rack app" do
      before(:each) do
        stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
      end

      it "should return response returned by the rack app" do
        http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!'
      end
    end

    describe "when sequences of responses are declared" do
      it "should return responses one by one if declared in array" do
        stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "3"
      end

      it "should repeat returning last declared response from a sequence after all responses were returned" do
        stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"} ])
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "2"
      end

      it "should return responses one by one if declared as comma separated params" do
        stub_request(:get, "www.example.com").to_return({:body => "1"}, {:body => "2"}, {:body => "3"})
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "3"
      end

      it "should return responses one by one if declared with several to_return invokations" do
        stub_request(:get, "www.example.com").
          to_return({:body => "1"}).
          to_return({:body => "2"}).
          to_return({:body => "3"})
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "3"
      end

      it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
        stub_request(:get, "www.example.com").to_return({:body => "1"}).then.
          to_return({:body => "2"}).then.to_return({:body => "3"})
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "3"
      end

    end

    describe "when responses are declared to return more than once" do
      it "should repeat one response declared number of times" do
        stub_request(:get, "www.example.com").
          to_return({:body => "1"}).times(2).
          to_return({:body => "2"})
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
      end


      it "should repeat sequence of response declared number of times" do
        stub_request(:get, "www.example.com").
          to_return({:body => "1"}, {:body => "2"}).times(2).
          to_return({:body => "3"})
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "2"
        http_request(:get, "http://www.example.com/").body.should == "3"
      end


      it "should repeat infinitely last response even if number of declared times is lower" do
        stub_request(:get, "www.example.com").
          to_return({:body => "1"}).times(2)
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "1"
        http_request(:get, "http://www.example.com/").body.should == "1"
      end

      it "should give error if times is declared without specifying response" do
        lambda {
          stub_request(:get, "www.example.com").times(3)
        }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
      end

    end

    describe "when exception is declared to be raised more than once" do
      it "should repeat raising exception declared number of times" do
        stub_request(:get, "www.example.com").
          to_raise(MyException).times(2).
          to_return({:body => "2"})
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(MyException, "Exception from WebMock")
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(MyException, "Exception from WebMock")
        http_request(:get, "http://www.example.com/").body.should == "2"
      end

      it "should repeat raising sequence of exceptions declared number of times" do
        stub_request(:get, "www.example.com").
          to_raise(MyException, ArgumentError).times(2).
          to_return({:body => "2"})
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(MyException, "Exception from WebMock")
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(ArgumentError)
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(MyException, "Exception from WebMock")
        lambda {
          http_request(:get, "http://www.example.com/")
        }.should raise_error(ArgumentError)
        http_request(:get, "http://www.example.com/").body.should == "2"
      end
    end
  end
end
Something went wrong with that request. Please try again.