webmock 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG.md +47 -2
  2. data/README.md +68 -6
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/lib/webmock.rb +2 -2
  6. data/lib/webmock/adapters/rspec.rb +1 -1
  7. data/lib/webmock/adapters/rspec/matchers.rb +2 -2
  8. data/lib/webmock/adapters/rspec/{request_profile_matcher.rb → request_pattern_matcher.rb} +5 -5
  9. data/lib/webmock/adapters/rspec/webmock_matcher.rb +2 -2
  10. data/lib/webmock/config.rb +2 -1
  11. data/lib/webmock/http_lib_adapters/httpclient.rb +5 -4
  12. data/lib/webmock/http_lib_adapters/net_http.rb +5 -3
  13. data/lib/webmock/http_lib_adapters/patron.rb +82 -0
  14. data/lib/webmock/request_execution_verifier.rb +8 -8
  15. data/lib/webmock/request_pattern.rb +130 -0
  16. data/lib/webmock/request_registry.rb +4 -9
  17. data/lib/webmock/request_signature.rb +18 -37
  18. data/lib/webmock/request_stub.rb +17 -6
  19. data/lib/webmock/response.rb +87 -31
  20. data/lib/webmock/util/headers.rb +5 -0
  21. data/lib/webmock/webmock.rb +10 -6
  22. data/spec/httpclient_spec.rb +0 -1
  23. data/spec/httpclient_spec_helper.rb +11 -1
  24. data/spec/net_http_spec.rb +8 -1
  25. data/spec/net_http_spec_helper.rb +11 -1
  26. data/spec/patron_spec.rb +83 -0
  27. data/spec/patron_spec_helper.rb +44 -0
  28. data/spec/request_execution_verifier_spec.rb +8 -8
  29. data/spec/request_pattern_spec.rb +243 -0
  30. data/spec/request_registry_spec.rb +34 -19
  31. data/spec/request_signature_spec.rb +23 -191
  32. data/spec/request_stub_spec.rb +32 -11
  33. data/spec/response_spec.rb +98 -5
  34. data/spec/spec_helper.rb +8 -16
  35. data/spec/webmock_spec.rb +154 -49
  36. data/webmock.gemspec +14 -7
  37. metadata +21 -7
  38. data/lib/webmock/request.rb +0 -29
  39. data/lib/webmock/request_profile.rb +0 -50
  40. data/spec/request_profile_spec.rb +0 -68
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'httpclient'
3
+ require 'patron' unless RUBY_PLATFORM =~ /java/
4
+
3
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
7
  require 'spec'
@@ -29,7 +31,7 @@ def setup_expectations_for_real_example_com_request(options = {})
29
31
  defaults = { :host => "www.example.com", :port => 80, :method => "GET",
30
32
  :path => "/",
31
33
  :response_code => 200, :response_message => "OK",
32
- :response_body => "<title>Google fake response</title>" }
34
+ :response_body => "<title>example</title>" }
33
35
  setup_expectations_for_real_request(defaults.merge(options))
34
36
  end
35
37
 
@@ -38,23 +40,13 @@ def client_specific_request_string(string)
38
40
  has_body = string.include?(" with body")
39
41
  default_headers = default_client_request_headers(method, has_body)
40
42
  if default_headers
41
- default_headers_string = WebMock::Util::Headers.normalize_headers(default_headers).inspect.gsub("\"","'")
42
- default_headers_string.gsub!(/[{}]/, "")
43
- if string.include?(" with headers")
43
+ if string.include?(" with headers")
44
44
  current_headers = JSON.parse(string.gsub(/.*with headers (\{[^}]+\}).*/, '\1').gsub("=>",":").gsub("'","\""))
45
45
  default_headers = WebMock::Util::Headers.normalize_headers(default_headers)
46
- default_headers.reject! {|k,v| current_headers.has_key?(k) }
47
- default_headers_string = default_headers.inspect.gsub("\"","'").gsub!(/[{}]/, "")
48
- string.gsub!(/(.*)(with headers \{[^}]*)(\}.*)/, '\1\2' + ", #{default_headers_string}}") if !default_headers_string.empty?
49
- string
50
- else
51
- headers_string =
52
- " with headers #{WebMock::Util::Headers.normalize_headers(default_headers).inspect.gsub("\"","'")}"
53
- string << headers_string
54
- end
46
+ default_headers.merge!(current_headers)
47
+ string.gsub!(/(.*) with headers.*/,'\1')
48
+ end
49
+ string << " with headers #{WebMock::Util::Headers.sorted_headers_string(default_headers)}"
55
50
  end
