forked from bblimke/webmock
/
curb_spec.rb
173 lines (144 loc) · 4.88 KB
/
curb_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'webmock_shared'
unless RUBY_PLATFORM =~ /java/
require 'curb_spec_helper'
shared_examples_for "Curb" do
include CurbSpecHelper
it_should_behave_like "WebMock"
describe "when doing PUTs" do
it "should stub them" do
stub_http_request(:put, "www.example.com").with(:body => "01234")
http_request(:put, "http://www.example.com", :body => "01234").
status.should == "200"
end
end
end
describe "Curb features" do
before(:each) do
WebMock.disable_net_connect!
WebMock.reset!
end
describe "callbacks" do
before(:each) do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
end
it "should call on_success with 2xx response" do
body = "on_success fired"
stub_request(:any, "example.com").to_return(:body => body)
test = nil
@curl.on_success do |c|
test = c.body_str
end
@curl.http_get
test.should == body
end
it "should call on_failure with 5xx response" do
response_code = 599
stub_request(:any, "example.com").
to_return(:status => [response_code, "Server On Fire"])
test = nil
@curl.on_failure do |c, code|
test = code
end
@curl.http_get
test.should == response_code
end
it "should call on_body when response body is read" do
body = "on_body fired"
stub_request(:any, "example.com").
to_return(:body => body)
test = nil
@curl.on_body do |data|
test = data
end
@curl.http_get
test.should == body
end
it "should call on_header when response headers are read" do
stub_request(:any, "example.com").
to_return(:headers => {:one => 1})
test = nil
@curl.on_header do |data|
test = data
end
@curl.http_get
test.should match /One: 1/
end
it "should call on_complete when request is complete" do
body = "on_complete fired"
stub_request(:any, "example.com").to_return(:body => body)
test = nil
@curl.on_complete do |curl|
test = curl.body_str
end
@curl.http_get
test.should == body
end
it "should call on_progress when portion of response body is read" do
stub_request(:any, "example.com").to_return(:body => "01234")
test = nil
@curl.on_progress do |*args|
args.length.should == 4
args.each {|arg| arg.is_a?(Float).should == true }
test = true
end
@curl.http_get
test.should == true
end
it "should call callbacks in correct order on successful request" do
stub_request(:any, "example.com")
order = []
@curl.on_success {|*args| order << :on_success }
@curl.on_failure {|*args| order << :on_failure }
@curl.on_header {|*args| order << :on_header }
@curl.on_body {|*args| order << :on_body }
@curl.on_complete {|*args| order << :on_complete }
@curl.on_progress {|*args| order << :on_progress }
@curl.http_get
order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_success]
end
it "should call callbacks in correct order on successful request" do
stub_request(:any, "example.com").to_return(:status => [500, ""])
order = []
@curl.on_success {|*args| order << :on_success }
@curl.on_failure {|*args| order << :on_failure }
@curl.on_header {|*args| order << :on_header }
@curl.on_body {|*args| order << :on_body }
@curl.on_complete {|*args| order << :on_complete }
@curl.on_progress {|*args| order << :on_progress }
@curl.http_get
order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_failure]
end
end
end
describe "Webmock with Curb" do
describe "using #http for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::DynamicHttp
it "should work with uppercase arguments" do
stub_request(:get, "www.example.com").to_return(:body => "abc")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.http(:GET)
c.body_str.should == "abc"
end
end
describe "using #http_* methods for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::NamedHttp
end
describe "using #perform for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::Perform
end
describe "using .http_* methods for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::ClassNamedHttp
end
describe "using .perform for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::ClassPerform
end
end
end