request_recorder 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- request_recorder (0.1.0)
4
+ request_recorder (0.1.1)
5
5
  activerecord
6
6
  rack
7
7
 
data/Readme.md CHANGED
@@ -24,6 +24,15 @@ Usage
24
24
  - redis 'request_recorder' gets a new entry with all the logging info from rails + activerecord
25
25
  - go to redis or build a nice frontend
26
26
 
27
+ Frontend
28
+ ========
29
+
30
+ Add :frontend_auth and find out if the current user is authorized
31
+
32
+ use RequestRecorder::Middleware, :frontent_auth => lambda{|env| env.warden.user.is_admin? }
33
+
34
+ Go to /request_recorder/<key> and see the recorded log.
35
+
27
36
  Author
28
37
  ======
29
38
  [Michael Grosser](http://grosser.it)<br/>
@@ -3,6 +3,7 @@
3
3
  source :rubygems
4
4
 
5
5
  gem "appraisal"
6
+ gem "bump"
6
7
  gem "rake"
7
8
  gem "rspec", "~>2"
8
9
  gem "sqlite3"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/request_recorder
3
3
  specs:
4
- request_recorder (0.0.9)
4
+ request_recorder (0.1.0)
5
5
  activerecord
6
6
  rack
7
7
 
@@ -14,6 +14,7 @@ GEM
14
14
  appraisal (0.4.1)
15
15
  bundler
16
16
  rake
17
+ bump (0.3.5)
17
18
  diff-lcs (1.1.3)
18
19
  fakeredis (0.4.1)
19
20
  redis (~> 3.0.0)
@@ -36,6 +37,7 @@ PLATFORMS
36
37
  DEPENDENCIES
37
38
  activerecord (~> 2.3.14)
38
39
  appraisal
40
+ bump
39
41
  fakeredis
40
42
  rake
41
43
  request_recorder!
@@ -3,6 +3,7 @@
3
3
  source :rubygems
4
4
 
5
5
  gem "appraisal"
6
+ gem "bump"
6
7
  gem "rake"
7
8
  gem "rspec", "~>2"
8
9
  gem "sqlite3"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/request_recorder
3
3
  specs:
4
- request_recorder (0.0.9)
4
+ request_recorder (0.1.0)
5
5
  activerecord
6
6
  rack
7
7
 
@@ -24,6 +24,7 @@ GEM
24
24
  rake
25
25
  arel (3.0.2)
26
26
  builder (3.0.4)
27
+ bump (0.3.5)
27
28
  diff-lcs (1.1.3)
28
29
  fakeredis (0.4.1)
29
30
  redis (~> 3.0.0)
@@ -49,6 +50,7 @@ PLATFORMS
49
50
  DEPENDENCIES
50
51
  activerecord (~> 3.2.7)
51
52
  appraisal
53
+ bump
52
54
  fakeredis
53
55
  rake
54
56
  request_recorder!
@@ -0,0 +1,25 @@
1
+ module RequestRecorder
2
+ class Frontend
3
+ class << self
4
+ def render(log)
5
+ convert_console_to_html_colors(log).gsub("\n", "<br/>")
6
+ end
7
+
8
+ private
9
+
10
+ def convert_console_to_html_colors(string)
11
+ string = string.dup
12
+ {
13
+ "0" => "inherit",
14
+ "0;1" => "inherit",
15
+ "4;35;1" => "red",
16
+ "4;36;1" => "blue",
17
+ }.each do |console, html|
18
+ string.gsub!("\e[#{console}m","</span><span style='color:#{html}'>")
19
+ end
20
+
21
+ "<span>#{string}</span>"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -2,6 +2,7 @@ require "stringio"
2
2
  require "rack/request"
3
3
  require "rack/response"
4
4
  require "request_recorder/repeater"
5
+ require "request_recorder/frontend"
5
6
  require "active_record"
6
7
 
7
8
  module RequestRecorder
@@ -10,32 +11,52 @@ module RequestRecorder
10
11
  MAX_STEPS = 100
11
12
  SEPARATOR = "|"
12
13
  NEED_AUTOFLUSH = (ActiveRecord::VERSION::MAJOR == 2)
14
+ AUTH = :frontend_auth
13
15
 
14
16
  def initialize(app, options={})
15
17
  @app = app
16
18
  @store = options.fetch(:store)
19
+ @auth = options[AUTH]
17
20
  end
18
21
 
19
22
  def call(env)
20
23
  # keep this part as fast as possible, since 99.99999% of requests will not need it
21
- return @app.call(env) unless (
22
- (env["QUERY_STRING"] && env["QUERY_STRING"].include?(MARKER)) or
23
- (env["HTTP_COOKIE"] && env["HTTP_COOKIE"].include?(MARKER))
24
- )
24
+ return @app.call(env) unless "#{env["PATH_INFO"]}-#{env["QUERY_STRING"]}-#{env["HTTP_COOKIE"]}".include?(MARKER)
25
25
 
26
- result = nil
27
- log = capture_logging do
28
- result = @app.call(env)
29
- end
26
+ if env["PATH_INFO"].to_s.starts_with?("/#{MARKER}/")
27
+ key = env["PATH_INFO"].split("/")[2]
28
+ render_frontend(env, key)
29
+ else
30
+ result = nil
31
+ log = capture_logging do
32
+ result = @app.call(env)
33
+ end
30
34
 
31
- steps_left, id = read_state_from_env(env)
32
- return [500, {}, "#{MARKER} exceeded maximum value #{MAX_STEPS}"] if steps_left > MAX_STEPS
33
- id = persist_log(id, log)
34
- response_with_data_in_cookie(result, steps_left, id)
35
+ steps_left, id = read_state_from_env(env)
36
+ return [500, {}, "#{MARKER} exceeded maximum value #{MAX_STEPS}"] if steps_left > MAX_STEPS
37
+ id = persist_log(id, log)
38
+ response_with_data_in_cookie(result, steps_left, id)
39
+ end
35
40
  end
36
41
 
37
42
  private
38
43
 
44
+ def render_frontend(env, key)
45
+ if @auth
46
+ if @auth.call(env)
47
+ if log = @store.read(key)
48
+ [200, {}, Frontend.render(log)]
49
+ else
50
+ [404, {}, "Did not find a log for key #{key}"]
51
+ end
52
+ else
53
+ [401, {}, "Unauthorized"]
54
+ end
55
+ else
56
+ [500, {}, "you need to provide #{AUTH.inspect} option"]
57
+ end
58
+ end
59
+
39
60
  def persist_log(id, log)
40
61
  @store.write(id, log)
41
62
  end
@@ -1,3 +1,3 @@
1
1
  module RequestRecorder
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe RequestRecorder::Frontend do
4
+ describe "#render" do
5
+ it "renders" do
6
+ result = RequestRecorder::Frontend.render("CONTENT")
7
+ result.should include "CONTENT"
8
+ end
9
+
10
+ it "converts colors from cli to html" do
11
+ result = RequestRecorder::Frontend.render("\e[4;35;1mxxx\e[0;1m")
12
+ result.should include "<span style='color:red'>xxx</span>"
13
+ end
14
+ end
15
+ end
@@ -114,6 +114,46 @@ describe RequestRecorder do
114
114
  }.to raise_error "Oooops"
