low 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/lib/low.rb +0 -1
- data/lib/low/rack/default.rb +19 -0
- data/lib/low/rack/exceptions.rb +2 -2
- data/lib/low/rack/log_level.rb +1 -1
- data/lib/low/rack/rack_errors.rb +1 -1
- data/lib/low/rack/request_id.rb +11 -9
- data/lib/low/rack/request_logger.rb +2 -2
- data/lib/low/version.rb +1 -1
- data/spec/low/rack/default_spec.rb +50 -0
- data/spec/low/rack/log_level_spec.rb +7 -0
- data/spec/low/rack/rack_errors_spec.rb +7 -0
- data/spec/low/rack/request_id_spec.rb +10 -4
- metadata +4 -1
data/Gemfile.lock
CHANGED
data/lib/low.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Low
|
2
|
+
module Rack
|
3
|
+
class Default
|
4
|
+
|
5
|
+
LOGGER_KEY = 'rack.logger'
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
env['low.show_exceptions'] =
|
13
|
+
['development', 'test'].include? ENV['RACK_ENV']
|
14
|
+
|
15
|
+
@app.call(env)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/low/rack/exceptions.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'low'
|
1
|
+
require 'low/rack/default'
|
2
2
|
|
3
3
|
module Low
|
4
4
|
module Rack
|
@@ -10,7 +10,7 @@ module Low
|
|
10
10
|
|
11
11
|
def initialize(app, opts = {})
|
12
12
|
@app = app
|
13
|
-
@logger_key = opts[:logger_key] || Low::
|
13
|
+
@logger_key = opts[:logger_key] || Low::Rack::Default::LOGGER_KEY
|
14
14
|
end
|
15
15
|
|
16
16
|
def call(env)
|
data/lib/low/rack/log_level.rb
CHANGED
data/lib/low/rack/rack_errors.rb
CHANGED
data/lib/low/rack/request_id.rb
CHANGED
@@ -21,19 +21,21 @@ module Low
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def call(env)
|
24
|
-
|
25
|
-
req = ::Rack::Request.new(env)
|
24
|
+
unless env['low.request_id']
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
env['useless.request_id'] = req['request_id']
|
26
|
+
# If there is a request_id parameter,
|
27
|
+
req = ::Rack::Request.new(env)
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
# and it's valid, use it;
|
30
|
+
if req['request_id'] and RequestId.is_valid?(req['request_id'])
|
31
|
+
env['low.request_id'] = req['request_id']
|
32
|
+
|
33
|
+
# otherwise, use the generated one
|
34
|
+
else
|
35
|
+
env['low.request_id'] = @current_request_id.to_s
|
36
|
+
end
|
34
37
|
end
|
35
38
|
|
36
|
-
# and call the app.
|
37
39
|
@app.call(env)
|
38
40
|
end
|
39
41
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'logger'
|
2
|
-
require 'low'
|
2
|
+
require 'low/rack/default'
|
3
3
|
require 'low/scoped_logger'
|
4
4
|
|
5
5
|
module Low
|
@@ -12,7 +12,7 @@ module Low
|
|
12
12
|
|
13
13
|
def initialize(app, opts = {})
|
14
14
|
@app = app
|
15
|
-
@key = opts[:key] || Low::
|
15
|
+
@key = opts[:key] || Low::Rack::Default::LOGGER_KEY
|
16
16
|
end
|
17
17
|
|
18
18
|
def call(env)
|
data/lib/low/version.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
require 'low/rack/default'
|
4
|
+
|
5
|
+
describe Low::Rack::Default do
|
6
|
+
def app(key)
|
7
|
+
base = lambda do |env|
|
8
|
+
[200, {'Content-Type' => 'text/plain'}, [env[key]]]
|
9
|
+
end
|
10
|
+
|
11
|
+
Low::Rack::Default.new(base)
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'low.show_exceptions' do
|
15
|
+
it 'should set low.show_exceptions to true in development' do
|
16
|
+
begin
|
17
|
+
ENV['RACK_ENV'] = 'development'
|
18
|
+
response = app('low.show_exceptions').call({})
|
19
|
+
response[2].first.should == true
|
20
|
+
ensure
|
21
|
+
ENV['RACK_ENV'] = 'test'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should set low.show_exceptions to true in test' do
|
26
|
+
response = app('low.show_exceptions').call({})
|
27
|
+
response[2].first.should == true
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should set low.show_exceptions to false in production' do
|
31
|
+
begin
|
32
|
+
ENV['RACK_ENV'] = 'production'
|
33
|
+
response = app('low.show_exceptions').call({})
|
34
|
+
response[2].first.should == false
|
35
|
+
ensure
|
36
|
+
ENV['RACK_ENV'] = 'test'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should set low.show_exceptions to false in any other environment' do
|
41
|
+
begin
|
42
|
+
ENV['RACK_ENV'] = 'stage'
|
43
|
+
response = app('low.show_exceptions').call({})
|
44
|
+
response[2].first.should == false
|
45
|
+
ensure
|
46
|
+
ENV['RACK_ENV'] = 'test'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -60,4 +60,11 @@ describe Low::Rack::LogLevel do
|
|
60
60
|
response = rack.call({})
|
61
61
|
response[2].should == Logger::DEBUG
|
62
62
|
end
|
63
|
+
|
64
|
+
it 'should not set low.log_level if it is already set' do
|
65
|
+
ENV['LOG_LEVEL'] = 'DEBUG'
|
66
|
+
rack = Low::Rack::LogLevel.new test_app, default_level: Logger::DEBUG
|
67
|
+
response = rack.call({'low.log_level' => Logger::INFO})
|
68
|
+
response[2].should == Logger::INFO
|
69
|
+
end
|
63
70
|
end
|
@@ -40,4 +40,11 @@ describe Low::Rack::RackErrors do
|
|
40
40
|
response = rack.call({})
|
41
41
|
response[2].should == STDOUT
|
42
42
|
end
|
43
|
+
|
44
|
+
it 'should not set rack.errors if it is already set' do
|
45
|
+
ENV['RACK_ENV'] = 'test'
|
46
|
+
rack = Low::Rack::RackErrors.new test_app
|
47
|
+
response = rack.call('rack.errors' => STDOUT)
|
48
|
+
response[2].should == STDOUT
|
49
|
+
end
|
43
50
|
end
|
@@ -4,27 +4,33 @@ require 'low/rack/request_id'
|
|
4
4
|
describe Low::Rack::RequestId do
|
5
5
|
def app
|
6
6
|
app = lambda do |env|
|
7
|
-
[200, {'Content-Type' => 'text/plain'}, ['Request Id: ' + env['
|
7
|
+
[200, {'Content-Type' => 'text/plain'}, ['Request Id: ' + env['low.request_id']]]
|
8
8
|
end
|
9
9
|
|
10
10
|
Low::Rack::RequestId.new(app)
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'should add a request ID to the env while proxying transparently' do
|
14
|
-
res = Rack::MockRequest.new(app).get('http://
|
14
|
+
res = Rack::MockRequest.new(app).get('http://low.edu')
|
15
15
|
res.should be_ok
|
16
16
|
res.body.should =~ /Request Id: [a-z0-9]{1,}/
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'should use the ID specified in the query parameter' do
|
20
|
-
res = Rack::MockRequest.new(app).get('http://
|
20
|
+
res = Rack::MockRequest.new(app).get('http://low.edu?request_id=jah')
|
21
21
|
res.should be_ok
|
22
22
|
res.body.should == 'Request Id: jah'
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should not use the ID specified it does not have "simple" characters' do
|
26
|
-
res = Rack::MockRequest.new(app).get('http://
|
26
|
+
res = Rack::MockRequest.new(app).get('http://low.edu?request_id=(jah)')
|
27
27
|
res.should be_ok
|
28
28
|
res.body.should_not == 'Request Id: (jah)'
|
29
29
|
end
|
30
|
+
|
31
|
+
it 'should not set low.request_id if it is already set' do
|
32
|
+
set_app = lambda { |env| app.call(env.merge('low.request_id' => 'special request ID')) }
|
33
|
+
res = Rack::MockRequest.new(set_app).get('http://low.edu?request_id=jah')
|
34
|
+
res.body.should == 'Request Id: special request ID'
|
35
|
+
end
|
30
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: low
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- lib/low/mongo.rb
|
110
110
|
- lib/low/mongo/heroku.rb
|
111
111
|
- lib/low/mongo/util.rb
|
112
|
+
- lib/low/rack/default.rb
|
112
113
|
- lib/low/rack/exceptions.rb
|
113
114
|
- lib/low/rack/log_level.rb
|
114
115
|
- lib/low/rack/rack_errors.rb
|
@@ -120,6 +121,7 @@ files:
|
|
120
121
|
- low.gemspec
|
121
122
|
- spec/low/mongo/util_spec.rb
|
122
123
|
- spec/low/mongo_spec.rb
|
124
|
+
- spec/low/rack/default_spec.rb
|
123
125
|
- spec/low/rack/exceptions_spec.rb
|
124
126
|
- spec/low/rack/log_level_spec.rb
|
125
127
|
- spec/low/rack/rack_errors_spec.rb
|
@@ -155,6 +157,7 @@ summary: A low-level utility library.
|
|
155
157
|
test_files:
|
156
158
|
- spec/low/mongo/util_spec.rb
|
157
159
|
- spec/low/mongo_spec.rb
|
160
|
+
- spec/low/rack/default_spec.rb
|
158
161
|
- spec/low/rack/exceptions_spec.rb
|
159
162
|
- spec/low/rack/log_level_spec.rb
|
160
163
|
- spec/low/rack/rack_errors_spec.rb
|