-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from ifad/chore/improve-specs
Improve specs
- Loading branch information
Showing
7 changed files
with
101 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
class Failure < Hawk::Model::Base | ||
url 'https://example.org/' | ||
client_name 'Foobar' | ||
end | ||
|
||
RSpec.describe Hawk::Error do | ||
it 'raises an exception when the response is 0' do | ||
stub_request(:GET, 'https://example.org/failures/0') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 0, body: 'Not found', headers: {}) | ||
|
||
expect { Failure.find(0) }.to raise_error(described_class::Empty) | ||
end | ||
|
||
it 'raises an exception when the request is bad' do | ||
stub_request(:GET, 'https://example.org/failures/400') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 400, body: 'Not found', headers: {}) | ||
|
||
expect { Failure.find(400) }.to raise_error(described_class::BadRequest) | ||
end | ||
|
||
it 'raises an exception when the request is forbidden' do | ||
stub_request(:GET, 'https://example.org/failures/403') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 403, body: 'Not found', headers: {}) | ||
|
||
expect { Failure.find(403) }.to raise_error(described_class::Forbidden) | ||
end | ||
|
||
it 'raises an exception when the record is missing' do | ||
stub_request(:GET, 'https://example.org/failures/404') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 404, body: 'Not found', headers: {}) | ||
|
||
expect { Failure.find(404) }.to raise_error(described_class::NotFound) | ||
end | ||
|
||
it 'raises an exception when the server returns an HTTP 500' do | ||
stub_request(:GET, 'https://example.org/failures/500') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 500, body: ':(', headers: {}) | ||
|
||
expect { Failure.find(500) }.to raise_error(described_class::InternalServerError) | ||
end | ||
|
||
it 'raises an exception when the server returns unexpected HTTP codes' do | ||
stub_request(:GET, 'https://example.org/failures/666') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_return(status: 666, body: '{}', headers: {}) | ||
|
||
expect { Failure.find(666) }.to raise_error(described_class) | ||
end | ||
|
||
it 'raises an exception when the server times out' do | ||
stub_request(:GET, 'https://example.org/failures/123') | ||
.with(headers: { 'User-Agent' => 'Foobar' }) | ||
.to_timeout | ||
|
||
expect { Failure.find(123) }.to raise_error(described_class::Timeout) | ||
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
class Bear < Hawk::Model::Base | ||
url 'https://example.org/' | ||
client_name 'Foobar' | ||
|
||
schema do | ||
integer :id | ||
string :name | ||
end | ||
end | ||
|
||
RSpec.describe Hawk, '#save' do | ||
let(:bear_attributes) do | ||
{ | ||
id: 1, name: 'Paddington' | ||
} | ||
end | ||
|
||
it 'triggers an HTTP request' do | ||
stub_request(:PUT, 'https://example.org/bears') | ||
.with(body: { 'name' => 'Paddington' }, | ||
headers: { | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
'User-Agent' => 'Typhoeus - https://github.com/typhoeus/typhoeus' | ||
}) | ||
.to_return(status: 200, body: bear_attributes.to_json, headers: {}) | ||
paddington = Bear.new.tap { |b| b.name = 'Paddington' } | ||
expect { paddington.save }.not_to raise_error | ||
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
This file was deleted.
Oops, something went wrong.