Skip to content

Commit

Permalink
Failing test for stacked versions w/ catch-all
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrivadeneira committed Oct 17, 2018
1 parent b88a8df commit 8e7dbc9
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
128 changes: 128 additions & 0 deletions spec/grape/version_fallback_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'spec_helper'
require 'shared/versioning_examples'

describe Grape::API do
subject { Class.new(Grape::API) }

def app
subject
end

before do
api1 = Class.new(Grape::API)
api1.version %(v3 v2 v1), version_options
api1.get('all') { 'v1' }
api1.get('only_v1') { 'only_v1' }

api2 = Class.new(Grape::API)
api2.version %w(v3 v2), version_options
api2.get('all') { 'v2' }
api2.get('only_v2') { 'only_v2' }

api3 = Class.new(Grape::API)
api3.version 'v3', version_options
api3.get('all') { 'v3' }

app.mount api3
app.mount api2
app.mount api1
end

shared_examples 'version fallback' do
it 'returns the correct version' do
versioned_get '/all', 'v1', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v1')

versioned_get '/all', 'v2', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v2')

versioned_get '/all', 'v3', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v3')

versioned_get '/only_v1', 'v2', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('only_v1')

versioned_get '/only_v1', 'v3', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('only_v1')

versioned_get '/only_v2', 'v3', version_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('only_v2')
end
end

context 'with catch-all' do
before do
app.route :any, '*path' do
error!("Unrecognized request path: #{params[:path]} - #{env['PATH_INFO']}#{env['SCRIPT_NAME']}", 404)
end
end

shared_examples 'catch-all' do
it 'returns a 404' do
get '/foobar'
expect(last_response.status).to eq(404)
expect(last_response.body).to eq('Unrecognized request path: foobar - /foobar')
end
end

context 'using path' do
let(:version_options) { { using: :path } }

it_behaves_like 'version fallback'
it_behaves_like 'catch-all'
end

context 'using param' do
let(:version_options) { { using: :param } }

it_behaves_like 'version fallback'
it_behaves_like 'catch-all'
end

context 'using accept_version_header' do
let(:version_options) { { using: :accept_version_header } }

it_behaves_like 'version fallback'
it_behaves_like 'catch-all'
end

context 'using header' do
let(:version_options) { { using: :header, vendor: 'test' } }

it_behaves_like 'version fallback'
it_behaves_like 'catch-all'
end
end

context 'without catch-all' do
context 'using path' do
let(:version_options) { { using: :path } }

it_behaves_like 'version fallback'
end

context 'using param' do
let(:version_options) { { using: :param } }

it_behaves_like 'version fallback'
end

context 'using accept_version_header' do
let(:version_options) { { using: :accept_version_header } }

it_behaves_like 'version fallback'
end

context 'using header' do
let(:version_options) { { using: :header, vendor: 'test' } }

it_behaves_like 'version fallback'
end
end
end
2 changes: 1 addition & 1 deletion spec/support/versioned_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def versioned_get(path, version_name, version_options = {})
headers = versioned_headers(version_options.merge(version: version_name))
params = {}
if version_options[:using] == :param
params = { version_options[:parameter] => version_name }
params = { version_options.fetch(:parameter, 'apiver') => version_name }
end
get path, params, headers
end
Expand Down

0 comments on commit 8e7dbc9

Please sign in to comment.