http-testing 0.1.4 → 0.1.5

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.
@@ -2,6 +2,30 @@ module HttpTesting
2
2
  class HttpTestingError < StandardError
3
3
  end
4
4
 
5
+ class DefaultLogFactory
6
+ def self.create_logger(name)
7
+ require 'logger' unless defined? Logger
8
+ log = Logger.new(STDOUT)
9
+ log.formatter = proc do |severity, datetime, progname, msg|
10
+ "#{datetime.strftime('%Y-%m-%d %H:%M:%S,%L')} [#{severity}] (#{name}) - #{msg}\n"
11
+ end
12
+ log
13
+ end
14
+ end
15
+
16
+ class EmptyLoggerFactory
17
+ def self.fatal(*args); end
18
+ def self.error(*args); end
19
+ def self.warn(*args); end
20
+ def self.info(*args); end
21
+ def self.debug(*args); end
22
+
23
+ def self.create_logger(*args)
24
+ self
25
+ end
26
+ end
27
+
28
+
5
29
  autoload :Context, 'http_testing/context'
6
30
  autoload :VERSION, 'http_testing/version'
7
31
  end
@@ -3,21 +3,26 @@ require 'monitor'
3
3
 
4
4
  class HttpTesting::Context
5
5
  include WEBrick
6
- # extend MonitorMixin
7
6
 
8
7
  def initialize(port, options = {})
9
8
  @options = {
10
- :wait_timeout => 3 #seconds
9
+ wait_timeout: 3, #seconds
10
+ verbose: false,
11
+ requests_dumps_path: nil, #Path to the folder to dump requests
12
+ log_factory: HttpTesting::DefaultLogFactory
11
13
  }.merge options
12
14
  @port = port
13
15
 
14
- @monitor = Monitor.new
16
+ @monitor = Monitor.new
15
17
  @completed_cond = @monitor.new_cond
16
- @started_cond = @monitor.new_cond
18
+ @started_cond = @monitor.new_cond
17
19
 
18
- @started = false
20
+ @started = false
19
21
  @completed = false
20
- @error = nil
22
+ @error = nil
23
+
24
+ logger_factory = @options[:verbose] ? @options[:log_factory] : HttpTesting::EmptyLoggerFactory
25
+ @log = logger_factory.create_logger('http-testing::context')
21
26
  end
22
27
 
23
28
  def self.start(port, options = {}, &block)
@@ -30,12 +35,37 @@ class HttpTesting::Context
30
35
  @error = nil
31
36
 
32
37
  #Starting separate thread for the server
38
+ @log.info 'Starting main worker thread...'
33
39
  @main = Thread.start do
34
- @server = HTTPServer.new(Port: @port, Logger: Log.new(nil, BasicLog::ERROR), AccessLog: [])
40
+ @log.info "Starting http server on port: #{@port}."
41
+
42
+ access_log = []
43
+ access_log << [$stdout, WEBrick::AccessLog::COMMON_LOG_FORMAT] if @options[:verbose]
44
+ @server = HTTPServer.new(
45
+ Port: @port,
46
+ Logger: Log.new(nil, BasicLog::ERROR),
47
+ AccessLog: access_log,
48
+ RequestCallback: proc do |request, response|
49
+ if @options[:requests_dumps_path]
50
+ FileUtils.mkdirs(@options[:requests_dumps_path]) unless File.exists?(@options[:requests_dumps_path])
51
+ timestamp = Time.now.strftime('%Y%m%d%H%M%S%L')
52
+ request_dump_file = File.join(@options[:requests_dumps_path], "#{timestamp}-#{request.request_method}.txt")
53
+ File.open(request_dump_file, 'w') do |f|
54
+ f.write(request.request_line)
55
+ f.write(request.raw_header.join(''))
56
+ f.write("\r\n")
57
+ f.write(request.body)
58
+ end
59
+ end
60
+ end)
61
+
35
62
  @server.mount_proc("/", nil) do |request, response|
63
+ @log.info "Connection started. Path: #{request.path}."
36
64
  begin
65
+ @log.info 'Yielding request and response...'
37
66
  yield(request, response)
38
67
  rescue
68
+ @log.error "Block raised error #{$!}"
39
69
  @error = $!
40
70
  end
41
71
 
@@ -43,6 +73,7 @@ class HttpTesting::Context
43
73
  @monitor.synchronize do
44
74
  @completed_cond.signal
45
75
  end
76
+ @log.info 'Connection completed.'
46
77
  end
47
78
  @started = true
48
79
  @monitor.synchronize do
@@ -55,13 +86,18 @@ class HttpTesting::Context
55
86
  @monitor.synchronize do
56
87
  @started_cond.wait_until { @started }
57
88
  end
89
+ @log.info 'Server started.'
58
90
  self
59
91
  end
60
92
 
61
93
  def wait
62
94
  @monitor.synchronize do
63
- @completed_cond.wait(@options[:wait_timeout]) unless @completed
95
+ unless @completed
96
+ @log.info 'Waiting for connection to complete...'
97
+ @completed_cond.wait(@options[:wait_timeout])
98
+ end
64
99
  end
100
+ @log.info "Stopping http server on port: #{@port}."
65
101
  @server.shutdown
66
102
  raise HttpTesting::HttpTestingError.new "HTTP Connection was not completed within #{@options[:wait_timeout]} seconds" unless @completed
67
103
  raise @error if @error
@@ -1,3 +1,3 @@
1
1
  module HttpTesting
2
- VERSION = "0.1.4".freeze
2
+ VERSION = "0.1.5".freeze
3
3
  end
metadata CHANGED
@@ -1,27 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Evgeny Myasishchev
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-04-29 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rspec
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: 2.0.0
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: 2.0.0
27
30
  description: HttpTesting allows testing HTTP requests.
@@ -48,26 +51,27 @@ files:
48
51
  - spec/spec_helper.rb
49
52
  homepage: http://github.com/evgeny-myasishchev/http-testing
50
53
  licenses: []
51
- metadata: {}
52
54
  post_install_message:
53
55
  rdoc_options: []
54
56
  require_paths:
55
57
  - lib
56
58
  required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
57
60
  requirements:
58
- - - '>='
61
+ - - ! '>='
59
62
  - !ruby/object:Gem::Version
60
63
  version: 1.9.3
61
64
  required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
62
66
  requirements:
63
- - - '>='
67
+ - - ! '>='
64
68
  - !ruby/object:Gem::Version
65
69
  version: '0'
66
70
  requirements: []
67
71
  rubyforge_project: http-testing
68
- rubygems_version: 2.0.3
72
+ rubygems_version: 1.8.25
69
73
  signing_key:
70
- specification_version: 4
74
+ specification_version: 3
71
75
  summary: Library for testing HTTP requests in Ruby.
72
76
  test_files:
73
77
  - spec/context_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: e2edb59d922e578fda365269e88432b5c1a48fe3
4
- data.tar.gz: 49f751a723a923f02f2f62c55a417633d3c6b3eb
5
- SHA512:
6
- metadata.gz: 97e811491183d35a657c78277d6b2c60f672bc107d702a96a6076edbab7ca50f86d14450ad1801677ae25ee1b48327eb6cd09fb858ee4a8a6ab48cdec1e161f4
7
- data.tar.gz: 471d8838ebb32daf4f029b5dfea318794d69d3a23ced5d2959570bc24f732896f9f691463ca5aa86d73ad11ef1dd7600ffaea3379c837ab24d374c581cb63bd6