http-testing 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/http_testing.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  module HttpTesting
2
+ class HttpTestingError < StandardError
3
+ end
4
+
2
5
  autoload :Context, 'http_testing/context'
3
6
  autoload :VERSION, 'http_testing/version'
4
7
  end
@@ -3,23 +3,27 @@ require 'monitor'
3
3
 
4
4
  class HttpTesting::Context
5
5
  include WEBrick
6
+ # extend MonitorMixin
6
7
 
7
- WAIT_TIMEOUT = 0 #Works with no timeout :)
8
-
9
- def initialize(port)
8
+ def initialize(port, options = {})
9
+ @options = {
10
+ :wait_timeout => 3 #seconds
11
+ }.merge options
10
12
  @port = port
11
13
 
12
- @monitor = Monitor.new
14
+ @monitor = Monitor.new
13
15
  @completed_cond = @monitor.new_cond
14
- @started_cond = @monitor.new_cond
16
+ @started_cond = @monitor.new_cond
15
17
 
18
+ @started = false
16
19
  @completed = false
17
- @error = nil
20
+ @error = nil
18
21
  end
19
22
 
20
23
  def start(&block)
24
+ @started = false
21
25
  @completed = false
22
- @error = nil
26
+ @error = nil
23
27
 
24
28
  #Starting separate thread for the server
25
29
  @main = Thread.start do
@@ -36,24 +40,25 @@ class HttpTesting::Context
36
40
  @completed_cond.signal
37
41
  end
38
42
  end
39
-
43
+ @started = true
44
+ @monitor.synchronize do
45
+ @started_cond.signal
46
+ end
40
47
  @server.start
41
48
  end
42
49
 
43
50
  #Waiting for server to start
44
51
  @monitor.synchronize do
45
- @started_cond.wait(WAIT_TIMEOUT)
52
+ @started_cond.wait_until { @started }
46
53
  end
47
54
  end
48
55
 
49
56
  def wait
50
57
  @monitor.synchronize do
51
- @completed_cond.wait(WAIT_TIMEOUT)
58
+ @completed_cond.wait(@options[:wait_timeout]) unless @completed
52
59
  end
53
-
54
60
  @server.shutdown
55
-
56
- raise "HTTP Connection was not completed within #{WAIT_TIMEOUT} seconds" unless @completed
61
+ raise HttpTesting::HttpTestingError.new "HTTP Connection was not completed within #{@options[:wait_timeout]} seconds" unless @completed
57
62
  raise @error if @error
58
63
  end
59
- end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpTesting
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpTesting::Context do
4
+ def context(options = {})
5
+ HttpTesting::Context.new(30013, options)
6
+ end
7
+
8
+ it "should start web server at specified port" do
9
+ started = false
10
+ c = context
11
+ c.start do |request, response|
12
+ started = true
13
+ end
14
+ Net::HTTP.get(URI.parse('http://localhost:30013/'))
15
+ c.wait
16
+ started.should be_true
17
+ end
18
+
19
+ describe "wait" do
20
+ it "should raise exception if no request received withing wait_timeout" do
21
+ c = context(:wait_timeout => 0)
22
+ c.start do |request, response| end
23
+ lambda { c.wait }.should raise_error(HttpTesting::HttpTestingError)
24
+ end
25
+
26
+ it "should reraise exception that was raised in start block" do
27
+ c = context(:wait_timeout => 0)
28
+ c.start do |request, response|
29
+ raise "Error in start block"
30
+ end
31
+ Net::HTTP.get(URI.parse('http://localhost:30013/'))
32
+ lambda { c.wait }.should raise_error(RuntimeError)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'rspec'
4
+ require 'http_testing'
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Evgeny Myasishchev
@@ -49,6 +49,8 @@ files:
49
49
  - lib/http_testing.rb
50
50
  - lib/http_testing/context.rb
51
51
  - lib/http_testing/version.rb
52
+ - spec/context_spec.rb
53
+ - spec/spec_helper.rb
52
54
  has_rdoc: true
53
55
  homepage: http://github.com/evgeny-myasishchev/http-testing
54
56
  licenses: []
@@ -79,5 +81,6 @@ rubygems_version: 1.3.6
79
81
  signing_key:
80
82
  specification_version: 3
81
83
  summary: Library for testing HTTP requests in Ruby.
82
- test_files: []
83
-
84
+ test_files:
85
+ - spec/context_spec.rb
86
+ - spec/spec_helper.rb