http_logger 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
data/http_logger.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{http_logger}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
12
- s.date = %q{2013-02-13}
12
+ s.date = %q{2013-02-15}
13
13
  s.description = %q{This gem keep an eye on every Net::HTTP library usage and dump all request and response data to the log file}
14
14
  s.email = %q{agresso@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/http_logger.rb CHANGED
@@ -75,8 +75,9 @@ class HttpLogger
75
75
  end
76
76
 
77
77
  def log_post_put_params(request)
78
- if request.is_a?(::Net::HTTP::Post) || request.is_a?(::Net::HTTP::Put)
79
- log("#{request.class.to_s.upcase} params", request.body)
78
+ body = request.body
79
+ if body && !body.empty? && (request.is_a?(::Net::HTTP::Post) || request.is_a?(::Net::HTTP::Put))
80
+ log("Request body", truncate_body(body))
80
81
  end
81
82
  end
82
83
 
@@ -91,13 +92,12 @@ class HttpLogger
91
92
  end
92
93
 
93
94
  def log_response_body(body)
94
- if body && !body.is_a?(Net::ReadAdapter)
95
- if collapse_body_limit && collapse_body_limit > 0 && body.size >= collapse_body_limit
96
- body = body[0..1000] + "\n\n<some data truncated>\n\n" + body[(body.size - 1000)..body.size]
97
- end
98
- log("Response body", body)
99
- else
95
+ if body.is_a?(Net::ReadAdapter)
100
96
  log("Response body", "<impossible to log>")
97
+ else
98
+ if body && !body.empty?
99
+ log("Response body", truncate_body(body))
100
+ end
101
101
  end
102
102
  end
103
103
 
@@ -112,6 +112,17 @@ class HttpLogger
112
112
  self.logger && (http.started? || fakeweb)
113
113
  end
114
114
 
115
+ def truncate_body(body)
116
+ if collapse_body_limit && collapse_body_limit > 0 && body && body.size >= collapse_body_limit
117
+ body_piece_size = collapse_body_limit / 2
118
+ body[0..body_piece_size] +
119
+ "\n\n<some data truncated>\n\n" +
120
+ body[(body.size - body_piece_size)..body.size]
121
+ else
122
+ body
123
+ end
124
+ end
125
+
115
126
 
116
127
  def log(message, dump)
117
128
  self.logger.debug(format_log_entry(message, dump))
@@ -15,6 +15,11 @@ describe HttpLogger do
15
15
  Net::HTTP.get_response(uri)
16
16
  end
17
17
 
18
+ let(:long_body) do
19
+ "12,Dodo case,dodo@case.com,tech@dodcase.com,single elimination\n" * 50 +
20
+ "12,Bonobos,bono@bos.com,tech@bonobos.com,double elimination\n" * 50
21
+ end
22
+
18
23
  subject do
19
24
  _context if defined?(_context)
20
25
  request
@@ -47,12 +52,23 @@ describe HttpLogger do
47
52
  end
48
53
 
49
54
  describe "post request" do
55
+ let(:body) {{:a => 'hello', :b => 1}}
50
56
  let(:request) do
51
- Net::HTTP.post_form(uri, {:a => 'hello', :b => 1})
57
+ Net::HTTP.post_form(uri, body)
52
58
  end
53
59
 
54
- it {should include("POST params")}
60
+ it {should include("Request body")}
55
61
  it {should include("a=hello&b=1")}
62
+ context "with too long body" do
63
+ let(:url) do
64
+ FakeWeb.register_uri(:post, "http://github.com", :body => long_body)
65
+ "http://github.com/"
66
+ end
67
+ it { should include("12,Dodo case,dodo@case.com,tech@dodcase.com,single elimination\n")}
68
+ it { should include("<some data truncated>") }
69
+ it { should include("12,Bonobos,bono@bos.com,tech@bonobos.com,double elimination\n")}
70
+ end
71
+
56
72
  end
57
73
  describe "put request" do
58
74
  let(:request) do
@@ -62,19 +78,15 @@ describe HttpLogger do
62
78
  http.request(request)
63
79
  end
64
80
 
81
+ it {should include("Request body")}
65
82
  it {should include("a=hello&b=1")}
66
- it {should include("PUT params")}
67
83
  end
68
84
 
69
85
  context "with long response body" do
70
86
 
71
- let(:body) do
72
- "12,Dodo case,dodo@case.com,tech@dodcase.com,single elimination\n" * 50 +
73
- "12,Bonobos,bono@bos.com,tech@bonobos.com,double elimination\n" * 50
74
- end
75
87
 
76
88
  let(:url) do
77
- FakeWeb.register_uri(:get, "http://github.com", :body => body)
89
+ FakeWeb.register_uri(:get, "http://github.com", :body => long_body)
78
90
  "http://github.com"
79
91
  end
80
92
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_logger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-02-13 00:00:00 +02:00
18
+ date: 2013-02-15 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency