rack-requestid 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/README.md +33 -0
- data/Rakefile +7 -0
- data/lib/rack/request_id.rb +33 -0
- data/rack-requestid.gemspec +16 -0
- data/spec/requestid_spec.rb +68 -0
- data/spec/spec_helper.rb +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05cb5461dd64235e8c10705dfff1a7f029769f7b
|
4
|
+
data.tar.gz: 0fcbb9da47dbfad13a2362748b674e240110beb3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 95860cc5316b6f625e762f127f13c432747b18734ec531c776cf3f7d7ae3a3925b523a93605287b28dca145026a563ecc0f368f4b103d62a2a7f6277e4e5a75b
|
7
|
+
data.tar.gz: 06cdf2688ce0351d92189d7b46a6eccb2a8d354b15bba90387ef8dbcff2ccf52a091ab9c4c628f09a87800548c8514d3b59d7ea747daac20949e911e9bbf2214
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Rack::RequestID, a Rack middleware for generating request IDs
|
2
|
+
|
3
|
+
This is a simple middleware that will generate a request GUID if one was not
|
4
|
+
already provided by the client or platform.
|
5
|
+
|
6
|
+
A request ID can be specified in the request (by the client or platform) by
|
7
|
+
setting the `X-Request-Id` header, and it will be available to other middleware
|
8
|
+
via the `HTTP_X_REQUEST_ID` key in the Rack environment hash. If a request ID
|
9
|
+
was not provided, Rack::RequestID will generate one and insert it into the Rack
|
10
|
+
environment. It will also insert the request ID into the `X-Request-Id` header
|
11
|
+
of the response.
|
12
|
+
|
13
|
+
## Configuration
|
14
|
+
|
15
|
+
To use Rack::RequestID, add the following to your Gemfile:
|
16
|
+
|
17
|
+
gem 'rack-requestid', :github => 'dancavallaro/rack-requestid'
|
18
|
+
|
19
|
+
Then in `config.ru`:
|
20
|
+
|
21
|
+
use Rack::RequestID
|
22
|
+
|
23
|
+
Use Rack::RequestID before any middleware that needs a request ID.
|
24
|
+
|
25
|
+
There are a few options you can pass in:
|
26
|
+
|
27
|
+
* `:include_response_header` determines whether to include the request ID in a response header.
|
28
|
+
* `:overwrite` can be enabled to overwrite any request ID provided by the platform or client.
|
29
|
+
* `:generator` can be specified to provide a custom request ID generator.
|
30
|
+
|
31
|
+
If you want to modify the options used, simply do:
|
32
|
+
|
33
|
+
use Rack::RequestID, :include_response_header => false, :generator => lambda { "#{Time.now.getutc.to_i}" }
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class RequestID
|
5
|
+
|
6
|
+
REQUEST_ID_HEADER = 'X-Request-Id'
|
7
|
+
REQUEST_ID_KEY = 'HTTP_X_REQUEST_ID'
|
8
|
+
|
9
|
+
def initialize(app, options={})
|
10
|
+
@app = app
|
11
|
+
|
12
|
+
@include_response_header = options.fetch(:include_response_header, true)
|
13
|
+
@overwrite = options.fetch(:overwrite, false)
|
14
|
+
@generator = options[:generator] || lambda { SecureRandom.uuid }
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
if !env.key?(REQUEST_ID_KEY) || @overwrite
|
19
|
+
request_id = @generator.call
|
20
|
+
env[REQUEST_ID_KEY] = request_id
|
21
|
+
end
|
22
|
+
|
23
|
+
status, headers, body = @app.call(env)
|
24
|
+
|
25
|
+
if @include_response_header
|
26
|
+
headers[REQUEST_ID_HEADER] = env[REQUEST_ID_KEY]
|
27
|
+
end
|
28
|
+
|
29
|
+
[status, headers, body]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rack-requestid'
|
3
|
+
s.version = '0.1'
|
4
|
+
s.summary = 'Middleware for generating request IDs.'
|
5
|
+
s.description = 'Rack::RequestID is a Rack middleware for generating request IDs. It generates a request GUID for every request (if one was not already provided) and inserts the request ID as a response header.'
|
6
|
+
s.author = 'Dan Cavallaro'
|
7
|
+
s.email = 'dan.t.cavallaro@gmail.com'
|
8
|
+
s.homepage = 'https://github.com/dancavallaro/rack-requestid'
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
12
|
+
|
13
|
+
s.add_development_dependency 'rspec'
|
14
|
+
s.add_development_dependency 'rack'
|
15
|
+
s.add_development_dependency 'rake'
|
16
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
REQUEST_ID_HEADER = 'X-Request-Id'
|
4
|
+
REQUEST_ID_KEY = 'HTTP_X_REQUEST_ID'
|
5
|
+
|
6
|
+
TEST_REQUEST_ID = '00000000-0000-0000-0000-000000000000'
|
7
|
+
|
8
|
+
describe Rack::RequestID do
|
9
|
+
|
10
|
+
let(:app) { lambda { |env| [200, {}, [env.to_yaml]] } }
|
11
|
+
let(:stack) { Rack::RequestID.new app }
|
12
|
+
let(:request) { Rack::MockRequest.new stack }
|
13
|
+
|
14
|
+
it "should generate random request IDs" do
|
15
|
+
first_requestid = get_id_from_response request.get('/')
|
16
|
+
second_requestid = get_id_from_response request.get('/')
|
17
|
+
|
18
|
+
expect(first_requestid).not_to eq(second_requestid)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return the request ID in the response headers" do
|
22
|
+
response = request.get '/'
|
23
|
+
requestid = get_id_from_response(response)
|
24
|
+
|
25
|
+
expect(response.headers[REQUEST_ID_HEADER]).to eq(requestid)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not overwrite existing request ID" do
|
29
|
+
response = request.get '/', REQUEST_ID_KEY => TEST_REQUEST_ID
|
30
|
+
|
31
|
+
expect(get_id_from_response(response)).to eq(TEST_REQUEST_ID)
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when header disabled" do
|
35
|
+
let(:stack) { Rack::RequestID.new app, :include_response_header => false }
|
36
|
+
|
37
|
+
it "should not insert response header" do
|
38
|
+
response = request.get '/'
|
39
|
+
|
40
|
+
expect(response.headers).not_to have_key(REQUEST_ID_HEADER)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when using custom generator" do
|
45
|
+
let(:stack) { Rack::RequestID.new app, :generator => lambda { TEST_REQUEST_ID } }
|
46
|
+
|
47
|
+
it "should use correct request ID" do
|
48
|
+
requestid = get_id_from_response request.get('/')
|
49
|
+
|
50
|
+
expect(requestid).to eq(TEST_REQUEST_ID)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when overwriting request ID" do
|
55
|
+
let(:stack) { Rack::RequestID.new app, :overwrite => true }
|
56
|
+
|
57
|
+
it "should overwrite request ID" do
|
58
|
+
response = request.get '/', REQUEST_ID_KEY => TEST_REQUEST_ID
|
59
|
+
|
60
|
+
expect(get_id_from_response(response)).not_to eq(TEST_REQUEST_ID)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_id_from_response(response)
|
65
|
+
YAML.load(response.body)[REQUEST_ID_KEY]
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-requestid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Cavallaro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Rack::RequestID is a Rack middleware for generating request IDs. It generates
|
56
|
+
a request GUID for every request (if one was not already provided) and inserts the
|
57
|
+
request ID as a response header.
|
58
|
+
email: dan.t.cavallaro@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/rack/request_id.rb
|
69
|
+
- rack-requestid.gemspec
|
70
|
+
- spec/requestid_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/dancavallaro/rack-requestid
|
73
|
+
licenses: []
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.4
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Middleware for generating request IDs.
|
95
|
+
test_files:
|
96
|
+
- spec/requestid_spec.rb
|
97
|
+
- spec/spec_helper.rb
|