Skip to content

Commit 90348e8

Browse files
committed
added tests for bad responses
1 parent 6e52b9a commit 90348e8

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

spec/shared/ex_bad_response.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
shared_examples "bad response handling" do |method, endpoint|
2+
context 'with a bad response' do
3+
let(:method) { method }
4+
let(:endpoint) { endpoint }
5+
6+
def make_request(**kwargs)
7+
subject.send("#{method}_request", endpoint, **kwargs)
8+
end
9+
10+
context 'if the response has a 4xx status' do
11+
let(:response) {{
12+
body: JSON.generate(error: 'BadReq', message: 'This request was bad'),
13+
status: 403,
14+
headers: { 'Content-Type': 'application/json' }
15+
}}
16+
17+
it 'should raise a ClientErrorResponse error' do
18+
expect { make_request }.to raise_error { |err|
19+
err.should be_a(Minisky::ClientErrorResponse)
20+
err.status.should == 403
21+
err.data.should be_a(Hash)
22+
err.error_type.should == 'BadReq'
23+
err.error_message.should == 'This request was bad'
24+
}
25+
end
26+
end
27+
28+
context 'if the response has a 5xx status' do
29+
let(:response) {{
30+
body: JSON.generate(error: 'Boom', message: 'Server exploded'),
31+
status: 500,
32+
headers: { 'Content-Type': 'application/json' }
33+
}}
34+
35+
it 'should raise a ServerErrorResponse error' do
36+
expect { make_request }.to raise_error { |err|
37+
err.should be_a(Minisky::ServerErrorResponse)
38+
err.status.should == 500
39+
err.data.should be_a(Hash)
40+
err.error_type.should == 'Boom'
41+
err.error_message.should == 'Server exploded'
42+
}
43+
end
44+
end
45+
46+
context 'if the response is a redirect' do
47+
let(:response) {{ status: 302, headers: { 'Location': 'https://google.com' }}}
48+
49+
it 'should raise an UnexpectedRedirect error' do
50+
expect { make_request }.to raise_error { |err|
51+
err.should be_a(Minisky::UnexpectedRedirect)
52+
err.status.should == 302
53+
err.data.should be_a(Hash)
54+
err.location.should == 'https://google.com'
55+
}
56+
end
57+
end
58+
59+
context 'if the response is an ExpiredToken error' do
60+
let(:response) {{
61+
body: JSON.generate(error: 'ExpiredToken', message: 'Your token has expired'),
62+
status: 401,
63+
headers: { 'Content-Type': 'application/json' }
64+
}}
65+
66+
it 'should raise an ExpiredTokenError error' do
67+
expect { make_request }.to raise_error { |err|
68+
err.should be_a(Minisky::ExpiredTokenError)
69+
err.status.should == 401
70+
err.data.should be_a(Hash)
71+
err.error_type.should == 'ExpiredToken'
72+
err.error_message.should == 'Your token has expired'
73+
}
74+
end
75+
end
76+
77+
context 'if the response is not json' do
78+
let(:response) {{
79+
body: '<html>wtf</html>',
80+
status: 503
81+
}}
82+
83+
it 'should raise an error with the response body' do
84+
expect { make_request }.to raise_error { |err|
85+
err.should be_a(Minisky::BadResponse)
86+
err.status.should == 503
87+
err.data.should == '<html>wtf</html>'
88+
err.error_type.should be_nil
89+
err.error_message.should be_nil
90+
}
91+
end
92+
end
93+
end
94+
end

spec/shared/ex_get_request.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
require_relative 'ex_authorization'
2+
require_relative 'ex_bad_response'
23

34
shared_examples "get_request" do
45
describe '#get_request' do
56
before do
6-
stub_request(:get, %r(https://#{host}/xrpc/com.example.service.getStuff(\?.*)?))
7-
.to_return_json(body: { "result": 123 })
7+
stub_request(:get, %r(https://#{host}/xrpc/com.example.service.getStuff(\?.*)?)).to_return(response)
88
end
99

10+
let(:response) {{ body: JSON.generate({ "result": 123 }), headers: { content_type: 'application/json' }}}
11+
1012
it 'should make a request to the given XRPC endpoint' do
1113
subject.get_request('com.example.service.getStuff')
1214

@@ -70,6 +72,8 @@
7072
end
7173
end
7274

75+
include_examples "bad response handling", :get, 'com.example.service.getStuff'
76+
7377
include_examples "authorization",
7478
request: ->(subject, params) { subject.get_request('com.example.service.getStuff', **params) },
7579
expected: ->(host) { [:get, "https://#{host}/xrpc/com.example.service.getStuff"] }

spec/shared/ex_post_request.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative 'ex_authorization'
2+
require_relative 'ex_bad_response'
23

34
shared_examples "post_request" do
45
describe '#post_request' do
@@ -235,6 +236,8 @@
235236
end
236237
end
237238

239+
include_examples "bad response handling", :post, 'com.example.service.doStuff'
240+
238241
include_examples "authorization",
239242
request: ->(subject, params) { subject.post_request('com.example.service.doStuff', **params) },
240243
expected: ->(host) { [:post, "https://#{host}/xrpc/com.example.service.doStuff"] }

0 commit comments

Comments
 (0)