|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe 'entry variants' do |
| 4 | + let(:client) { create_client } |
| 5 | + let(:branch_client) { create_client(ENV['DELIVERY_TOKEN'], ENV['API_KEY'], ENV['ENVIRONMENT'], { branch: 'stack_branch' }) } |
| 6 | + let(:entry_uid) { 'uid' } |
| 7 | + let(:category_entry) { client.content_type('category').entry(entry_uid) } |
| 8 | + let(:category_query) { client.content_type('category').query } |
| 9 | + |
| 10 | + describe Contentstack::Entry do |
| 11 | + it 'stores variant UID and branch on the query' do |
| 12 | + entry = category_entry.variants('variant1', 'branch_name') |
| 13 | + expect(entry.query[:variant_uids]).to eq 'variant1' |
| 14 | + expect(entry.query[:branch]).to eq 'branch_name' |
| 15 | + end |
| 16 | + |
| 17 | + it 'stores multiple variant UIDs' do |
| 18 | + entry = category_entry.variants(['variant1', 'variant2'], 'branch_name') |
| 19 | + expect(entry.query[:variant_uids]).to eq ['variant1', 'variant2'] |
| 20 | + end |
| 21 | + |
| 22 | + it 'raises when variant UIDs are missing' do |
| 23 | + expect { category_entry.variants(nil) }.to raise_error(Contentstack::Error, /Variant UID/) |
| 24 | + expect { category_entry.variants([]) }.to raise_error(Contentstack::Error, /Variant UID/) |
| 25 | + end |
| 26 | + |
| 27 | + it 'raises when variant UIDs are invalid' do |
| 28 | + expect { category_entry.variants(123) }.to raise_error(Contentstack::Error, /String or Array/) |
| 29 | + expect { category_entry.variants(['']) }.to raise_error(Contentstack::Error, /String or Array/) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + describe Contentstack::Query do |
| 34 | + it 'stores variant UID and branch on the query' do |
| 35 | + query = category_query.variants('variant1', 'branch_name') |
| 36 | + expect(query.query[:variant_uids]).to eq 'variant1' |
| 37 | + expect(query.query[:branch]).to eq 'branch_name' |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + describe 'HTTP headers' do |
| 42 | + it 'sends x-cs-variant-uid and branch for a single entry fetch' do |
| 43 | + stub = stub_request(:get, /cdn\.contentstack\.io\/v3\/content_types\/category\/entries\/uid/). |
| 44 | + with { |req| |
| 45 | + req.headers['X-Cs-Variant-Uid'] == 'variant1' && |
| 46 | + req.headers['Branch'] == 'branch_name' |
| 47 | + }. |
| 48 | + to_return(status: 200, body: File.read(File.dirname(__FILE__) + '/fixtures/category_entry.json'), headers: {}) |
| 49 | + |
| 50 | + category_entry.variants('variant1', 'branch_name').fetch |
| 51 | + expect(stub).to have_been_requested |
| 52 | + end |
| 53 | + |
| 54 | + it 'sends comma-separated variant UIDs for multiple variants' do |
| 55 | + stub = stub_request(:get, /cdn\.contentstack\.io\/v3\/content_types\/category\/entries\/uid/). |
| 56 | + with { |req| req.headers['X-Cs-Variant-Uid'] == 'variant1, variant2' }. |
| 57 | + to_return(status: 200, body: File.read(File.dirname(__FILE__) + '/fixtures/category_entry.json'), headers: {}) |
| 58 | + |
| 59 | + category_entry.variants(['variant1', 'variant2'], 'branch_name').fetch |
| 60 | + expect(stub).to have_been_requested |
| 61 | + end |
| 62 | + |
| 63 | + it 'sends x-cs-variant-uid and branch for an entries query' do |
| 64 | + stub = stub_request(:get, /cdn\.contentstack\.io\/v3\/content_types\/category\/entries/). |
| 65 | + with { |req| |
| 66 | + !req.uri.path.include?('/entries/uid') && |
| 67 | + req.headers['X-Cs-Variant-Uid'] == 'variant1' && |
| 68 | + req.headers['Branch'] == 'branch_name' |
| 69 | + }. |
| 70 | + to_return(status: 200, body: File.read(File.dirname(__FILE__) + '/fixtures/category_entry_collection_without_count.json'), headers: {}) |
| 71 | + |
| 72 | + category_query.variants('variant1', 'branch_name').fetch |
| 73 | + expect(stub).to have_been_requested |
| 74 | + end |
| 75 | + |
| 76 | + it 'uses stack-level branch when variants branch is omitted' do |
| 77 | + stub = stub_request(:get, /cdn\.contentstack\.io\/v3\/content_types\/category\/entries\/uid/). |
| 78 | + with { |req| |
| 79 | + req.headers['X-Cs-Variant-Uid'] == 'variant1' && |
| 80 | + req.headers['Branch'] == 'stack_branch' |
| 81 | + }. |
| 82 | + to_return(status: 200, body: File.read(File.dirname(__FILE__) + '/fixtures/category_entry.json'), headers: {}) |
| 83 | + |
| 84 | + branch_client.content_type('category').entry(entry_uid).variants('variant1').fetch |
| 85 | + expect(stub).to have_been_requested |
| 86 | + end |
| 87 | + |
| 88 | + it 'does not add variant headers when variants is not chained' do |
| 89 | + stub = stub_request(:get, /cdn\.contentstack\.io\/v3\/content_types\/category\/entries\/uid/). |
| 90 | + with { |req| req.headers['X-Cs-Variant-Uid'].nil? }. |
| 91 | + to_return(status: 200, body: File.read(File.dirname(__FILE__) + '/fixtures/category_entry.json'), headers: {}) |
| 92 | + |
| 93 | + category_entry.fetch |
| 94 | + expect(stub).to have_been_requested |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments