Sha256: 5688dd033c40ca708a8b6be62ef5e8be8093ad08cac9988b0b5952b3b0951817

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

require 'rest_in_peace/template_sanitizer'

describe RESTinPeace::TemplateSanitizer do

  let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params) }

  describe '#url' do
    subject { template_sanitizer.url }

    context 'single token' do
      let(:params) { { id: 1 } }
      let(:url_template) { '/a/:id' }
      specify { expect(subject).to eq('/a/1') }
    end

    context 'multiple token' do
      let(:params) { { id: 2, a_id: 1 } }
      let(:url_template) { '/a/:a_id/b/:id' }
      specify { expect(subject).to eq('/a/1/b/2') }
    end

    context 'incomplete params' do
      let(:params) { {} }
      let(:url_template) { '/a/:id' }
      specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
    end

    context 'immutability of the url template' do
      let(:params) { { id: 1 } }
      let(:url_template) { '/a/:id' }
      specify { expect { subject }.to_not change { url_template } }
    end

    context 'immutability of the params' do
      let(:params) { { id: 1 } }
      let(:url_template) { '/a/:id' }
      specify { expect { subject }.to_not change { params } }
    end
  end

  describe '#tokens' do
    let(:params) { {} }

    context 'single token' do
      let(:url_template) { '/a/:id' }
      subject { template_sanitizer.tokens }
      specify { expect(subject).to eq(%w(id)) }
    end

    context 'multiple tokens' do
      let(:url_template) { '/a/:a_id/b/:id' }
      subject { template_sanitizer.tokens }
      specify { expect(subject).to eq(%w(a_id id)) }
    end
  end

  describe '#leftover_params' do
    let(:params) { { id: 1, name: 'test' } }
    let(:url_template) { '/a/:id' }
    subject { template_sanitizer.leftover_params }

    specify { expect(subject).to eq({name: 'test'}) }
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rest-in-peace-1.4.0 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.3.1 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.3.0 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.2.1 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.2.0 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.1.1 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.1.0 spec/rest_in_peace/template_sanitizer_spec.rb
rest-in-peace-1.0.0 spec/rest_in_peace/template_sanitizer_spec.rb