1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
# frozen_string_literal: true
require 'spec_helper'
describe Grape::Middleware::Versioner::AcceptVersionHeader do
subject { described_class.new(app, **(@options || {})) }
let(:app) { ->(env) { [200, env, env] } }
before do
@options = {
version_options: {
using: :accept_version_header
}
}
end
context 'api.version' do
before do
@options[:versions] = ['v1']
end
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
expect(env['api.version']).to eql 'v1'
expect(status).to eq(200)
end
it 'is set if format provided' do
status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
expect(env['api.version']).to eql 'v1'
expect(status).to eq(200)
end
it 'fails with 406 Not Acceptable if version is not supported' do
expect do
subject.call('HTTP_ACCEPT_VERSION' => 'v2').last
end.to throw_symbol(
:error,
status: 406,
headers: { 'X-Cascade' => 'pass' },
message: 'The requested version is not supported.'
)
end
end
it 'succeeds if :strict is not set' do
expect(subject.call('HTTP_ACCEPT_VERSION' => '').first).to eq(200)
expect(subject.call({}).first).to eq(200)
end
it 'succeeds if :strict is set to false' do
@options[:version_options][:strict] = false
expect(subject.call('HTTP_ACCEPT_VERSION' => '').first).to eq(200)
expect(subject.call({}).first).to eq(200)
end
context 'when :strict is set' do
before do
@options[:versions] = ['v1']
@options[:version_options][:strict] = true
end
it 'fails with 406 Not Acceptable if header is not set' do
expect do
subject.call({}).last
end.to throw_symbol(
:error,
status: 406,
headers: { 'X-Cascade' => 'pass' },
message: 'Accept-Version header must be set.'
)
end
it 'fails with 406 Not Acceptable if header is empty' do
expect do
subject.call('HTTP_ACCEPT_VERSION' => '').last
end.to throw_symbol(
:error,
status: 406,
headers: { 'X-Cascade' => 'pass' },
message: 'Accept-Version header must be set.'
)
end
it 'succeeds if proper header is set' do
expect(subject.call('HTTP_ACCEPT_VERSION' => 'v1').first).to eq(200)
end
end
context 'when :strict and cascade: false' do
before do
@options[:versions] = ['v1']
@options[:version_options][:strict] = true
@options[:version_options][:cascade] = false
end
it 'fails with 406 Not Acceptable if header is not set' do
expect do
subject.call({}).last
end.to throw_symbol(
:error,
status: 406,
headers: {},
message: 'Accept-Version header must be set.'
)
end
it 'fails with 406 Not Acceptable if header is empty' do
expect do
subject.call('HTTP_ACCEPT_VERSION' => '').last
end.to throw_symbol(
:error,
status: 406,
headers: {},
message: 'Accept-Version header must be set.'
)
end
it 'succeeds if proper header is set' do
expect(subject.call('HTTP_ACCEPT_VERSION' => 'v1').first).to eq(200)
end
end
end
|