|
| 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 |
0 commit comments