Sha256: da7a85c8246a5d23d56596224a0d330ead1816e9042cad97be4784368099c763

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

require File.dirname(File.dirname(__FILE__)) + '/spec_helper'

CURRENT_DIR = Dir.getwd

def without_rack_env_app
  Rack::Builder.new do
    run TestRackApp.new
  end
end

def rack_env_app
  Rack::Builder.new do
    use Rack::Env
    run TestRackApp.new
  end
end

def rack_env_app_with_argument
  Rack::Builder.new do
    use Rack::Env, envfile: ".envfile"
    run TestRackApp.new
  end
end

describe 'Rack::Env' do
  include Rack::Test::Methods

  let(:request) { get '/' }

  before do
    # move current_dir to ./spec/tmp as root_dir
    Dir::chdir(File.dirname(File.dirname(__FILE__)) + '/tmp')
  end

  after(:all) do
    Dir::chdir(CURRENT_DIR)
  end

  context "When not using Rack::Env" do
    def app
      without_rack_env_app
    end

    it "should not load environment variable" do
      request
      expect(ENV['FOO']).to eq nil
    end
  end

  context "When using Rack::Env" do
    context "default envfile" do
      def app
        rack_env_app
      end

      it "should ignore empty line and commented out line" do
        expect{
          request
        }.to change{ ENV.size }.by(2)
      end

      it "should load environment variable" do
        request
        expect(ENV['JAPAN']).to eq "Tokyo"
      end
    end

    context "specified envfile" do
      def app
        rack_env_app_with_argument
      end

      it "should ignore empty line and commented out line" do
        expect{
          request
        }.to change{ ENV.size }.by(2)
      end

      it "should load environment variable" do
        request
        expect(ENV['INDIA']).to eq "Delhi"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-env-0.1.2 spec/rack/env_spec.rb
rack-env-0.1.1 spec/rack/env_spec.rb
rack-env-0.1.0 spec/rack/env_spec.rb