rack-request_police 0.0.2alpha → 0.0.3alpha
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.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/rack/request_police/middleware.rb +11 -1
- data/lib/rack/request_police/version.rb +1 -1
- data/lib/rack/request_police/web_helpers.rb +6 -0
- data/spec/middleware_spec.rb +19 -10
- data/web/views/index.erb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d13fc52b2497aa40b442a235a51589361abfe55c
|
4
|
+
data.tar.gz: 53861595d2c9c9608c818a18f9a9d716365feca1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1bee036da587a96b5f140550c61d164f8af9ca3d12e1a8776fa99e6111455c7cab8f1c5db6e22de553a85a048c037fb63179bcf9e4c1a4c5b4fe4d10a0c8749
|
7
|
+
data.tar.gz: d10d467d3aaf8579a80e99773a37236b6bd81ba6295be316ca67a018d2c3ed438e1dbc3b5d292d844a992dfae641ac33940287305c19d1e39cf6c55afd011173
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[](https://codeclimate.com/github/emq/rack-request_police)
|
4
4
|
[](https://travis-ci.org/emq/rack-request_police)
|
5
5
|
[](https://coveralls.io/r/emq/rack-request_police)
|
6
|
+
[](http://badge.fury.io/rb/rack-request_police)
|
6
7
|
[](https://gemnasium.com/emq/rack-request_police)
|
7
8
|
|
8
9
|
Rack middleware for logging selected request for further investigation / analyze.
|
@@ -17,7 +17,7 @@ module Rack
|
|
17
17
|
if !::Rack::RequestPolice.regex || full_url =~ ::Rack::RequestPolice.regex
|
18
18
|
request_params = {
|
19
19
|
'url' => full_url,
|
20
|
-
'ip' => env
|
20
|
+
'ip' => ip_address(env),
|
21
21
|
'method' => env['REQUEST_METHOD'].downcase,
|
22
22
|
'time' => Time.now.to_i
|
23
23
|
}
|
@@ -31,6 +31,16 @@ module Rack
|
|
31
31
|
|
32
32
|
@app.call(env)
|
33
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def ip_address(env)
|
38
|
+
if !env['HTTP_X_FORWARDED_FOR'] || env['HTTP_X_FORWARDED_FOR'].empty?
|
39
|
+
env['REMOTE_ADDR']
|
40
|
+
else
|
41
|
+
env['HTTP_X_FORWARDED_FOR']
|
42
|
+
end
|
43
|
+
end
|
34
44
|
end
|
35
45
|
end
|
36
46
|
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -22,7 +22,7 @@ describe "My Middleware", type: :request do
|
|
22
22
|
}
|
23
23
|
|
24
24
|
it "logs request without query params" do
|
25
|
-
|
25
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
26
26
|
.with('url' => "http://example.org/", 'ip' => "127.0.0.1", 'method' => "get", 'time' => Time.now.to_i)
|
27
27
|
|
28
28
|
get '/'
|
@@ -31,13 +31,22 @@ describe "My Middleware", type: :request do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "logs request with query params" do
|
34
|
-
|
34
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
35
35
|
.with('url' => "http://example.org/?what-the&hell=", 'ip' => "127.0.0.1", 'method' => "get", 'time' => Time.now.to_i)
|
36
36
|
|
37
37
|
get '/?what-the&hell='
|
38
38
|
|
39
39
|
expect(last_response.status).to eq 200
|
40
40
|
end
|
41
|
+
|
42
|
+
it "logs ip address from HTTP_X_FORWARDED_FOR header if avaiable" do
|
43
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
44
|
+
.with('url' => "http://example.org/", 'ip' => "1.2.3.4", 'method' => "get", 'time' => Time.now.to_i)
|
45
|
+
|
46
|
+
get '/', nil, { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4' }
|
47
|
+
|
48
|
+
expect(last_response.status).to eq 200
|
49
|
+
end
|
41
50
|
end
|
42
51
|
|
43
52
|
context "logging only POST requests" do
|
@@ -58,13 +67,13 @@ describe "My Middleware", type: :request do
|
|
58
67
|
}
|
59
68
|
|
60
69
|
it "ignores get requests" do
|
61
|
-
|
70
|
+
expect(Rack::RequestPolice.storage).not_to receive(:log_request)
|
62
71
|
get '/'
|
63
72
|
expect(last_response.status).to eq 200
|
64
73
|
end
|
65
74
|
|
66
75
|
it "logs post request with request data" do
|
67
|
-
|
76
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
68
77
|
.with('url' => "http://example.org/form", 'ip' => "127.0.0.1", 'method' => "post", 'time' => Time.now.to_i, 'data' => 'user[name]=john&user[email]=john%40test.com')
|
69
78
|
|
70
79
|
post '/form', { user: { name: 'john', email: 'john@test.com' } }
|
@@ -89,7 +98,7 @@ describe "My Middleware", type: :request do
|
|
89
98
|
}
|
90
99
|
|
91
100
|
it "logs patch request with request data" do
|
92
|
-
|
101
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
93
102
|
.with('url' => "http://example.org/update", 'ip' => "127.0.0.1", 'method' => "patch", 'time' => Time.now.to_i, 'data' => 'user[name]=john')
|
94
103
|
|
95
104
|
patch '/update', { user: { name: 'john' } }
|
@@ -114,7 +123,7 @@ describe "My Middleware", type: :request do
|
|
114
123
|
}
|
115
124
|
|
116
125
|
it "logs delete request with request data" do
|
117
|
-
|
126
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
118
127
|
.with('url' => "http://example.org/destroy", 'ip' => "127.0.0.1", 'method' => "delete", 'time' => Time.now.to_i, 'data' => 'user[id]=1')
|
119
128
|
|
120
129
|
delete '/destroy', { user: { id: 1 } }
|
@@ -141,13 +150,13 @@ describe "My Middleware", type: :request do
|
|
141
150
|
}
|
142
151
|
|
143
152
|
it "ignores queries that does not match given regex" do
|
144
|
-
|
153
|
+
expect(Rack::RequestPolice.storage).not_to receive(:log_request)
|
145
154
|
get '/account'
|
146
155
|
expect(last_response.status).to eq 200
|
147
156
|
end
|
148
157
|
|
149
158
|
it "logs matching queries" do
|
150
|
-
|
159
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
151
160
|
.with('url' => "http://example.org/user", 'ip' => "127.0.0.1", 'method' => "get", 'time' => Time.now.to_i)
|
152
161
|
|
153
162
|
get '/user'
|
@@ -172,13 +181,13 @@ describe "My Middleware", type: :request do
|
|
172
181
|
}
|
173
182
|
|
174
183
|
it "ignores queries that does not match given regex" do
|
175
|
-
|
184
|
+
expect(Rack::RequestPolice.storage).not_to receive(:log_request)
|
176
185
|
get '/user?id=2'
|
177
186
|
expect(last_response.status).to eq 200
|
178
187
|
end
|
179
188
|
|
180
189
|
it "logs matching queries" do
|
181
|
-
|
190
|
+
expect(Rack::RequestPolice.storage).to receive(:log_request)
|
182
191
|
.with('url' => "http://example.org/user?id=1", 'ip' => "127.0.0.1", 'method' => "get", 'time' => Time.now.to_i)
|
183
192
|
|
184
193
|
get '/user?id=1'
|
data/web/views/index.erb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-request_police
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafał Wojsznis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|