115
115
  end
116
116
 
117
+ context "frontend" do
118
+ let(:store){ RequestRecorder::RedisLogger.new(redis) }
119
+ let(:middleware){
120
+ RequestRecorder::Middleware.new(
121
+ inner_app,
122
+ :store => store,
123
+ :frontend_auth => lambda{|env| env["success"] }
124
+ )
125
+ }
126
+
127
+ before do
128
+ store.write("xxx", "yyy")
129
+ end
130
+
131
+ it "can view a log" do
132
+ status, headers, body = middleware.call("PATH_INFO" => "/request_recorder/xxx", "success" => true)
133
+ status.should == 200
134
+ body.should include("yyy")
135
+ end
136
+
137
+ it "cannot view a log if auth fails" do
138
+ status, headers, body = middleware.call("PATH_INFO" => "/request_recorder/xxx")
139
+ status.should == 401
140
+ body.should_not include("yyy")
141
+ end
142
+
143
+ it "cannot view a missing log" do
144
+ status, headers, body = middleware.call("PATH_INFO" => "/request_recorder/missing-key", "success" => true)
145
+ status.should == 404
146
+ body.should include("missing-key")
147
+ end
148
+
149
+ it "warns about unconfigured :frontend_auth" do
150
+ middleware = RequestRecorder::Middleware.new(inner_app, :store => store)
151
+ status, headers, body = middleware.call("PATH_INFO" => "/request_recorder/xxx")
152
+ status.should == 500
153
+ body.should include(":frontend_auth")
154
+ end
155
+ end
156
+
117
157
  it "integrates" do
118
158
  stored.size.should == 0
119
159
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_recorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-03 00:00:00.000000000 Z
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -61,12 +61,14 @@ files:
61
61
  - gemfiles/rails3.gemfile.lock
62
62
  - lib/request_recorder.rb
63
63
  - lib/request_recorder/cache_logger.rb
64
+ - lib/request_recorder/frontend.rb
64
65
  - lib/request_recorder/middleware.rb
65
66
  - lib/request_recorder/redis_logger.rb
66
67
  - lib/request_recorder/repeater.rb
67
68
  - lib/request_recorder/version.rb
68
69
  - request_recorder.gemspec
69
70
  - spec/request_recorder/cache_logger_spec.rb
71
+ - spec/request_recorder/frontend_spec.rb
70
72
  - spec/request_recorder/redis_logger_spec.rb
71
73
  - spec/request_recorder_spec.rb
72
74
  - spec/spec_helper.rb
@@ -85,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
87
  version: '0'
86
88
  segments:
87
89
  - 0
88
- hash: -3301936430759891302
90
+ hash: -2711650895206651119
89
91
  required_rubygems_version: !ruby/object:Gem::Requirement
90
92
  none: false
91
93
  requirements:
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  version: '0'
95
97
  segments:
96
98
  - 0
97
- hash: -3301936430759891302
99
+ hash: -2711650895206651119
98
100
  requirements: []
99
101
  rubyforge_project:
100
102
  rubygems_version: 1.8.24