# frozen_string_literal: true require 'spec_helper' require 'drillbit/requests/base' require 'drillbit/matchers/subdomain' require 'drillbit/configuration' module Drillbit module Matchers describe Subdomain do before(:each) do Drillbit.configuration.allowed_subdomains = %w{api} Drillbit.configuration.allowed_api_subdomains = %w{api} end it 'matches if the subdomain is API' do env = { 'HTTP_HOST' => 'api.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new expect(matcher.matches?(request)).to be_a TrueClass end it 'matches if the first subdomain is API' do env = { 'HTTP_HOST' => 'api.westeros.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new expect(matcher.matches?(request)).to be_a TrueClass end it 'does not match if the first subdomain is not API' do env = { 'HTTP_HOST' => 'westeros.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new expect(matcher.matches?(request)).to be_a FalseClass end it 'allows the matched subdomain to be specified' do env = { 'HTTP_HOST' => 'westeros.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new(allowed_subdomains: 'westeros') expect(matcher.matches?(request)).to be_a TrueClass end it 'allows more than one subdomain to be matched' do env = { 'HTTP_HOST' => 'westeros.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new(allowed_subdomains: %w{api westeros}) expect(matcher.matches?(request)).to be_a TrueClass env = { 'HTTP_HOST' => 'api.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new(allowed_subdomains: %w{api westeros}) expect(matcher.matches?(request)).to be_a TrueClass end it 'can match only the api subdomain' do env = { 'HTTP_HOST' => 'westeros.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new(allowed_api_subdomains: %w{westeros}) expect(matcher.matches_api_subdomain?(request)).to be_a TrueClass end it 'matches "api" as an api subdomain by default' do env = { 'HTTP_HOST' => 'api.example.com' } request = Requests::Base.resolve(env) matcher = Subdomain.new expect(matcher.matches_api_subdomain?(request)).to be_a TrueClass end end end end