-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add raise_error feature (#792)
- Loading branch information
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
module Features | ||
class RaiseError < Feature | ||
def initialize(ignore: []) | ||
super() | ||
|
||
@ignore = ignore | ||
end | ||
|
||
def wrap_response(response) | ||
return response if response.code < 400 | ||
return response if @ignore.include?(response.code) | ||
|
||
raise HTTP::StatusError, response | ||
end | ||
|
||
HTTP::Options.register_feature(:raise_error, self) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe HTTP::Features::RaiseError do | ||
subject(:feature) { described_class.new(ignore: ignore) } | ||
|
||
let(:connection) { double } | ||
let(:status) { 200 } | ||
let(:ignore) { [] } | ||
|
||
describe "#wrap_response" do | ||
subject(:result) { feature.wrap_response(response) } | ||
|
||
let(:response) do | ||
HTTP::Response.new( | ||
version: "1.1", | ||
status: status, | ||
headers: {}, | ||
connection: connection, | ||
request: HTTP::Request.new(verb: :get, uri: "https://example.com") | ||
) | ||
end | ||
|
||
context "when status is 200" do | ||
it "returns original request" do | ||
expect(result).to be response | ||
end | ||
end | ||
|
||
context "when status is 399" do | ||
let(:status) { 399 } | ||
|
||
it "returns original request" do | ||
expect(result).to be response | ||
end | ||
end | ||
|
||
context "when status is 400" do | ||
let(:status) { 400 } | ||
|
||
it "raises" do | ||
expect { result }.to raise_error(HTTP::StatusError, "Unexpected status code 400") | ||
end | ||
end | ||
|
||
context "when status is 599" do | ||
let(:status) { 599 } | ||
|
||
it "raises" do | ||
expect { result }.to raise_error(HTTP::StatusError, "Unexpected status code 599") | ||
end | ||
end | ||
|
||
context "when error status is ignored" do | ||
let(:status) { 500 } | ||
let(:ignore) { [500] } | ||
|
||
it "returns original request" do | ||
expect(result).to be response | ||
end | ||
end | ||
end | ||
end |