/
stub_tests.rb
182 lines (133 loc) · 5.04 KB
/
stub_tests.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
174
175
176
177
178
179
180
181
182
Shindo.tests('Excon stubs') do
tests("missing stub").raises(Excon::Errors::StubNotFound) do
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
connection.request(:method => :get, :path => '/content-length/100')
end
tests("stub({})").raises(ArgumentError) do
Excon.stub({})
end
tests("stub({}, {}) {}").raises(ArgumentError) do
Excon.stub({}, {}) {}
end
tests("stub({:method => :get}, {:body => 'body', :status => 200})") do
Excon.stub({:method => :get}, {:body => 'body', :status => 200})
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
response = connection.request(:method => :get, :path => '/content-length/100')
tests('response.body').returns('body') do
response.body
end
tests('response.headers').returns({}) do
response.headers
end
tests('response.status').returns(200) do
response.status
end
tests('request body with response_block given').returns('body') do
body = ''
response_block = lambda do |chunk, remaining_bytes, total_bytes|
body << chunk
end
connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
body
end
Excon.stubs.clear
end
tests("stub({:path => %r{/tests/(\S+)}}, {:body => $1, :status => 200})") do
Excon.stub({:path => %r{/tests/(\S+)}}) do |params|
{
:body => params[:captures][:path].first,
:status => 200
}
end
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
response = connection.request(:method => :get, :path => '/tests/test')
tests('response.body').returns('test') do
response.body
end
tests('response.headers').returns({}) do
response.headers
end
tests('response.status').returns(200) do
response.status
end
Excon.stubs.clear
end
tests("stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}") do
Excon.stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
response = connection.request(:body => 'body', :method => :get, :path => '/content-length/100')
tests('response.body').returns('body') do
response.body
end
tests('response.headers').returns({'Host' => '127.0.0.1:9292'}) do
response.headers
end
tests('response.status').returns(200) do
response.status
end
tests('request body with response block given').returns('body') do
body = ''
response_block = lambda do |chunk, remaining_bytes, total_bytes|
body << chunk
end
connection.request(:body => 'body', :method => :get, :path => '/content-length/100', :response_block => response_block)
body
end
Excon.stubs.clear
end
tests("stub({:body => File.open(...), :method => :get}, { :status => 200 })") do
file_path = File.join(File.dirname(__FILE__), 'data', 'xs')
Excon.stub(
{ :body => File.read(file_path), :method => :get },
{ :status => 200 }
)
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
response = connection.request(:body => File.open(file_path), :method => :get, :path => '/')
tests('response.status') do
response.status
end
Excon.stubs.clear
end
tests("mismatched stub").raises(Excon::Errors::StubNotFound) do
Excon.stub({:method => :post}, {:body => 'body'})
Excon.get('http://127.0.0.1:9292/', :mock => true)
end
Excon.stubs.clear
tests("stub({}, {:body => 'x' * (Excon::CHUNK_SIZE + 1)})") do
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
Excon.stub({}, {:body => 'x' * (Excon::CHUNK_SIZE + 1)})
test("with response_block") do
chunks = []
response_block = lambda do |chunk, remaining_bytes, total_bytes|
chunks << chunk
end
connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
chunks == ['x' * Excon::CHUNK_SIZE, 'x']
end
end
Excon.stubs.clear
tests("stub({}, {:status => 404, :body => 'Not Found'}") do
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
Excon.stub({}, {:status => 404, :body => 'Not Found'})
tests("request(:expects => 200, :method => :get, :path => '/')").raises(Excon::Errors::NotFound) do
connection.request(:expects => 200, :method => :get, :path => '/')
end
test("request(:expects => 200, :method => :get, :path => '/') with block does not invoke the block since it raises an error") do
block_called = false
begin
response_block = lambda do |_,_,_|
block_called = true
end
connection.request(:expects => 200, :method => :get, :path => '/', :response_block => response_block)
rescue Excon::Errors::NotFound
end
!block_called
end
Excon.stubs.clear
end
tests('mock = false') do
with_rackup('basic.ru') do
basic_tests
end
end
end