require 'spec_helper'

module VCAP::CloudController
  module Diego
    RSpec.describe LifecycleBundleUriGenerator do
      before do
        TestConfig.override(diego: { file_server_url: 'https://file-server.example.com:1234' })
      end

      it 'creates a file server url for a bundle path' do
        expect(LifecycleBundleUriGenerator.uri('path/bundle.tgz')).to eq('https://file-server.example.com:1234/v1/static/path/bundle.tgz')
      end

      it 'returns an error when passed nil' do
        expect { LifecycleBundleUriGenerator.uri(nil) }.to raise_error(
          VCAP::CloudController::Diego::LifecycleBundleUriGenerator::InvalidStack,
          /no compiler defined for requested stack/
        )
      end

      it 'returns back a full url' do
        expect(LifecycleBundleUriGenerator.uri('http://bundle.example.com')).to eq('http://bundle.example.com')
      end

      it 'raises an error for a non http or https url' do
        expect do
          LifecycleBundleUriGenerator.uri('ftp://bundle.example.com')
        end.to raise_error(VCAP::CloudController::Diego::LifecycleBundleUriGenerator::InvalidCompiler, /invalid compiler URI/)
      end
    end
  end
end
