rack-request_police 0.0.2alpha → 0.0.3alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2a52c5c86d8e26cb59e35182aa408dbe54b2ab2
4
- data.tar.gz: 55886777b2b8bd0b6f98404fea8e1f4dd6366340
3
+ metadata.gz: d13fc52b2497aa40b442a235a51589361abfe55c
4
+ data.tar.gz: 53861595d2c9c9608c818a18f9a9d716365feca1
5
5
  SHA512:
6
- metadata.gz: 3fa3502d9c55f9fa32bb2c6b4bb8cf2e4a5f5c10decc36937dd09e8a61aa209e88cef0170e7bb981f4a356845e1cd7ab9e4eaf0bc362637024dd75a3a3c20270
7
- data.tar.gz: ad73e34f990fc84b127607d188af98fd2bb7b7277dc9039858143e30c5f4b1099d46f36da9b887af9910a421347a1af7262d16fcb1b9c1b405181df3f0d85bcc
6
+ metadata.gz: e1bee036da587a96b5f140550c61d164f8af9ca3d12e1a8776fa99e6111455c7cab8f1c5db6e22de553a85a048c037fb63179bcf9e4c1a4c5b4fe4d10a0c8749
7
+ data.tar.gz: d10d467d3aaf8579a80e99773a37236b6bd81ba6295be316ca67a018d2c3ed438e1dbc3b5d292d844a992dfae641ac33940287305c19d1e39cf6c55afd011173
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  [![Code Climate](https://codeclimate.com/github/emq/rack-request_police/badges/gpa.svg)](https://codeclimate.com/github/emq/rack-request_police)
4
4
  [![Build Status](https://travis-ci.org/emq/rack-request_police.svg)](https://travis-ci.org/emq/rack-request_police)
5
5
  [![Coverage Status](https://coveralls.io/repos/emq/rack-request_police/badge.svg)](https://coveralls.io/r/emq/rack-request_police)
6
+ [![Gem Version](https://badge.fury.io/rb/rack-request_police.svg)](http://badge.fury.io/rb/rack-request_police)
6
7
  [![Dependency Status](https://gemnasium.com/emq/rack-request_police.svg)](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['REMOTE_ADDR'],
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
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module RequestPolice
3
- VERSION = "0.0.2alpha"
3
+ VERSION = "0.0.3alpha"
4
4
  end
5
5
  end
@@ -33,6 +33,12 @@ module Rack
33
33
  def escape(text)
34
34
  ::Rack::Utils.escape_html(text)
35
35
  end
36
+
37
+ def parse_post_data(post_data)
38
+ JSON.pretty_generate(JSON.parse(post_data))
39
+ rescue JSON::ParserError
40
+ post_data
41
+ end
36
42
  end
37
43
  end
38
44
  end
@@ -22,7 +22,7 @@ describe "My Middleware", type: :request do
22
22
  }
23
23
 
24
24
  it "logs request without query params" do
25
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).not_to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).not_to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).not_to receive(:log_request)
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
- expect_any_instance_of(DummyStorage).to receive(:log_request)
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
@@ -75,7 +75,7 @@
75
75
  <tr class="details" id="details_<%= idx %>">
76
76
  <td colspan="5">
77
77
  <pre>
78
- <%= escape(log.data.inspect) %>
78
+ <%= escape(parse_post_data(log.data)) %>
79
79
  </pre>
80
80
  </td>
81
81
  </tr>
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.2alpha
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-06 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler