Skip to content

Commit

Permalink
Add RSpec examples to test the adding of custom fields via LogStasher
Browse files Browse the repository at this point in the history
  • Loading branch information
aldavidson committed Nov 24, 2023
1 parent 4fa7abc commit d56ce6f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions spec/lib/govuk_json_logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require "govuk_app_config/govuk_json_logging"
require "rack/test"

class TestController < ActionController::Base
include LogStasher::ActionController::Instrumentation
def index; end
end

RSpec.describe GovukJsonLogging do
let(:govuk_headers_class) do
Class.new do
Expand Down Expand Up @@ -78,6 +83,66 @@ def self.headers
expect(fake_stdout.read).to match(/test default log entry/)
end

context "given a block" do
it "evals the block" do
done = false
expect {
GovukJsonLogging.configure do
done = true
end
}.to change { done }.to(true)
end

context "and the block configures custom fields" do
describe "any subsequently-created ActionController" do
let(:headers) { { "REMOTE_ADDR" => "10.10.10.10" } }
let(:mock_request) { ActionDispatch::TestRequest.new(Rack::MockRequest.env_for("http://example.com:8080/", headers)) }
let(:mock_response) { ActionDispatch::TestResponse.new }

before do
GovukJsonLogging.configure do
add_custom_fields do |fields|
fields[:govuk_custom_field] = request.headers["GOVUK-Custom-Header"]
end
end

@controller = TestController.new
allow(@controller).to receive(:request).and_return(mock_request)
allow(@controller).to receive(:response).and_return(mock_response)
end

it "has a logstasher_add_custom_fields_to_payload method" do
expect(@controller.methods).to include(:logstasher_add_custom_fields_to_payload)
end

describe "calling the logstasher_add_custom_fields_to_payload" do
let(:payload) { {} }

it "executes the block" do
expect(@controller).to receive(:logstasher_add_custom_fields_to_payload)
@controller.send(:append_info_to_payload, payload)
end

it "adds the custom fields to the payload" do
@controller.send(:append_info_to_payload, payload)
expect(payload.keys).to include(:govuk_custom_field)
end

context "when the custom field has a value" do
before do
mock_request.headers["GOVUK-Custom-header"] = "My header value"
end

it "sets the custom field value in the payload" do
@controller.send(:append_info_to_payload, payload)
expect(payload[:govuk_custom_field]).to eq("My header value")
end
end
end
end
end
end

describe "when making requests to the application" do
include Rack::Test::Methods

Expand Down

0 comments on commit d56ce6f

Please sign in to comment.