-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTPEvents: filter headers using
http_header_filters
configuration (#…
…10)
- Loading branch information
Showing
5 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ jobs: | |
- 2.5 | ||
- 2.4 | ||
- 2.3 | ||
- 2.2 | ||
- jruby-9.4.3.0 | ||
- jruby-9.2.14.0 | ||
- truffleruby-23.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Logtail | ||
module Integrations | ||
module Rack | ||
VERSION = "0.2.1" | ||
VERSION = "0.2.2" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require "spec_helper" | ||
require "logtail-rack/config" | ||
require 'stringio' | ||
|
||
|
||
RSpec.describe Logtail::Integrations::Rack::HTTPEvents do | ||
let(:app) { ->(env) { [200, env, "app"] } } | ||
let(:mock_request) { Rack::MockRequest.env_for('https://example.com/test-page', { 'HTTP_AUTHORIZATION' => 'Bearer secret_token', 'HTTP_CONTENT_TYPE' => 'text/plain' }) } | ||
|
||
let :middleware do | ||
described_class.new(app) | ||
end | ||
|
||
it "log HTTP request and response" do | ||
logs = capture_logs { middleware.call mock_request } | ||
|
||
expect(logs.map { |log| log['message'] }).to match(['Started GET "/test-page"', /Completed 200 OK in \d+\.\d+ms/]) | ||
end | ||
|
||
it "log HTTP request headers" do | ||
logs = capture_logs { middleware.call mock_request } | ||
|
||
request_headers_json = logs.first["event"]["http_request_received"]["headers_json"] | ||
expect(JSON.parse(request_headers_json)).to eq({"Authorization" => "Bearer secret_token", "Content_Type" => "text/plain"}) | ||
end | ||
|
||
it "filter HTTP request headers using http_header_filters" do | ||
logs = capture_logs { with_http_header_filters(%w[Authorization]) { middleware.call mock_request } } | ||
|
||
request_headers_json = logs.first["event"]["http_request_received"]["headers_json"] | ||
expect(JSON.parse(request_headers_json)).to eq({"Authorization" => "[FILTERED]", "Content_Type" => "text/plain"}) | ||
end | ||
|
||
it "filter HTTP request headers using http_header_filters without regard to case or dashes" do | ||
logs = capture_logs { with_http_header_filters(%w[authorization CONTENT-TYPE]) { middleware.call mock_request } } | ||
|
||
request_headers_json = logs.first["event"]["http_request_received"]["headers_json"] | ||
expect(JSON.parse(request_headers_json)).to eq({"Authorization" => "[FILTERED]", "Content_Type" => "[FILTERED]"}) | ||
end | ||
|
||
it "ignores non-existent headers in http_header_filters" do | ||
logs = capture_logs { with_http_header_filters(%w[Not_Found_Header]) { middleware.call mock_request } } | ||
|
||
request_headers_json = logs.first["event"]["http_request_received"]["headers_json"] | ||
expect(JSON.parse(request_headers_json)).to eq({"Authorization" => "Bearer secret_token", "Content_Type" => "text/plain"}) | ||
end | ||
|
||
def capture_logs(&blk) | ||
old_logger = Logtail::Config.instance.logger | ||
|
||
string_io = StringIO.new | ||
logger = Logtail::Logger.new(string_io) | ||
logger.formatter = Logtail::Logger::JSONFormatter.new | ||
Logtail::Config.instance.logger = logger | ||
|
||
blk.call | ||
|
||
string_io.string.split("\n").map { |record| JSON.parse(record) } | ||
ensure | ||
Logtail::Config.instance.logger = old_logger | ||
end | ||
|
||
def with_http_header_filters(headers, &blk) | ||
previous_http_header_filters = Logtail::Integrations::Rack::HTTPEvents.http_header_filters = headers | ||
|
||
blk.call | ||
ensure | ||
Logtail::Integrations::Rack::HTTPEvents.http_header_filters = previous_http_header_filters | ||
end | ||
end |