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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
# frozen_string_literal: true
require 'spec_helper'
describe Grape::Middleware::Versioner::Header do
subject { described_class.new(app, **(@options || {})) }
let(:app) { ->(env) { [200, env, env] } }
before do
@options = {
version_options: {
using: :header,
vendor: 'vendor'
}
}
end
context 'api.type and api.subtype' do
it 'sets type and subtype to first choice of content type if no preference given' do
status, _, env = subject.call('HTTP_ACCEPT' => '*/*')
expect(env['api.type']).to eql 'application'
expect(env['api.subtype']).to eql 'vnd.vendor+xml'
expect(status).to eq(200)
end
it 'sets preferred type' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/*')
expect(env['api.type']).to eql 'application'
expect(env['api.subtype']).to eql 'vnd.vendor+xml'
expect(status).to eq(200)
end
it 'sets preferred type and subtype' do
status, _, env = subject.call('HTTP_ACCEPT' => 'text/plain')
expect(env['api.type']).to eql 'text'
expect(env['api.subtype']).to eql 'plain'
expect(status).to eq(200)
end
end
context 'api.format' do
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor+json')
expect(env['api.format']).to eql 'json'
expect(status).to eq(200)
end
it 'is nil if not provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor')
expect(env['api.format']).to be nil
expect(status).to eq(200)
end
['v1', :v1].each do |version|
context "when version is set to #{version}" do
before do
@options[:versions] = [version]
end
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
expect(env['api.format']).to eql 'json'
expect(status).to eq(200)
end
it 'is nil if not provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
expect(env['api.format']).to be nil
expect(status).to eq(200)
end
end
end
end
context 'api.vendor' do
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor')
expect(env['api.vendor']).to eql 'vendor'
expect(status).to eq(200)
end
it 'is set if format provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor+json')
expect(env['api.vendor']).to eql 'vendor'
expect(status).to eq(200)
end
it 'fails with 406 Not Acceptable if vendor is invalid' do
expect { subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor+json').last }
.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include 'API vendor not found'
end
end
context 'when version is set' do
before do
@options[:versions] = ['v1']
end
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
expect(env['api.vendor']).to eql 'vendor'
expect(status).to eq(200)
end
it 'is set if format provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
expect(env['api.vendor']).to eql 'vendor'
expect(status).to eq(200)
end
it 'fails with 406 Not Acceptable if vendor is invalid' do
expect { subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor-v1+json').last }
.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include('API vendor not found')
end
end
end
end
context 'api.version' do
before do
@options[:versions] = ['v1']
end
it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-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' => 'application/vnd.vendor-v1+json')
expect(env['api.version']).to eql 'v1'
expect(status).to eq(200)
end
it 'fails with 406 Not Acceptable if version is invalid' do
expect { subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v2+json').last }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidVersionHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include('API version not found')
end
end
end
it 'succeeds if :strict is not set' do
expect(subject.call('HTTP_ACCEPT' => '').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' => '').first).to eq(200)
expect(subject.call({}).first).to eq(200)
end
it 'succeeds if :strict is set to false and given an invalid header' do
@options[:version_options][:strict] = false
expect(subject.call('HTTP_ACCEPT' => 'yaml').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 { subject.call({}).last }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include('Accept header must be set.')
end
end
it 'fails with 406 Not Acceptable if header is empty' do
expect { subject.call('HTTP_ACCEPT' => '').last }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include('Accept header must be set.')
end
end
it 'succeeds if proper header is set' do
expect(subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json').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 { subject.call({}).last }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql({})
expect(exception.status).to be 406
expect(exception.message).to include('Accept header must be set.')
end
end
it 'fails with 406 Not Acceptable if header is application/xml' do
expect { subject.call('HTTP_ACCEPT' => 'application/xml').last }
.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql({})
expect(exception.status).to be 406
expect(exception.message).to include('API vendor or version not found.')
end
end
it 'fails with 406 Not Acceptable if header is empty' do
expect { subject.call('HTTP_ACCEPT' => '').last }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql({})
expect(exception.status).to be 406
expect(exception.message).to include('Accept header must be set.')
end
end
it 'fails with 406 Not Acceptable if header contains a single invalid accept' do
expect { subject.call('HTTP_ACCEPT' => 'application/json;application/vnd.vendor-v1+json').first }
.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
expect(exception.headers).to eql({})
expect(exception.status).to be 406
expect(exception.message).to include('API vendor or version not found.')
end
end
it 'succeeds if proper header is set' do
expect(subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json').first).to eq(200)
end
end
context 'when multiple versions are specified' do
before do
@options[:versions] = %w[v1 v2]
end
it 'succeeds with v1' do
expect(subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json').first).to eq(200)
end
it 'succeeds with v2' do
expect(subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v2+json').first).to eq(200)
end
it 'fails with another version' do
expect { subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v3+json') }.to raise_exception do |exception|
expect(exception).to be_a(Grape::Exceptions::InvalidVersionHeader)
expect(exception.headers).to eql('X-Cascade' => 'pass')
expect(exception.status).to be 406
expect(exception.message).to include('API version not found')
end
end
end
context 'when there are multiple versions with complex vendor specified with rescue_from :all' do
subject do
Class.new(Grape::API) do
rescue_from :all
end
end
let(:v1_app) do
Class.new(Grape::API) do
version 'v1', using: :header, vendor: 'test.a-cool_resource', cascade: false, strict: true
content_type :v1_test, 'application/vnd.test.a-cool_resource-v1+json'
formatter :v1_test, ->(object, _) { object }
format :v1_test
resources :users do
get :hello do
'one'
end
end
end
end
let(:v2_app) do
Class.new(Grape::API) do
version 'v2', using: :header, vendor: 'test.a-cool_resource', strict: true
content_type :v2_test, 'application/vnd.test.a-cool_resource-v2+json'
formatter :v2_test, ->(object, _) { object }
format :v2_test
resources :users do
get :hello do
'two'
end
end
end
end
def app
subject.mount v2_app
subject.mount v1_app
subject
end
context 'with header versioned endpoints and a rescue_all block defined' do
it 'responds correctly to a v1 request' do
versioned_get '/users/hello', 'v1', using: :header, vendor: 'test.a-cool_resource'
expect(last_response.body).to eq('one')
expect(last_response.body).not_to include('API vendor or version not found')
end
it 'responds correctly to a v2 request' do
versioned_get '/users/hello', 'v2', using: :header, vendor: 'test.a-cool_resource'
expect(last_response.body).to eq('two')
expect(last_response.body).not_to include('API vendor or version not found')
end
end
end
end
|