Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 16 #21

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/siren_client/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(data, config={})
if data.class != Hash
raise ArgumentError, "You must pass in a Hash to SirenClient::Action.new"
end
@payload = data
@payload = data.deep_stringify_keys

@config = { format: :json }.merge config
@name = @payload['name'] || ''
Expand Down
2 changes: 1 addition & 1 deletion lib/siren_client/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(data, config={})
raise InvalidResponseError, e.message
end
elsif data.class == Hash
@payload = data
@payload = data.deep_stringify_keys
else
raise ArgumentError, "You must pass in either a url(String) or an entity(Hash) to SirenClient::Entity.new"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/siren_client/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(data)
if data.class != Hash
raise ArgumentError, "You must pass in a Hash to SirenClient::Action.new"
end
@payload = data
@payload = data.deep_stringify_keys

@name = @payload['name'] || ''
@type = @payload['type'] || 'text'
Expand Down
2 changes: 1 addition & 1 deletion lib/siren_client/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(data, config={})
if data.class != Hash
raise ArgumentError, "You must pass in a Hash to SirenClient::Link.new"
end
@payload = data
@payload = data.deep_stringify_keys
@config = { format: :json }.merge config

@rels = @payload['rel'] || []
Expand Down
1 change: 1 addition & 0 deletions spec/helper/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'siren_client'
require 'coveralls'
Coveralls.wear!
Dir["./spec/support/shared_examples/*.rb"].sort.each {|f| require f}

RSpec.configure do |config|
# Ensure we only use `expect` and not `should`.
Expand Down
108 changes: 108 additions & 0 deletions spec/support/shared_examples/siren_client_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
shared_examples 'a SirenClient::Action' do
describe '.config' do
it 'is a hash' do
expect(action.config).to be_a Hash
end

it 'can access a property of the config' do
expect(action.config[:headers]['Accept']).to eq('application/json')
end
end

describe 'when initialized with data' do
describe '.payload' do
it 'is a hash' do
expect(action.payload).to be_a Hash
end

it 'is NOT overwritten with SirenClient::Field classes' do
expect(action.payload['fields'][0]).to be_a Hash
end
end

describe '.name' do
it 'is a string' do
expect(action.name).to eq action_data['name']
end
end

describe '.classes' do
it 'is an array' do
expect(action.classes).to match_array action_data['class']
end
end

describe '.method' do
it 'is a string' do
expect(action.method).to eq 'get'
end
end

describe '.href' do
it 'is a string' do
expect(action.href).to eq action_data['href']
end

it 'can change .href as needed' do
action.href = action.href + '?query=test'
expect(/query=test/).to match(action.href)
end
end

describe '.title' do
it 'is a string' do
expect(action.title).to eq action_data['title']
end
end

describe '.type' do
it 'is a string' do
expect(action.type).to eq action_data['type']
end
end

describe '.fields' do
it 'is an array' do
expect(action.fields).to be_a Array
end

it 'is an array of SirenClient::Field\'s' do
action.fields.each do |field|
expect(field).to be_a SirenClient::Field
end
end
end
end

describe '.where(params)' do
it 'executes the GET action' do
# I'm expecting an error here, all I want to see is that the url it being traversed.
expect { action.where(test: 'hi') }.to raise_error SirenClient::InvalidResponseError
end

it 'GET action does not send the Content-Type header' do
expect { action.where(test: 'hi') }.to raise_error SirenClient::InvalidResponseError
expect(output_buffer.string).not_to include('Content-Type: application/x-www-form-urlencoded')
end

it 'executes the POST action' do
# I'm expecting an error here, all I want to see is that the url it being traversed.
expect { action_post.where(test: 'hi') }.to raise_error SirenClient::InvalidResponseError
end

it 'POST action does send the Content-Type header' do
expect { action_post.where(test: 'hi') }.to raise_error SirenClient::InvalidResponseError
expect(output_buffer.string).to include('Content-Type: application/x-www-form-urlencoded')
end

it 'executes the DELETE action' do
expect { action_delete.submit }.to raise_error SirenClient::InvalidResponseError
end
end

describe '.submit' do
it 'executes the action without any parameters' do
expect { action.submit }.to raise_error SirenClient::InvalidResponseError
end
end
end
Loading