# frozen_string_literal: true require 'spec_helper' require 'drillbit/accept_header' # rubocop:disable Metrics/LineLength module Drillbit describe AcceptHeader do it 'can validate an accept header with all the pieces of information' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep;version=1.0.0') expect(header).to be_valid end it 'can validate an accept header with multiple accept options' do header = AcceptHeader.new(application: 'westeros', header: 'application/json,application/vnd.westeros+redkeep;version=1.0.0,application/json') expect(header).to be_valid end it 'does not validate an accept header without passing an application' do header = AcceptHeader.new(application: '', header: 'application/vnd.westeros+redkeep;version=1.0.0') expect(header).not_to be_valid end it 'does not validate an accept header if it is not passed in' do header = AcceptHeader.new(application: '', header: '') expect(header).not_to be_valid header = AcceptHeader.new(application: '', header: nil) expect(header).not_to be_valid end it 'does not validate an accept header without an application in the header' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.+redkeep;version=1.0.0') expect(header).not_to be_valid header = AcceptHeader.new(application: 'westeros', header: 'application/+redkeep;version=1.0.0') expect(header).not_to be_valid header = AcceptHeader.new(application: 'westeros', header: 'application/westeros+redkeep;version=1.0.0') expect(header).not_to be_valid end it 'does not validate an accept header with an invalid version' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep;version=10..0') expect(header).not_to be_valid header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep;version=neo') expect(header).not_to be_valid header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep;version=') expect(header).not_to be_valid header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep;10.0') expect(header).not_to be_valid end it 'does validate an accept header even with a missing content type' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros;version=10.0') expect(header).to be_valid end it 'does validate an accept header with only the minimal information' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros') expect(header).to be_valid end it 'does validate an accept header with only a content type but no version' do header = AcceptHeader.new(application: 'westeros', header: 'application/vnd.westeros+redkeep') expect(header).to be_valid end it 'can extract version information from an accept header' do header = AcceptHeader.new( application: 'westeros', header: 'application/json,application/vnd.westeros+redkeep;version=10.0.0beta1,application/json', ) expect(header.version).to eql '10.0.0beta1' end it 'can extract the content type from an accept header' do header = AcceptHeader.new( application: 'westeros', header: 'application/vnd.westeros+redkeep;version=10.0.0beta1', ) expect(header.content_type).to eql 'redkeep' end end end