Skip to content

Commit

Permalink
replace webmock stubs with faraday stubs
Browse files Browse the repository at this point in the history
to correctly handle the serialized JSON
  • Loading branch information
koenpunt committed Nov 30, 2014
1 parent 15f4e2c commit ddd1b66
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
Empty file modified Rakefile
100644 → 100755
Empty file.
86 changes: 70 additions & 16 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ module Hyperclient
Resource.expects(:new)

link = Link.new('key', { 'href' => '/' }, entry_point)
stub_request(:get, 'http://api.example.org/').to_return(body: {}.to_json)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/') { [200, {}, nil] }
end

link._resource
end
Expand All @@ -124,14 +127,20 @@ module Hyperclient
it 'sends a GET request with the link url' do
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)

stub_request(:get, 'http://api.example.org/productions/1').to_return(body: nil)
stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._get.must_be_kind_of Resource
end

it 'raises exceptions by default' do
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)

stub_request(:get, 'http://api.example.org/productions/1').to_return(status: 400)
stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/productions/1') { [400, {}, nil] }
end

lambda { link._get }.must_raise Faraday::ClientError
end
end
Expand All @@ -140,23 +149,34 @@ module Hyperclient
it 'sends a OPTIONS request with the link url' do
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)

stub_request(:options, 'http://api.example.org/productions/1').to_return(body: nil)
stub_request(entry_point.connection) do |stub|
stub.options('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._options.must_be_kind_of Resource
end
end

describe '_head' do
it 'sends a HEAD request with the link url' do
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
stub_request(:head, 'http://api.example.org/productions/1').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.head('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._head.must_be_kind_of Resource
end
end

describe '_delete' do
it 'sends a DELETE request with the link url' do
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
stub_request(:delete, 'http://api.example.org/productions/1').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.delete('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._delete.must_be_kind_of Resource
end
end
Expand All @@ -165,12 +185,20 @@ module Hyperclient
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }

it 'sends a POST request with the link url and params' do
stub_request(:post, 'http://api.example.org/productions/1').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._post('foo' => 'bar').must_be_kind_of Resource
end

it 'defaults params to an empty hash' do
stub_request(:post, 'http://api.example.org/productions/1').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._post.must_be_kind_of Resource
end
end
Expand All @@ -179,12 +207,20 @@ module Hyperclient
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }

it 'sends a PUT request with the link url and params' do
stub_request(:put, 'http://api.example.org/productions/1').with(body: '{"foo":"bar"}').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.put('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] }
end

link._put('foo' => 'bar').must_be_kind_of Resource
end

it 'defaults params to an empty hash' do
stub_request(:put, 'http://api.example.org/productions/1').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.put('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._put.must_be_kind_of Resource
end
end
Expand All @@ -193,12 +229,19 @@ module Hyperclient
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }

it 'sends a PATCH request with the link url and params' do
stub_request(:patch, 'http://api.example.org/productions/1').with(body: '{"foo":"bar"}').to_return(body: nil)

stub_request(entry_point.connection) do |stub|
stub.patch('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] }
end

link._patch('foo' => 'bar').must_be_kind_of Resource
end

it 'defaults params to an empty hash' do
stub_request(:patch, 'http://api.example.org/productions/1').to_return(body: nil)
stub_request(entry_point.connection) do |stub|
stub.patch('http://api.example.org/productions/1') { [200, {}, nil] }
end

link._patch.must_be_kind_of Resource
end
end
Expand All @@ -216,23 +259,34 @@ module Hyperclient
describe 'delegation' do
it 'delegates when link key matches' do
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
stub_request(:get, 'http://api.example.org/orders').to_return(body: { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }.to_json)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
end

resource.orders._embedded.orders.first.id.must_equal 1
resource.orders.first.id.must_equal 1
end

it "doesn't delegate when link key doesn't match" do
resource = Resource.new({ '_links' => { 'foos' => { 'href' => '/orders' } } }, entry_point)
stub_request(:get, 'http://api.example.org/orders').to_return(body: { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }.to_json)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
end

resource.foos._embedded.orders.first.id.must_equal 1
resource.foos.first.must_equal nil
end
end

describe 'resource' do
before do
stub_request(:get, 'http://myapi.org/orders')
.to_return(body: '{"resource": "This is the resource"}')

stub_request(entry_point.connection) do |stub|
stub.get('http://myapi.org/orders') { [200, {}, '{"resource": "This is the resource"}'] }
end

Resource.stubs(:new).returns(resource)
end

Expand Down
10 changes: 9 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
require 'minitest/autorun'
require 'mocha/setup'
require 'turn'
require 'webmock/minitest'
require 'json'
require 'pry'

MiniTest::Unit::TestCase.class_eval do

def stub_request(conn, adapter_class = Faraday::Adapter::Test, &stubs_block)
adapter_handler = conn.builder.handlers.find { |h| h.klass < Faraday::Adapter }
conn.builder.swap(adapter_handler, adapter_class, &stubs_block)
end

end

0 comments on commit ddd1b66

Please sign in to comment.