forked from excon/excon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub_tests.rb
106 lines (79 loc) · 2.84 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
Shindo.tests('Excon stubs') do
Excon.mock = true
tests("missing stub").raises(Excon::Errors::StubNotFound) do
connection = Excon.new('http://127.0.0.1:9292')
response = 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')
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 block given').returns('body') do
body = ''
connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
body << chunk
end
body
end
Excon.stubs.pop
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')
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 block given').returns('body') do
body = ''
connection.request(:body => 'body', :method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
body << chunk
end
body
end
Excon.stubs.pop
end
tests("mismatched stub").raises(Excon::Errors::StubNotFound) do
Excon.stub({:method => :post}, {:body => 'body'})
Excon.get('http://127.0.0.1:9292/')
end
tests("stub({}, {:body => 'x' * (Excon::CHUNK_SIZE + 1)})") do
connection = Excon.new('http://127.0.0.1:9292')
Excon.stub({}, {:body => 'x' * (Excon::CHUNK_SIZE + 1)})
test("with block") do
chunks = []
response = connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
chunks << chunk
end
chunks == ['x' * Excon::CHUNK_SIZE, 'x']
end
end
Excon.stubs.clear
Excon.mock = false
tests('mock = false') do
with_rackup('basic.ru') do
basic_tests
end
end
end