alovak-network 1.0.0 → 1.1.0

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.
@@ -4,7 +4,8 @@ module Network
4
4
 
5
5
  attr_accessor :read_timeout, :open_timeout, :headers,
6
6
  :verify_peer, :ca_file,
7
- :debugger_stream
7
+ :debugger_stream,
8
+ :logger, :request_filter, :response_filter, :sender
8
9
 
9
10
  READ_TIMEOUT = 60
10
11
  OPEN_TIMEOUT = 30
@@ -22,6 +23,7 @@ module Network
22
23
 
23
24
  def post(data)
24
25
  begin
26
+ log_request(data)
25
27
  http.post(uri.path, data, post_headers(data))
26
28
  rescue Timeout::Error, Errno::ETIMEDOUT, Timeout::ExitException
27
29
  raise Error, "The connection to the remote server is timed out"
@@ -91,5 +93,22 @@ module Network
91
93
  def cert
92
94
  OpenSSL::X509::Certificate.new(pem)
93
95
  end
96
+
97
+ def log(message)
98
+ logger.info(message) if logger
99
+ end
100
+
101
+ def log_request(data)
102
+ log sender if sender
103
+ log "POST #{uri}"
104
+ log "--->"
105
+ log (request_filter ? request_filter.call(data) : data)
106
+ end
107
+
108
+ def log_response(data)
109
+ log "<---"
110
+ log (response_filter ? response_filter.call(data) : data)
111
+ log "----"
112
+ end
94
113
  end
95
114
  end
@@ -1,5 +1,57 @@
1
1
  require 'test_helper'
2
2
 
3
+ class TestLogger
4
+ attr_reader :log
5
+
6
+ def method_missing(method, *args)
7
+ @log ||= ""
8
+ @log << "#{args.first}\n"
9
+ end
10
+ end
11
+
12
+ class TestConnectionLogging < Test::Unit::TestCase
13
+ def setup
14
+ @connection = Network::Connection.new("http://example.com/path")
15
+ @connection.logger = TestLogger.new
16
+ end
17
+
18
+ def test_log_request
19
+ @connection.send(:log_request, "request data")
20
+ log = @connection.logger.log
21
+
22
+ assert_match /POST http:\/\/example.com\/path/, log
23
+ assert_match /--->/, log
24
+ assert_match /request data/, log
25
+ end
26
+
27
+ def test_log_request_with_sender
28
+ @connection.sender = "ModuleName"
29
+ @connection.send(:log_request, "request data")
30
+ assert_match /ModuleName/, @connection.logger.log
31
+ end
32
+
33
+ def test_log_response
34
+ @connection.send(:log_response, "response data")
35
+ log = @connection.logger.log
36
+
37
+ assert_match /<---/, log
38
+ assert_match /response data/, log
39
+ assert_match /----/, log
40
+ end
41
+
42
+ def test_request_filtering
43
+ @connection.request_filter = Proc.new {|req| "#{req} is filtered"}
44
+ @connection.send(:log_request, "request")
45
+ assert_match /request is filtered/, @connection.logger.log
46
+ end
47
+
48
+ def test_response_filtering
49
+ @connection.response_filter = Proc.new {|req| "#{req} is filtered"}
50
+ @connection.send(:log_response, "response")
51
+ assert_match /response is filtered/, @connection.logger.log
52
+ end
53
+ end
54
+
3
55
  class TestConnection < Test::Unit::TestCase
4
56
  def setup
5
57
  @connection = Network::Connection.new("http://example.com/path")
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "test/unit"
2
+ require "rubygems"
2
3
  require "mocha"
3
4
  require 'turn'
4
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alovak-network
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Gabriel
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.6.0
34
34
  version:
35
- description: HTTP/HTTPS communication module based on ruby net/http, net/https modules
35
+ description: HTTP/HTTPS communication module with logging, filtering and easy SSL configuration
36
36
  email: alovak@gmail.com
37
37
  executables: []
38
38