Created
September 2, 2014 02:33
-
-
Save brandur/910df6ff22a802ef9c89 to your computer and use it in GitHub Desktop.
Reflect Input Request IDs
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
diff --git a/lib/api/middleware/request_id.rb b/lib/api/middleware/request_id.rb | |
index 9367bf7..255f4e4 100644 | |
--- a/lib/api/middleware/request_id.rb | |
+++ b/lib/api/middleware/request_id.rb | |
@@ -8,7 +8,8 @@ def initialize(app) | |
end | |
def call(env) | |
- request_ids = [SecureRandom.uuid] + extract_request_ids(env) | |
+ extracted_request_ids = extract_request_ids(env) | |
+ request_ids = [SecureRandom.uuid] + extracted_request_ids | |
# make ID of the request accessible to consumers down the stack | |
env["REQUEST_ID"] = request_ids[0] | |
@@ -19,8 +20,15 @@ def call(env) | |
status, headers, response = @app.call(env) | |
- # tag all responses with a request ID | |
- headers["Request-Id"] = request_ids[0] | |
+ # Tag all responses with a request ID. | |
+ # | |
+ # If valid request IDs were sent in from a client, pass those back. | |
+ # Otherwise, give them the default generated request ID. | |
+ headers["Request-Id"] = if extracted_request_ids.size > 0 | |
+ extracted_request_ids.join(",") | |
+ else | |
+ request_ids[0] | |
+ end | |
[status, headers, response] | |
end | |
diff --git a/spec/middleware/request_id_spec.rb b/spec/middleware/request_id_spec.rb | |
new file mode 100644 | |
index 0000000..1b78ad7 | |
--- /dev/null | |
+++ b/spec/middleware/request_id_spec.rb | |
@@ -0,0 +1,49 @@ | |
+require "spec_helper" | |
+ | |
+describe API::Middleware::RequestID do | |
+ include Rack::Test::Methods | |
+ | |
+ def app | |
+ Rack::Builder.new do | |
+ use Rack::Lint | |
+ use API::Middleware::RequestID | |
+ run Sinatra.new { | |
+ get "/" do | |
+ MultiJson.encode(env["REQUEST_IDS"]) | |
+ end | |
+ } | |
+ end | |
+ end | |
+ | |
+ it "generates a request ID for requests" do | |
+ get "/" | |
+ request_ids = MultiJson.decode(last_response.body) | |
+ assert_equal 1, request_ids.count | |
+ assert_match API::Middleware::RequestID::UUID_PATTERN, request_ids[0] | |
+ end | |
+ | |
+ it "sets a request ID in the Request-Id response header" do | |
+ get "/" | |
+ assert_match API::Middleware::RequestID::UUID_PATTERN, | |
+ last_response.headers["Request-Id"] | |
+ end | |
+ | |
+ it "allows request IDs to be passed into requests" do | |
+ input_ids = [SecureRandom.uuid, SecureRandom.uuid] | |
+ input = input_ids.join(",") | |
+ header "Request-Id", input | |
+ get "/" | |
+ request_ids = MultiJson.decode(last_response.body) | |
+ assert_equal 3, request_ids.count | |
+ assert_includes request_ids, input_ids[0] | |
+ assert_includes request_ids, input_ids[1] | |
+ end | |
+ | |
+ it "sets input request IDs in the Request-Id response header" do | |
+ input_ids = [SecureRandom.uuid, SecureRandom.uuid] | |
+ input = input_ids.join(",") | |
+ header "Request-Id", input | |
+ get "/" | |
+ assert_equal input, last_response.headers["Request-Id"] | |
+ end | |
+end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment