|
89 | 89 | it { expect(subject.response_headers).to eq(headers) } |
90 | 90 | it { expect(subject.response_body).to eq(body) } |
91 | 91 | end |
| 92 | + |
| 93 | + context 'with hash missing status key' do |
| 94 | + let(:exception) { { body: 'error body' } } |
| 95 | + |
| 96 | + it { expect(subject.wrapped_exception).to be_nil } |
| 97 | + it { expect(subject.response).to eq(exception) } |
| 98 | + it { expect(subject.message).to eq('the server responded with status - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware') } |
| 99 | + end |
| 100 | + |
| 101 | + context 'with hash with status but missing request data' do |
| 102 | + let(:exception) { { status: 404, body: 'not found' } } # missing request key |
| 103 | + |
| 104 | + it { expect(subject.wrapped_exception).to be_nil } |
| 105 | + it { expect(subject.response).to eq(exception) } |
| 106 | + it { expect(subject.message).to eq('the server responded with status 404 - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware') } |
| 107 | + end |
| 108 | + |
| 109 | + context 'with hash with status and request but missing method in request' do |
| 110 | + let(:exception) { { status: 404, body: 'not found', request: { url: 'http://example.com/test' } } } # missing method |
| 111 | + |
| 112 | + it { expect(subject.wrapped_exception).to be_nil } |
| 113 | + it { expect(subject.response).to eq(exception) } |
| 114 | + it { expect(subject.message).to eq('the server responded with status 404 for http://example.com/test') } |
| 115 | + end |
| 116 | + |
| 117 | + context 'with hash with status and request but missing url in request' do |
| 118 | + let(:exception) { { status: 404, body: 'not found', request: { method: :get } } } # missing url |
| 119 | + |
| 120 | + it { expect(subject.wrapped_exception).to be_nil } |
| 121 | + it { expect(subject.response).to eq(exception) } |
| 122 | + it { expect(subject.message).to eq('the server responded with status 404 for GET ') } |
| 123 | + end |
| 124 | + |
| 125 | + context 'with properly formed Faraday::Env' do |
| 126 | + # This represents the normal case - a well-formed Faraday::Env object |
| 127 | + # with all the standard properties populated as they would be during |
| 128 | + # a typical HTTP request/response cycle |
| 129 | + let(:exception) { Faraday::Env.new } |
| 130 | + |
| 131 | + before do |
| 132 | + exception.status = 500 |
| 133 | + exception.method = :post |
| 134 | + exception.url = URI('https://api.example.com/users') |
| 135 | + exception.request = Faraday::RequestOptions.new |
| 136 | + exception.response_headers = { 'content-type' => 'application/json' } |
| 137 | + exception.response_body = '{"error": "Internal server error"}' |
| 138 | + exception.request_headers = { 'authorization' => 'Bearer token123' } |
| 139 | + exception.request_body = '{"name": "John"}' |
| 140 | + end |
| 141 | + |
| 142 | + it { expect(subject.wrapped_exception).to be_nil } |
| 143 | + it { expect(subject.response).to eq(exception) } |
| 144 | + it { expect(subject.message).to eq('the server responded with status 500 for POST https://api.example.com/users') } |
| 145 | + end |
| 146 | + |
| 147 | + context 'with Faraday::Env missing status key' do |
| 148 | + let(:exception) { Faraday::Env.new } |
| 149 | + |
| 150 | + before do |
| 151 | + exception[:body] = 'error body' |
| 152 | + # Intentionally not setting status |
| 153 | + end |
| 154 | + |
| 155 | + it { expect(subject.wrapped_exception).to be_nil } |
| 156 | + it { expect(subject.response).to eq(exception) } |
| 157 | + it { expect(subject.message).to eq('the server responded with status for ') } |
| 158 | + end |
| 159 | + |
| 160 | + context 'with Faraday::Env with direct method and url properties' do |
| 161 | + let(:exception) { Faraday::Env.new } |
| 162 | + |
| 163 | + before do |
| 164 | + exception.status = 404 |
| 165 | + exception.method = :get |
| 166 | + exception.url = URI('http://example.com/test') |
| 167 | + exception[:body] = 'not found' |
| 168 | + end |
| 169 | + |
| 170 | + it { expect(subject.wrapped_exception).to be_nil } |
| 171 | + it { expect(subject.response).to eq(exception) } |
| 172 | + it { expect(subject.message).to eq('the server responded with status 404 for GET http://example.com/test') } |
| 173 | + end |
92 | 174 | end |
93 | 175 | end |
0 commit comments