http-testing 0.1.1 → 0.1.2
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.
- data/README.rdoc +73 -1
- data/lib/http_testing/context.rb +5 -0
- data/lib/http_testing/samples/rspec_sample.rb +49 -0
- data/lib/http_testing/version.rb +1 -1
- data/spec/context_spec.rb +5 -8
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -2,4 +2,76 @@
|
|
2
2
|
|
3
3
|
Library for testing HTTP requests in Ruby.
|
4
4
|
|
5
|
-
|
5
|
+
= Install
|
6
|
+
|
7
|
+
gem install http-testing
|
8
|
+
|
9
|
+
= General usage pattern
|
10
|
+
#Start HttpTesting context
|
11
|
+
c = HttpTesting::Context.start(30013) do |request, response|
|
12
|
+
# request - instance of WEBrick::HTTPRequest
|
13
|
+
# response - instance of WEBrick::HTTPResponse
|
14
|
+
|
15
|
+
#Assert request properties
|
16
|
+
|
17
|
+
#Set response
|
18
|
+
end
|
19
|
+
|
20
|
+
#Your code that will send request goes here
|
21
|
+
|
22
|
+
#Wait for request to complete
|
23
|
+
c.wait
|
24
|
+
|
25
|
+
#Check the result of request
|
26
|
+
|
27
|
+
= RSpec sample
|
28
|
+
|
29
|
+
require 'spec_helper'
|
30
|
+
|
31
|
+
describe "HTTP requests sender" do
|
32
|
+
it "should get '/say-hello'" do
|
33
|
+
#Start HttpTesting context on port 30013
|
34
|
+
c = HttpTesting::Context.start(30013) do |request, response|
|
35
|
+
# request - instance of WEBrick::HTTPRequest
|
36
|
+
# response - instance of WEBrick::HTTPResponse
|
37
|
+
|
38
|
+
#Check method and path
|
39
|
+
request.request_method.should eql "GET"
|
40
|
+
request.path.should eql '/say-hello'
|
41
|
+
|
42
|
+
#Set response
|
43
|
+
response.body = "Hello World"
|
44
|
+
end
|
45
|
+
|
46
|
+
#Send get request
|
47
|
+
result = Net::HTTP.get(URI.parse('http://localhost:30013/say-hello'))
|
48
|
+
|
49
|
+
#Wait for request to complete
|
50
|
+
c.wait
|
51
|
+
|
52
|
+
#Check the result
|
53
|
+
result.should eql "Hello World"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should post '/hello-there'" do
|
57
|
+
#Starting context
|
58
|
+
c = HttpTesting::Context.start(30013) do |request, response|
|
59
|
+
#Checking method, path and body of request
|
60
|
+
request.request_method.should eql "POST"
|
61
|
+
request.path.should eql '/hello-there'
|
62
|
+
request.body.should eql "from=Mike"
|
63
|
+
|
64
|
+
#Set response
|
65
|
+
response.body = "Hello"
|
66
|
+
end
|
67
|
+
|
68
|
+
#Send post request
|
69
|
+
response = Net::HTTP.post_form(URI.parse('http://localhost:30013/hello-there'), :from => "Mike")
|
70
|
+
|
71
|
+
#Wait for request to complete
|
72
|
+
c.wait
|
73
|
+
|
74
|
+
#Check the result
|
75
|
+
response.body.should eql "Hello"
|
76
|
+
end
|
77
|
+
end
|
data/lib/http_testing/context.rb
CHANGED
@@ -20,6 +20,10 @@ class HttpTesting::Context
|
|
20
20
|
@error = nil
|
21
21
|
end
|
22
22
|
|
23
|
+
def self.start(port, options = {}, &block)
|
24
|
+
new(port, options).start(&block)
|
25
|
+
end
|
26
|
+
|
23
27
|
def start(&block)
|
24
28
|
@started = false
|
25
29
|
@completed = false
|
@@ -51,6 +55,7 @@ class HttpTesting::Context
|
|
51
55
|
@monitor.synchronize do
|
52
56
|
@started_cond.wait_until { @started }
|
53
57
|
end
|
58
|
+
self
|
54
59
|
end
|
55
60
|
|
56
61
|
def wait
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "HTTP requests sender" do
|
4
|
+
it "should get '/say-hello'" do
|
5
|
+
#Start HttpTesting context on port 30013
|
6
|
+
c = HttpTesting::Context.start(30013) do |request, response|
|
7
|
+
# request - instance of WEBrick::HTTPRequest
|
8
|
+
# response - instance of WEBrick::HTTPResponse
|
9
|
+
|
10
|
+
#Check method and path
|
11
|
+
request.request_method.should eql "GET"
|
12
|
+
request.path.should eql '/say-hello'
|
13
|
+
|
14
|
+
#Set response
|
15
|
+
response.body = "Hello World"
|
16
|
+
end
|
17
|
+
|
18
|
+
#Send get request
|
19
|
+
result = Net::HTTP.get(URI.parse('http://localhost:30013/say-hello'))
|
20
|
+
|
21
|
+
#Wait for request to complete
|
22
|
+
c.wait
|
23
|
+
|
24
|
+
#Check the result
|
25
|
+
result.should eql "Hello World"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should post '/hello-there'" do
|
29
|
+
#Starting context
|
30
|
+
c = HttpTesting::Context.start(30013) do |request, response|
|
31
|
+
#Checking method, path and body of request
|
32
|
+
request.request_method.should eql "POST"
|
33
|
+
request.path.should eql '/hello-there'
|
34
|
+
request.body.should eql "from=Mike"
|
35
|
+
|
36
|
+
#Setting response
|
37
|
+
response.body = "Hello"
|
38
|
+
end
|
39
|
+
|
40
|
+
#Send post request
|
41
|
+
response = Net::HTTP.post_form(URI.parse('http://localhost:30013/hello-there'), :from => "Mike")
|
42
|
+
|
43
|
+
#Wait for request to complete
|
44
|
+
c.wait
|
45
|
+
|
46
|
+
#Check the result
|
47
|
+
response.body.should eql "Hello"
|
48
|
+
end
|
49
|
+
end
|
data/lib/http_testing/version.rb
CHANGED
data/spec/context_spec.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe HttpTesting::Context do
|
4
|
-
def context(options = {})
|
5
|
-
HttpTesting::Context.
|
4
|
+
def context(options = {}, &block)
|
5
|
+
HttpTesting::Context.start(30013, options, &block)
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should start web server at specified port" do
|
9
9
|
started = false
|
10
|
-
c = context
|
11
|
-
c.start do |request, response|
|
10
|
+
c = context do |request, response|
|
12
11
|
started = true
|
13
12
|
end
|
14
13
|
Net::HTTP.get(URI.parse('http://localhost:30013/'))
|
@@ -18,14 +17,12 @@ describe HttpTesting::Context do
|
|
18
17
|
|
19
18
|
describe "wait" do
|
20
19
|
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
|
20
|
+
c = context(:wait_timeout => 0) do |request, response| end
|
23
21
|
lambda { c.wait }.should raise_error(HttpTesting::HttpTestingError)
|
24
22
|
end
|
25
23
|
|
26
24
|
it "should reraise exception that was raised in start block" do
|
27
|
-
c = context(:wait_timeout => 0)
|
28
|
-
c.start do |request, response|
|
25
|
+
c = context(:wait_timeout => 0) do |request, response|
|
29
26
|
raise "Error in start block"
|
30
27
|
end
|
31
28
|
Net::HTTP.get(URI.parse('http://localhost:30013/'))
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Evgeny Myasishchev
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- http-testing.gemspec
|
49
49
|
- lib/http_testing.rb
|
50
50
|
- lib/http_testing/context.rb
|
51
|
+
- lib/http_testing/samples/rspec_sample.rb
|
51
52
|
- lib/http_testing/version.rb
|
52
53
|
- spec/context_spec.rb
|
53
54
|
- spec/spec_helper.rb
|