Sha256: 76b0e8837fa4702dcd130fb1b611fe69cf2f618d291121479f351813cf32c541

Contents?: true

Size: 1.65 KB

Versions: 31

Compression:

Stored size: 1.65 KB

Contents

require 'spec_helper'
require 'routemaster/responses/hateoas_response'

# need to laod this here to resolve circular dependency
require 'routemaster/resources/rest_resource'

describe Routemaster::Responses::HateoasResponse do
  let(:response) { double('Response', status: status, body: body, headers: headers) }
  let(:status) { 200 }
  let(:body) { {}.to_json }
  let(:headers) { {} }

  subject { described_class.new(response) }

  context 'link traversal' do
    let(:body) do
      {
        '_links' => {
          'self' => { 'href' => 'self_url' },
          'resource_a' => { 'href' => 'resource_a_url' },
          'resource_b' => { 'href' => 'resource_b_url' }
        }
      }
    end

    it 'creates a method for every key in _links attribute' do
      expect(subject.resource_a.url).to eq('resource_a_url')
      expect(subject.resource_b.url).to eq('resource_b_url')
    end

    it 'creates a _self method if there is a link with name self' do
      expect(subject._self.url).to eq('self_url')
    end

    it 'raise an exception when requested link does not exist' do
      expect { subject.some_unsupported_link }.to raise_error(NoMethodError)
    end

    describe '#body_without_links' do
      before do
        body.merge!('foo' => 'bar')
      end

      it 'returns the body without the _links key' do
        expect(subject.body_without_links).to eq({ 'foo' => 'bar' })
      end
    end

    describe '#has?' do
      it 'returns true for an existing link' do
        expect(subject.has?(:resource_a)).to be_truthy
      end

      it 'returns false for a non existing link' do
        expect(subject.has?(:other_resource)).to be_falsy
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.7.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.8 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.7 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.6 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.5 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.4 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.3 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.2 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.1 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.6.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.5.1 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.5.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.4.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.3.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.2.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.1.0 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.0.3 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.0.2 spec/routemaster/responses/hateoas_response_spec.rb
routemaster-drain-3.0.1 spec/routemaster/responses/hateoas_response_spec.rb