Sha256: 9c2acce4d09c81de7c473fa23e27836ea53f3b0c02e1ce8a1d3fbb49d5c97904

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'spec_helper'
require 'apartment/elevators/subdomain'

describe Apartment::Elevators::Subdomain do

  subject(:elevator){ described_class.new(Proc.new{}) }

  describe "#parse_tenant_name" do
    context "assuming tld_length of 1" do
      it "should parse subdomain" do
        request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
        expect(elevator.parse_tenant_name(request)).to eq('foo')
      end

      it "should return nil when no subdomain" do
        request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.com')
        expect(elevator.parse_tenant_name(request)).to be_nil
      end
    end

    context "assuming tld_length of 2" do
      before do
        Apartment.configure do |config|
          config.tld_length = 2
        end
      end

      it "should parse subdomain in the third level domain" do
        request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.co.uk')
        expect(elevator.parse_tenant_name(request)).to eq("foo")
      end

      it "should return nil when no subdomain in the third level domain" do
        request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.co.uk')
        expect(elevator.parse_tenant_name(request)).to be_nil
      end
    end
  end

  describe "#call" do
    it "switches to the proper tenant" do
      expect(Apartment::Tenant).to receive(:switch).with('tenant1')
      elevator.call('HTTP_HOST' => 'tenant1.example.com')
    end

    it "ignores excluded subdomains" do
      described_class.excluded_subdomains = %w{foo}

      expect(Apartment::Tenant).not_to receive(:switch)

      elevator.call('HTTP_HOST' => 'foo.bar.com')

      described_class.excluded_subdomains = nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apartment-1.2.0 spec/unit/elevators/subdomain_spec.rb
apartment-1.1.0 spec/unit/elevators/subdomain_spec.rb