log_spy 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87ce664150f376cd71aca737ccb28dc70dad3990
4
- data.tar.gz: b4f87234b6561e55e4ee6afbb3ee0faa53920d0c
3
+ metadata.gz: 34268e22c164c45eca6f47b0b0c36f884fafa44e
4
+ data.tar.gz: 60f7eb5ad9eecbf8aa043cccaf29fbd50704756b
5
5
  SHA512:
6
- metadata.gz: d8564baac75a081c8d39b01cf58c3118947543e4a348885916cafc4e3fb0b171e397e1df07e48095cfbf72768bdd21405e9661ec73efdf0d7bb0f1fbc8c7758a
7
- data.tar.gz: 70401bafdc79eb89cfb7f6590c12e06fa041be34e64b24e3334748b6cdb19c37c3b5515415b2f99ea9c6862009dc34ecec3769411f012d53c9d15bfd4ce76968
6
+ metadata.gz: a02c0a56797fb22b04571a651f5947f02f94daecb9c63569e0ee34b42b3449825c16db65c9f0b32f4b41989336cdfc9dd889d6179ee77d05238c44be5e98a163
7
+ data.tar.gz: 382af23783237d1962b0b54b0bd810694457ea956f55c7ea4e037bb02359c9864c7ad384170afb4a08bd7ccbd145ae2a5a8814fabac161c385eee9f9bfc55715
data/History.md ADDED
@@ -0,0 +1,5 @@
1
+
2
+ 0.0.3 / 2014-09-04
3
+ ==================
4
+
5
+ * accomondate the case when `Rack::Request#body` not readable, fallback to env['RAW_POST_BODY'] if any
@@ -8,8 +8,6 @@ class LogSpy::Payload
8
8
  end
9
9
 
10
10
  def to_json
11
- req_body = @req.content_type =~ /multipart/ ? '' : @req.body.read
12
-
13
11
  hash = {
14
12
  :path => @req.path,
15
13
  :status => @res.status,
@@ -20,7 +18,7 @@ class LogSpy::Payload
20
18
  :request_method => @req.request_method,
21
19
  :ip => @req.ip,
22
20
  :query_string => @req.query_string,
23
- :body => req_body
21
+ :body => request_body
24
22
  }
25
23
  }
26
24
 
@@ -30,4 +28,11 @@ class LogSpy::Payload
30
28
 
31
29
  hash.to_json
32
30
  end
31
+
32
+ def request_body
33
+ return '' if @req.content_type =~ /multipart/
34
+ @req.body.read
35
+ rescue Exception => e
36
+ @req.env['RAW_POST_BODY']
37
+ end
33
38
  end
@@ -1,3 +1,3 @@
1
1
  module LogSpy
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/payload_spec.rb CHANGED
@@ -8,7 +8,7 @@ describe LogSpy::Payload do
8
8
  let(:query_string) { 'query-key=query-val' }
9
9
  let(:status) { 200 }
10
10
  let(:duration) { 335 }
11
- let(:body) { double(:body, :read => 'the-raw-body') }
11
+ let(:body) { double(:body) }
12
12
  let(:content_type) { 'application/json' }
13
13
 
14
14
  let(:request) do
@@ -48,14 +48,12 @@ describe LogSpy::Payload do
48
48
  :content_type => content_type,
49
49
  :request_method => request_method,
50
50
  :ip => ip,
51
- :query_string => query_string,
52
- :body => body.read
51
+ :query_string => query_string
53
52
  }
54
53
  }
55
54
  end
56
55
 
57
- context "if no error" do
58
- let(:payload) { LogSpy::Payload.new request, response, begin_at }
56
+ shared_examples 'ensure_payload_formats' do
59
57
  it 'returns correct format' do
60
58
  expect(payload.to_json).to eq(expected_hash.to_json)
61
59
  end
@@ -68,17 +66,54 @@ describe LogSpy::Payload do
68
66
  end
69
67
  end
70
68
 
71
- context "if with error" do
69
+ shared_context "if_body_readable" do
70
+ before(:each) do
71
+ allow(body).to receive_messages(:read => 'the-raw-body')
72
+ expected_hash[:request][:body] = 'the-raw-body'
73
+ end
74
+ end
75
+
76
+ shared_context 'if_body_unreadable' do
77
+ before(:each) do
78
+ allow(body).to receive(:read).and_raise(Exception, 'closed stream')
79
+ allow(request).to receive_messages(:env => { 'RAW_POST_BODY' => 'raw-post-body' })
80
+ expected_hash[:request][:body] = 'raw-post-body'
81
+ end
82
+ end
83
+
84
+ context "if request ends without error" do
85
+ let(:payload) { LogSpy::Payload.new request, response, begin_at }
86
+
87
+ context "if body can be read" do
88
+ include_context "if_body_readable"
89
+ include_examples 'ensure_payload_formats'
90
+ end
91
+
92
+ context "if body can not be read" do
93
+ include_context "if_body_unreadable"
94
+ include_examples 'ensure_payload_formats'
95
+ end
96
+
97
+ end
98
+
99
+ context "if request ends with error" do
72
100
  let(:payload) { LogSpy::Payload.new request, response, begin_at, error }
73
- it 'returns error with message and backtrace' do
101
+ before :each do
74
102
  expected_hash[:error] = {
75
103
  :message => error.message,
76
104
  :backtrace => error.backtrace
77
105
  }
106
+ end
78
107
 
79
- expect(payload.to_json).to eq(expected_hash.to_json)
108
+ context "if request body can be read" do
109
+ include_context 'if_body_readable'
110
+ include_examples 'ensure_payload_formats'
111
+ end
112
+
113
+ context "if request body can not be read" do
114
+ include_context 'if_body_unreadable'
115
+ include_examples 'ensure_payload_formats'
80
116
  end
81
-
82
117
  end
83
118
  end
84
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_spy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yang-Hsing Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-03 00:00:00.000000000 Z
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -90,6 +90,7 @@ files:
90
90
  - .gitignore
91
91
  - .rspec
92
92
  - Gemfile
93
+ - History.md
93
94
  - LICENSE.txt
94
95
  - README.md
95
96
  - Rakefile
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  version: '0'
122
123
  requirements: []
123
124
  rubyforge_project:
124
- rubygems_version: 2.2.2
125
+ rubygems_version: 2.0.3
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: send rack application log to Amazon SQS