Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamoilov committed Feb 1, 2025
1 parent e36e699 commit 653fc90
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
66 changes: 66 additions & 0 deletions spec/integration/request_id_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require "http"

RSpec.describe "Request ID" do
before :all do
skip("skipping end-to-end tests") unless ENV["ENABLE_EXTERNAL_TESTS"] == "true"
end

let(:logs) { File.readlines("spec/integration/test_app/log/development.log") }

context "without the RequestID middleware" do
before :all do
launch_server
end

after :all do
stop_server
end

it "uses an internal request ID" do
response = HTTP.get("http://localhost:3000/get_request_id")
id = response.to_s

expect(id.size).to eq(16)
expect(logs.last).to start_with("[#{id}]")
expect(response.headers["x-request-id"]).to be_nil
end

it "ignores the X-Request-Id header" do
x_request_id = "my-test-request-id"
response = HTTP.headers("X-Request-Id" => x_request_id).get("http://localhost:3000/get_request_id")

expect(response.to_s).not_to eq(x_request_id)
expect(response.headers["x-request-id"]).to be_nil
end
end

context "with the RequestID middleware" do
before :all do
launch_server(env: { "ENABLE_REQUEST_ID_MIDDLEWARE" => "1" })
end

after :all do
stop_server
end

it "uses an internal request ID if X-Request-Id is not submitted" do
response = HTTP.get("http://localhost:3000/get_request_id")
id = response.to_s

expect(id.size).to eq(16)
expect(logs.last).to start_with("[#{id}]")
expect(response.headers["x-request-id"]).to eq(id)
end

it "uses the X-Request-Id value if it is submitted" do
x_request_id = "my-test-request-id"
response = HTTP.headers("X-Request-Id" => x_request_id).get("http://localhost:3000/get_request_id")

expect(response.to_s).to eq(x_request_id)
expect(logs.last).to start_with("[#{x_request_id}]")
expect(response.headers["x-request-id"]).to eq(x_request_id)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def empty
def raise_error
raise "1155 test error"
end

def get_request_id
render plain: request.request_id
end
end
4 changes: 4 additions & 0 deletions spec/integration/test_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def call(env)
Rage.configure do
config.middleware.use TestMiddleware
config.public_file_server.enabled = !!ENV["ENABLE_FILE_SERVER"]

if ENV["ENABLE_REQUEST_ID_MIDDLEWARE"]
config.middleware.use Rage::RequestId
end
end

require "rage/setup"
1 change: 1 addition & 0 deletions spec/integration/test_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
delete "delete", to: "application#delete"
get "empty", to: "application#empty"
get "raise_error", to: "application#raise_error"
get "get_request_id", to: "application#get_request_id"

get "params/digest", to: "params#digest"
post "params/digest", to: "params#digest"
Expand Down

0 comments on commit 653fc90

Please sign in to comment.