Sha256: 967e956fe565e84f99ccbc10e59ab71c67aa1d34b6e4964f12c4ca02c20b946a

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

require 'cgi'
describe 'access_logs' do
  let(:downstream_url) { 'http://localhost:12345/experiment1' }

  let(:experiment_body_1) { 'experiment1_body' }

  let(:access_logger) { StringIO.new }

  before do
    WebMock.enable!
  end

  let(:app) do
    downstream_url = downstream_url()
    access_logger = access_logger()

    sitehub = SiteHub.build do
      access_logger access_logger
      error_logger StringIO.new

      proxy '/endpoint' do
        split(label: :experiment1, percentage: 100) do
          split percentage: 100, label: 'variant1', url: downstream_url
        end
      end
    end
    Async::Middleware.new(sitehub)
  end

  let(:query_string) { '' }
  let(:request_url) { "/endpoint#{query_string.empty? ? '' : "?#{query_string}"}" }

  subject do
    query_string_hash = CGI.parse(query_string).collect { |key, value| [key, value.first] }.to_h
    stub_request(:get, downstream_url).with(query: query_string_hash)
    get(request_url)
    access_logger.string
  end

  context 'query string' do
    context 'present' do
      let(:query_string) { 'key=value' }
      it 'logs it' do
        expect(subject).to include(query_string)
      end
    end

    context 'not present' do
      let(:query_string) { '' }
      it 'is not logged' do
        expect(subject).to include(query_string)
      end
    end
  end

  it 'logs the transaction id' do
    expect(subject).to match(/transaction_id:.*?\s/)
  end

  it 'logs the response status' do
    expect(subject).to include('200')
  end

  it 'logs the downstream url that was proxied to' do
    expect(subject).to include("#{request_url} => #{downstream_url}")
  end

  it 'has the required format' do
    processing_time_matcher = '\d{1}\.\d{4}'
    transaction_id_matcher = '[a-z\d]+'
    expected_response_status = 200
    puts subject
    expect(subject).to match(/transaction_id:#{transaction_id_matcher}:\s"GET\s#{request_url}\s=>\s#{downstream_url}\s"\s#{expected_response_status}\s-\s#{processing_time_matcher}/)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha7 spec/integration/access_logs_spec.rb
sitehub-0.5.0.alpha6 spec/integration/access_logs_spec.rb