spec/integration/access_logs_spec.rb in sitehub-0.5.0.alpha7 vs spec/integration/access_logs_spec.rb in sitehub-0.5.0.alpha8

- old
+ new

@@ -1,6 +1,7 @@ require 'cgi' + describe 'access_logs' do let(:downstream_url) { 'http://localhost:12345/experiment1' } let(:experiment_body_1) { 'experiment1_body' } @@ -28,35 +29,46 @@ end let(:query_string) { '' } let(:request_url) { "/endpoint#{query_string.empty? ? '' : "?#{query_string}"}" } - subject do + before 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) + end + + subject do 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) + expect(subject).to include("GET #{request_url} => #{downstream_url}") end end context 'not present' do let(:query_string) { '' } it 'is not logged' do + expect(subject).to match(/"GET\s#{request_url}\s=>\s#{downstream_url}\s/) expect(subject).to include(query_string) end end end + module WebMock + def self.last_request + WebMock::RequestRegistry.instance.requested_signatures.hash.keys.first + end + end + it 'logs the transaction id' do - expect(subject).to match(/transaction_id:.*?\s/) + expected_id = WebMock.last_request.headers['Sitehub-Transaction-Id'] + expect(subject).to match(/transaction_id:#{expected_id}/) end it 'logs the response status' do expect(subject).to include('200') end @@ -67,9 +79,8 @@ 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