|  | 
|  | 1 | +require 'spec_helper' | 
|  | 2 | +require 'shared/versioning_examples' | 
|  | 3 | + | 
|  | 4 | +describe Grape::API do | 
|  | 5 | +  subject { Class.new(Grape::API) } | 
|  | 6 | + | 
|  | 7 | +  def app | 
|  | 8 | +    subject | 
|  | 9 | +  end | 
|  | 10 | + | 
|  | 11 | +  before do | 
|  | 12 | +    api1 = Class.new(Grape::API) | 
|  | 13 | +    api1.version %(v3 v2 v1), version_options | 
|  | 14 | +    api1.get('all') { 'v1' } | 
|  | 15 | +    api1.get('only_v1') { 'only_v1' } | 
|  | 16 | + | 
|  | 17 | +    api2 = Class.new(Grape::API) | 
|  | 18 | +    api2.version %w(v3 v2), version_options | 
|  | 19 | +    api2.get('all') { 'v2' } | 
|  | 20 | +    api2.get('only_v2') { 'only_v2' } | 
|  | 21 | + | 
|  | 22 | +    api3 = Class.new(Grape::API) | 
|  | 23 | +    api3.version 'v3', version_options | 
|  | 24 | +    api3.get('all') { 'v3' } | 
|  | 25 | + | 
|  | 26 | +    app.mount api3 | 
|  | 27 | +    app.mount api2 | 
|  | 28 | +    app.mount api1 | 
|  | 29 | +  end | 
|  | 30 | + | 
|  | 31 | +  shared_examples 'version fallback' do | 
|  | 32 | +    it 'returns the correct version' do | 
|  | 33 | +      versioned_get '/all', 'v1', version_options | 
|  | 34 | +      expect(last_response.status).to eq(200) | 
|  | 35 | +      expect(last_response.body).to eq('v1') | 
|  | 36 | + | 
|  | 37 | +      versioned_get '/all', 'v2', version_options | 
|  | 38 | +      expect(last_response.status).to eq(200) | 
|  | 39 | +      expect(last_response.body).to eq('v2') | 
|  | 40 | + | 
|  | 41 | +      versioned_get '/all', 'v3', version_options | 
|  | 42 | +      expect(last_response.status).to eq(200) | 
|  | 43 | +      expect(last_response.body).to eq('v3') | 
|  | 44 | + | 
|  | 45 | +      versioned_get '/only_v1', 'v2', version_options | 
|  | 46 | +      expect(last_response.status).to eq(200) | 
|  | 47 | +      expect(last_response.body).to eq('only_v1') | 
|  | 48 | + | 
|  | 49 | +      versioned_get '/only_v1', 'v3', version_options | 
|  | 50 | +      expect(last_response.status).to eq(200) | 
|  | 51 | +      expect(last_response.body).to eq('only_v1') | 
|  | 52 | + | 
|  | 53 | +      versioned_get '/only_v2', 'v3', version_options | 
|  | 54 | +      expect(last_response.status).to eq(200) | 
|  | 55 | +      expect(last_response.body).to eq('only_v2') | 
|  | 56 | +    end | 
|  | 57 | +  end | 
|  | 58 | + | 
|  | 59 | +  context 'with catch-all' do | 
|  | 60 | +    before do | 
|  | 61 | +      app.route :any, '*path' do | 
|  | 62 | +        error!("Unrecognized request path: #{params[:path]} - #{env['PATH_INFO']}#{env['SCRIPT_NAME']}", 404) | 
|  | 63 | +      end | 
|  | 64 | +    end | 
|  | 65 | + | 
|  | 66 | +    shared_examples 'catch-all' do | 
|  | 67 | +      it 'returns a 404' do | 
|  | 68 | +        get '/foobar' | 
|  | 69 | +        expect(last_response.status).to eq(404) | 
|  | 70 | +        expect(last_response.body).to eq('Unrecognized request path: foobar - /foobar') | 
|  | 71 | +      end | 
|  | 72 | +    end | 
|  | 73 | + | 
|  | 74 | +    context 'using path' do | 
|  | 75 | +      let(:version_options) { { using: :path } } | 
|  | 76 | + | 
|  | 77 | +      it_behaves_like 'version fallback' | 
|  | 78 | +      it_behaves_like 'catch-all' | 
|  | 79 | +    end | 
|  | 80 | + | 
|  | 81 | +    context 'using param' do | 
|  | 82 | +      let(:version_options) { { using: :param } } | 
|  | 83 | + | 
|  | 84 | +      it_behaves_like 'version fallback' | 
|  | 85 | +      it_behaves_like 'catch-all' | 
|  | 86 | +    end | 
|  | 87 | + | 
|  | 88 | +    context 'using accept_version_header' do | 
|  | 89 | +      let(:version_options) { { using: :accept_version_header } } | 
|  | 90 | + | 
|  | 91 | +      it_behaves_like 'version fallback' | 
|  | 92 | +      it_behaves_like 'catch-all' | 
|  | 93 | +    end | 
|  | 94 | + | 
|  | 95 | +    context 'using header' do | 
|  | 96 | +      let(:version_options) { { using: :header, vendor: 'test' } } | 
|  | 97 | + | 
|  | 98 | +      it_behaves_like 'version fallback' | 
|  | 99 | +      it_behaves_like 'catch-all' | 
|  | 100 | +    end | 
|  | 101 | +  end | 
|  | 102 | + | 
|  | 103 | +  context 'without catch-all' do | 
|  | 104 | +    context 'using path' do | 
|  | 105 | +      let(:version_options) { { using: :path } } | 
|  | 106 | + | 
|  | 107 | +      it_behaves_like 'version fallback' | 
|  | 108 | +    end | 
|  | 109 | + | 
|  | 110 | +    context 'using param' do | 
|  | 111 | +      let(:version_options) { { using: :param } } | 
|  | 112 | + | 
|  | 113 | +      it_behaves_like 'version fallback' | 
|  | 114 | +    end | 
|  | 115 | + | 
|  | 116 | +    context 'using accept_version_header' do | 
|  | 117 | +      let(:version_options) { { using: :accept_version_header } } | 
|  | 118 | + | 
|  | 119 | +      it_behaves_like 'version fallback' | 
|  | 120 | +    end | 
|  | 121 | + | 
|  | 122 | +    context 'using header' do | 
|  | 123 | +      let(:version_options) { { using: :header, vendor: 'test' } } | 
|  | 124 | + | 
|  | 125 | +      it_behaves_like 'version fallback' | 
|  | 126 | +    end | 
|  | 127 | +  end | 
|  | 128 | +end | 
0 commit comments