56
51
  string
57
52
  end
58
-
59
-
60
-
data/spec/webmock_spec.rb CHANGED
@@ -24,13 +24,17 @@ describe "WebMock", :shared => true do
24
24
  it "should make a real web request if request is not stubbed" do
25
25
  setup_expectations_for_real_example_com_request
26
26
  http_request(:get, "http://www.example.com/").
27
- body.should =~ /.*Google fake response.*/
27
+ body.should =~ /.*example.*/
28
28
  end
29
29
 
30
30
  it "should make a real https request if request is not stubbed" do
31
- setup_expectations_for_real_example_com_request(:port => 443)
32
- http_request(:get, "https://www.example.com/").
33
- body.should =~ /.*Google fake response.*/
31
+ setup_expectations_for_real_example_com_request(
32
+ :host => "www.paypal.com",
33
+ :port => 443,
34
+ :response_body => "hello paypal"
35
+ )
36
+ http_request(:get, "https://www.paypal.com/").
37
+ body.should =~ /.*paypal.*/
34
38
  end
35
39
 
36
40
  it "should return stubbed response if request was stubbed" do
@@ -56,6 +60,28 @@ describe "WebMock", :shared => true do
56
60
  end
57
61
  end
58
62
 
63
+ describe "is not allowed with exception for localhost" do
64
+ before(:each) do
65
+ WebMock.disable_net_connect!(:allow_localhost => true)
66
+ end
67
+
68
+ it "should return stubbed response if request was stubbed" do
69
+ stub_http_request(:get, "www.example.com").to_return(:body => "abc")
70
+ http_request(:get, "http://www.example.com/").body.should == "abc"
71
+ end
72
+
73
+ it "should raise exception if request was not stubbed" do
74
+ lambda {
75
+ http_request(:get, "http://www.example.com/")
76
+ }.should fail_with(client_specific_request_string("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
77
+ end
78
+
79
+ it "should allow a real request localhost" do
80
+ lambda {
81
+ http_request(:get, "http://localhost:12345/")
82
+ }.should raise_error(connection_refused_exception_class)
83
+ end
84
+ end
59
85
  end
60
86
 
61
87
  describe "when matching requests" do
@@ -90,9 +116,9 @@ describe "WebMock", :shared => true do
90
116
  stub_http_request(:get, "www.example.com")
91
117
  http_request(:get, "http://www.example.com/").status.should == "200"
92
118
  lambda {
93
- http_request(:post, "http://www.example.com/")
119
+ http_request(:delete, "http://www.example.com/")
94
120
  }.should fail_with(client_specific_request_string(
95
- "Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/")
121
+ "Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/")
96
122
  )
97
123
  end
98
124
 
@@ -236,14 +262,14 @@ describe "WebMock", :shared => true do
236
262
  end
237
263
 
238
264
  end
239
-
265
+
240
266
  describe "with block" do
241
-
267
+
242
268
  it "should match if block returns true" do
243
269
  stub_http_request(:get, "www.example.com").with { |request| true }
244
270
  http_request(:get, "http://www.example.com/").status.should == "200"
245
271
  end
246
-
272
+
247
273
  it "should not match if block returns false" do
248
274
  stub_http_request(:get, "www.example.com").with { |request| false }
249
275
  lambda {
@@ -251,7 +277,7 @@ describe "WebMock", :shared => true do
251
277
  }.should fail_with(client_specific_request_string(
252
278
  "Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
253
279
  end
254
-
280
+
255
281
  it "should pass the request to the block" do
256
282
  stub_http_request(:post, "www.example.com").with { |request| request.body == "wadus" }
257
283
  http_request(
@@ -262,7 +288,7 @@ describe "WebMock", :shared => true do
262
288
  }.should fail_with(client_specific_request_string(
263
289
  "Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'"))
264
290
  end
265
-
291
+
266
292
  end
267
293
 
268
294
  end
@@ -287,6 +313,25 @@ describe "WebMock", :shared => true do
287
313
  end
288
314
 
289
315
 
316
+ describe "raising timeout errors" do
317
+
318
+ it "should raise timeout exception if declared in a stubbed response" do
319
+ stub_http_request(:get, "www.example.com").to_timeout
320
+ lambda {
321
+ http_request(:get, "http://www.example.com/")
322
+ }.should raise_error(client_timeout_exception_class)
323
+ end
324
+
325
+ it "should raise exception if declared in a stubbed response after returning declared response" do
326
+ stub_http_request(:get, "www.example.com").to_return(:body => "abc").then.to_timeout
327
+ http_request(:get, "http://www.example.com/").body.should == "abc"
328
+ lambda {
329
+ http_request(:get, "http://www.example.com/")
330
+ }.should raise_error(client_timeout_exception_class)
331
+ end
332
+
333
+ end
334
+
290
335
  describe "returning stubbed responses" do
291
336
 
292
337
  it "should return declared body" do
@@ -300,10 +345,25 @@ describe "WebMock", :shared => true do
300
345
  response.headers["Content-Length"].should == "8888"
301
346
  end
302
347
 
303
- it "should return declared status" do
348
+ it "should return declared status code" do
304
349
  stub_http_request(:get, "www.example.com").to_return(:status => 500)
305
350
  http_request(:get, "http://www.example.com/").status.should == "500"
306
351
  end
352
+
353
+ it "should return declared status message" do
354
+ stub_http_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
355
+ http_request(:get, "http://www.example.com/").message.should == "Internal Server Error"
356
+ end
357
+
358
+ it "should return default status code" do
359
+ stub_http_request(:get, "www.example.com")
360
+ http_request(:get, "http://www.example.com/").status.should == "200"
361
+ end
362
+
363
+ it "should return default empty message" do
364
+ stub_http_request(:get, "www.example.com")
365
+ http_request(:get, "http://www.example.com/").message.should == ""
366
+ end
307
367
 
308
368
  it "should return body declared as IO" do
309
369
  stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
@@ -322,7 +382,7 @@ describe "WebMock", :shared => true do
322
382
  @file.should be_closed
323
383
  end
324
384
 
325
- describe "dynamic responses" do
385
+ describe "dynamic response parts" do
326
386
 
327
387
  it "should return evaluated response body" do
328
388
  stub_http_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
@@ -331,11 +391,48 @@ describe "WebMock", :shared => true do
331
391
 
332
392
  it "should return evaluated response headers" do
333
393
  stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
334
- http_request(:post, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
394
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
335
395
  end
336
396
 
337
397
  end
338
398
 
399
+ describe "dynamic responses" do
400
+
401
+ class Responder
402
+ def call(request)
403
+ {:body => request.body}
404
+ end
405
+ end
406
+
407
+ it "should return evaluated response body" do
408
+ stub_http_request(:post, "www.example.com").to_return(lambda { |request|
409
+ {:body => request.body}
410
+ })
411
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
412
+ end
413
+
414
+ it "should return evaluated response headers" do
415
+ stub_http_request(:get, "www.example.com").to_return(lambda { |request|
416
+ {:headers => request.headers}
417
+ })
418
+ http_request(:get, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
419
+ end
420
+
421
+ it "should create dynamic responses from blocks" do
422
+ stub_http_request(:post, "www.example.com").to_return do |request|
423
+ {:body => request.body}
424
+ end
425
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
426
+ end
427
+
428
+ it "should create dynamic responses from objects responding to call" do
429
+ stub_http_request(:post, "www.example.com").to_return(Responder.new)
430
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
431
+ end
432
+
433
+ end
434
+
435
+
339
436
  describe "replying raw responses from file" do
340
437
 
341
438
  before(:each) do
@@ -356,10 +453,14 @@ describe "WebMock", :shared => true do
356
453
  it "should return recorded body" do
357
454
  @response.body.size.should == 438
358
455
  end
359
-
456
+
360
457
  it "should return recorded status" do
361
458
  @response.status.should == "202"
362
- end
459
+ end
460
+
461
+ it "should return recorded status message" do
462
+ @response.message.should == "OK"
463
+ end
363
464
 
364
465
  it "should ensure file is closed" do
365
466
  @file.should be_closed
@@ -391,6 +492,10 @@ describe "WebMock", :shared => true do
391
492
  it "should return recorded status" do
392
493
  @response.status.should == "202"
393
494
  end
495
+
496
+ it "should return recorded status message" do
497
+ @response.message.should == "OK"
498
+ end
394
499
  end
395
500
 
396
501
  describe "sequences of responses" do
@@ -437,7 +542,7 @@ describe "WebMock", :shared => true do
437
542
  end
438
543
 
439
544
  describe "repeating declared responses more than once" do
440
-
545
+
441
546
  it "should repeat one response declared number of times" do
442
547
  stub_http_request(:get, "www.example.com").
443
548
  to_return({:body => "1"}).times(2).
@@ -446,8 +551,8 @@ describe "WebMock", :shared => true do
446
551
  http_request(:get, "http://www.example.com/").body.should == "1"
447
552
  http_request(:get, "http://www.example.com/").body.should == "2"
448
553
  end
449
-
450
-
554
+
555
+
451
556
  it "should repeat sequence of response declared number of times" do
452
557
  stub_http_request(:get, "www.example.com").
453
558
  to_return({:body => "1"}, {:body => "2"}).times(2).
@@ -456,10 +561,10 @@ describe "WebMock", :shared => true do
456
561
  http_request(:get, "http://www.example.com/").body.should == "2"
457
562
  http_request(:get, "http://www.example.com/").body.should == "1"
458
563
  http_request(:get, "http://www.example.com/").body.should == "2"
459
- http_request(:get, "http://www.example.com/").body.should == "3"
564
+ http_request(:get, "http://www.example.com/").body.should == "3"
460
565
  end
461
-
462
-
566
+
567
+
463
568
  it "should repeat infinitely last response even if number of declared times is lower" do
464
569
  stub_http_request(:get, "www.example.com").
465
570
  to_return({:body => "1"}).times(2)
@@ -467,17 +572,17 @@ describe "WebMock", :shared => true do
467
572
  http_request(:get, "http://www.example.com/").body.should == "1"
468
573
  http_request(:get, "http://www.example.com/").body.should == "1"
469
574
  end
470
-
575
+
471
576
  it "should give error if times is declared without specifying response" do
472
577
  lambda {
473
578
  stub_http_request(:get, "www.example.com").times(3)
474
579
  }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
475
580
  end
476
-
581
+
477
582
  end
478
-
583
+
479
584
  describe "raising declared exceptions more than once" do
480
-
585
+
481
586
  it "should repeat raising exception declared number of times" do
482
587
  stub_http_request(:get, "www.example.com").
483
588
  to_raise(MyException).times(2).
@@ -490,7 +595,7 @@ describe "WebMock", :shared => true do
490
595
  }.should raise_error(MyException, "Exception from WebMock")
491
596
  http_request(:get, "http://www.example.com/").body.should == "2"
492
597
  end
493
-
598
+
494
599
  it "should repeat raising sequence of exceptions declared number of times" do
495
600
  stub_http_request(:get, "www.example.com").
496
601
  to_raise(MyException, ArgumentError).times(2).
@@ -574,7 +679,7 @@ describe "WebMock", :shared => true do
574
679
 
575
680
  it "should fail if request was executed with different method" do
576
681
  lambda {
577
- http_request(:post, "http://www.example.com/")
682
+ http_request(:post, "http://www.example.com/", :body => "abc")
578
683
  request(:get, "http://www.example.com").should have_been_made
579
684
  }.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
580
685
  end
@@ -667,8 +772,8 @@ describe "WebMock", :shared => true do
667
772
 
668
773
  it "should succeed if request was executed with the same body" do
669
774
  lambda {
670
- http_request(:get, "http://www.example.com/", :body => "abc")
671
- request(:get, "www.example.com").with(:body => "abc").should have_been_made
775
+ http_request(:post, "http://www.example.com/", :body => "abc")
776
+ request(:post, "www.example.com").with(:body => "abc").should have_been_made
672
777
  }.should_not raise_error
673
778
  end
674
779
 
@@ -682,8 +787,8 @@ describe "WebMock", :shared => true do
682
787
 
683
788
  it "should succeed if request was executed with the same body" do
684
789
  lambda {
685
- http_request(:get, "http://www.example.com/", :body => "abc")
686
- request(:get, "www.example.com").with(:body => /^abc$/).should have_been_made
790
+ http_request(:post, "http://www.example.com/", :body => "abc")
791
+ request(:post, "www.example.com").with(:body => /^abc$/).should have_been_made
687
792
  }.should_not raise_error
688
793
  end
689
794
 
@@ -754,7 +859,7 @@ describe "WebMock", :shared => true do
754
859
  with(:headers => { :user_agent => /^MyAppName$/ }).should have_been_made
755
860
  }.should fail_with("The request GET http://www.example.com/ with headers {'User-Agent'=>/^MyAppName$/} was expected to execute 1 time but it executed 0 times")
756
861
  end
757
-
862
+
758
863
  it "should suceed if request was executed and block evaluated to true" do
759
864
  lambda {
760
865
  http_request(:post, "http://www.example.com/", :body => "wadus")
@@ -764,9 +869,9 @@ describe "WebMock", :shared => true do
764
869
 
765
870
  it "should fail if request was executed and block evaluated to false" do
766
871
  lambda {
767
- http_request(:post, "http://www.example.com/")
872
+ http_request(:post, "http://www.example.com/", :body => "abc")
768
873
  request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
769
- }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
874
+ }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
770
875
  end
771
876
 
772
877
  it "should fail if request was not expected but it executed and block matched request" do
@@ -809,11 +914,11 @@ describe "WebMock", :shared => true do
809
914
  request(:get, "http://www.example.com").should have_been_made.once
810
915
  }.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
811
916
  end
812
-
917
+
813
918
  it "should be order insensitive" do
814
- stub_request(:post, "http://www.example.com")
815
- http_request(:post, "http://www.example.com/", :body => "def")
816
- http_request(:post, "http://www.example.com/", :body => "abc")
919
+ stub_request(:post, "http://www.example.com")
920
+ http_request(:post, "http://www.example.com/", :body => "def")
921
+ http_request(:post, "http://www.example.com/", :body => "abc")
817
922
  WebMock.should have_requested(:post, "www.example.com").with(:body => "abc")
818
923
  WebMock.should have_requested(:post, "www.example.com").with(:body => "def")
819
924
  end
@@ -831,8 +936,8 @@ describe "WebMock", :shared => true do
831
936
 
832
937
  it "should verify expected requests occured" do
833
938
  lambda {
834
- http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
835
- WebMock.should have_requested(:get, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
939
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
940
+ WebMock.should have_requested(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
836
941
  }.should_not raise_error
837
942
  end
838
943
 
@@ -842,7 +947,7 @@ describe "WebMock", :shared => true do
842
947
  WebMock.should_not have_requested(:get, "http://www.example.com")
843
948
  }.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
844
949
  end
845
-
950
+
846
951
  it "should succeed if request was executed and block evaluated to true" do
847
952
  lambda {
848
953
  http_request(:post, "http://www.example.com/", :body => "wadus")
@@ -852,9 +957,9 @@ describe "WebMock", :shared => true do
852
957
 
853
958
  it "should fail if request was executed and block evaluated to false" do
854
959
  lambda {
855
- http_request(:post, "http://www.example.com/")
960
+ http_request(:post, "http://www.example.com/", :body => "abc")
856
961
  WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
857
- }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
962
+ }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
858
963
  end
859
964
 
860
965
  it "should fail if request was not expected but executed and block matched request" do
@@ -879,8 +984,8 @@ describe "WebMock", :shared => true do
879
984
 
880
985
  it "should verify expected requests occured" do
881
986
  lambda {
882
- http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
883
- assert_requested(:get, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
987
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
988
+ assert_requested(:post, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
884
989
  }.should_not raise_error
885
990
  end
886
991
 
@@ -890,14 +995,14 @@ describe "WebMock", :shared => true do
890
995
  assert_not_requested(:get, "http://www.example.com")
891
996
  }.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
892
997
  end
893
-
998
+
894
999
  it "should verify if non expected request executed and block evaluated to true" do
895
1000
  lambda {
896
1001
  http_request(:post, "http://www.example.com/", :body => "wadus")
897
1002
  assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
898
1003
  }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time")
899
1004
  end
900
-
1005
+
901
1006
  it "should verify if request was executed and block evaluated to true" do
902
1007
  lambda {
903
1008
  http_request(:post, "http://www.example.com/", :body => "wadus")
@@ -907,7 +1012,7 @@ describe "WebMock", :shared => true do
907
1012
 
908
1013
  it "should verify if request was executed and block evaluated to false" do
909
1014
  lambda {
910
- http_request(:post, "http://www.example.com/")
1015
+ http_request(:post, "http://www.example.com/", :body => "abc")
911
1016
  assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
912
1017
  }.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
913
1018
  end