|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "spec_helper" |
| 4 | +require "active_support/core_ext/string/strip" |
4 | 5 |
|
5 | 6 | RSpec.describe Veryfi::Request do |
6 | 7 | include_context :with_veryfi_client |
|
32 | 33 | stub_request(:get, /\.*/).to_return(status: 400, body: '{"code": 400, "error": "Bad Request"}') |
33 | 34 | end |
34 | 35 |
|
35 | | - it "raises error" do |
36 | | - expect { client.document.all }.to raise_error( |
37 | | - Veryfi::Error::BadRequest, |
38 | | - "400, Bad Request" |
39 | | - ) |
40 | | - end |
41 | | - end |
42 | | - |
43 | | - context "when server responds with 401 error" do |
44 | | - before do |
45 | | - stub_request(:get, /\.*/).to_return(status: 401, body: '{"error": "Unauthorized Access Token"}') |
| 36 | + let(:expected_error) do |
| 37 | + <<-TEXT.strip_heredoc.chomp |
| 38 | + { |
| 39 | + "code": 400, |
| 40 | + "error": "Bad Request" |
| 41 | + } |
| 42 | + TEXT |
46 | 43 | end |
47 | 44 |
|
48 | 45 | it "raises error" do |
49 | 46 | expect { client.document.all }.to raise_error( |
50 | | - Veryfi::Error::UnauthorizedAccessToken, |
51 | | - "401, Unauthorized Access Token" |
| 47 | + Veryfi::Error::VeryfiError, |
| 48 | + expected_error |
52 | 49 | ) |
53 | 50 | end |
54 | 51 | end |
55 | 52 |
|
56 | | - context "when server responds with 404 error" do |
| 53 | + context "when server responds with 400 error and details" do |
57 | 54 | before do |
58 | | - stub_request(:get, /\.*/).to_return(status: 404, body: '{"error": "Resource Not Found"}') |
59 | | - end |
60 | | - |
61 | | - it "raises error" do |
62 | | - expect { client.document.all }.to raise_error( |
63 | | - Veryfi::Error::ResourceNotFound, |
64 | | - "404, Resource not found" |
| 55 | + stub_request(:get, /\.*/).to_return( |
| 56 | + status: 400, |
| 57 | + body: <<-JSON |
| 58 | + { |
| 59 | + "code": 400, |
| 60 | + "error": "Bad Request", |
| 61 | + "details": [ |
| 62 | + { |
| 63 | + "type": "value_error", |
| 64 | + "loc": [], |
| 65 | + "msg": "Value error, Only one of ..." |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + JSON |
65 | 70 | ) |
66 | 71 | end |
67 | | - end |
68 | 72 |
|
69 | | - context "when server responds with 405 error" do |
70 | | - before do |
71 | | - stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}') |
72 | | - end |
| 73 | + let(:expected_error) do |
| 74 | + <<-TEXT.strip_heredoc.chomp |
| 75 | + { |
| 76 | + "code": 400, |
| 77 | + "error": "Bad Request", |
| 78 | + "details": [ |
| 79 | + { |
| 80 | + "type": "value_error", |
| 81 | + "loc": [ |
73 | 82 |
|
74 | | - it "raises error" do |
75 | | - expect { client.document.all }.to raise_error( |
76 | | - Veryfi::Error::UnexpectedHTTPMethod, |
77 | | - "405, Unexpected HTTP Method" |
78 | | - ) |
79 | | - end |
80 | | - end |
81 | | - |
82 | | - context "when server responds with 409 error" do |
83 | | - before do |
84 | | - stub_request(:get, /\.*/).to_return(status: 409, body: '{"error": "Access Limit Reached"}') |
| 83 | + ], |
| 84 | + "msg": "Value error, Only one of ..." |
| 85 | + } |
| 86 | + ] |
| 87 | + } |
| 88 | + TEXT |
85 | 89 | end |
86 | 90 |
|
87 | 91 | it "raises error" do |
88 | 92 | expect { client.document.all }.to raise_error( |
89 | | - Veryfi::Error::AccessLimitReached, |
90 | | - "409, Access Limit Reached" |
91 | | - ) |
92 | | - end |
93 | | - end |
94 | | - |
95 | | - context "when server responds with 500 error" do |
96 | | - before do |
97 | | - stub_request(:get, /\.*/).to_return(status: 500, body: '{"error": "Internal Server Error"}') |
98 | | - end |
99 | | - |
100 | | - it "raises error" do |
101 | | - expect { client.document.all }.to raise_error( |
102 | | - Veryfi::Error::InternalError, |
103 | | - "500, Internal Server Error" |
104 | | - ) |
| 93 | + Veryfi::Error::VeryfiError |
| 94 | + ) { |error| |
| 95 | + expect(error.to_s).to eq(expected_error) |
| 96 | + } |
105 | 97 | end |
106 | 98 | end |
107 | 99 |
|
|
117 | 109 | ) |
118 | 110 | end |
119 | 111 | end |
120 | | - |
121 | | - context "when server responds with unknown error and body" do |
122 | | - before do |
123 | | - stub_request(:get, /\.*/).to_return(status: 504, body: '{"error": "Gateway Timeout"}') |
124 | | - end |
125 | | - |
126 | | - it "raises error" do |
127 | | - expect { client.document.all }.to raise_error( |
128 | | - Veryfi::Error::VeryfiError, |
129 | | - "504, Gateway Timeout" |
130 | | - ) |
131 | | - end |
132 | | - end |
133 | 112 | end |
0 commit comments