rspec-httpd 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/rspec/httpd/client.rb +28 -27
- data/lib/rspec/httpd/server.rb +21 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39e7f534f32be091cf91b9a86482a7f6ad77edb2f23f900135e5c3b39f10e390
|
4
|
+
data.tar.gz: b8c93f5a2f115ed3095bd474bc9a2c63fac4a3324a7c04930b4b0b87b6e9e51e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 724693c35daa22840ff5a3871ac39fbecad5fdb22a38621e8e704b647683f28c953cdffe454a33a3b86434e6f8b2968528067e7b10c1008bac7b67bd537755f5
|
7
|
+
data.tar.gz: 28f7cf7303b11c67ebde228ec1a323f4ad88cca6891c6d46881b2fbebaeeed844f5cae2b3cd227dd1f4680e6ac85e8f7cbd36c71e86ff13c4f8dcf114d8665f7
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.15
|
data/lib/rspec/httpd/client.rb
CHANGED
@@ -1,6 +1,34 @@
|
|
1
1
|
require "net/http"
|
2
2
|
require "json"
|
3
3
|
|
4
|
+
class Net::HTTPResponse
|
5
|
+
def headers
|
6
|
+
@headers ||= HeadersHash.new(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
class HeadersHash < Hash
|
10
|
+
def initialize(response)
|
11
|
+
response.each_header do |k, v|
|
12
|
+
case self[k]
|
13
|
+
when Array then self[k].concat v
|
14
|
+
when nil then self[k] = v
|
15
|
+
else self[k].concat(v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
super key.downcase
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def []=(key, value)
|
27
|
+
super key.downcase, value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
4
32
|
module RSpec::Httpd
|
5
33
|
class Client
|
6
34
|
# host and port. Set at initialization
|
@@ -19,11 +47,6 @@ module RSpec::Httpd
|
|
19
47
|
Integer(response.code)
|
20
48
|
end
|
21
49
|
|
22
|
-
# returns the headers of the latest response
|
23
|
-
def headers
|
24
|
-
@headers ||= HeadersHash.new(response)
|
25
|
-
end
|
26
|
-
|
27
50
|
# returns the parsed response of the latest request
|
28
51
|
def result
|
29
52
|
@result ||= ResponseParser.parse(response)
|
@@ -85,28 +108,6 @@ module RSpec::Httpd
|
|
85
108
|
end
|
86
109
|
end
|
87
110
|
|
88
|
-
class HeadersHash < Hash
|
89
|
-
def initialize(response)
|
90
|
-
response.each_header do |k, v|
|
91
|
-
case self[k]
|
92
|
-
when Array then self[k].concat v
|
93
|
-
when nil then self[k] = v
|
94
|
-
else self[k].concat(v)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def [](key)
|
100
|
-
super key.downcase
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
def []=(key, value)
|
106
|
-
super key.downcase, value
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
111
|
module ResponseParser
|
111
112
|
def self.parse(response)
|
112
113
|
return nil if response.body.nil?
|
data/lib/rspec/httpd/server.rb
CHANGED
@@ -22,18 +22,36 @@ module RSpec::Httpd
|
|
22
22
|
|
23
23
|
def do_start(host, port, command)
|
24
24
|
logger = RSpec::Httpd.logger
|
25
|
+
|
26
|
+
if port_open?(host, port)
|
27
|
+
logger.error "A process is already running on #{host}:#{port}"
|
28
|
+
exit 2
|
29
|
+
end
|
30
|
+
|
25
31
|
logger.debug "Starting server: #{command}"
|
26
32
|
|
27
|
-
|
33
|
+
# start child process in a separate process group. at exit we'll
|
34
|
+
# kill the entire process group. This helps if the started process
|
35
|
+
# spawns another child again.
|
36
|
+
pid = spawn(command, pgroup: true)
|
37
|
+
pgid = Process.getpgid(pid)
|
28
38
|
|
29
39
|
at_exit do
|
30
40
|
begin
|
31
|
-
logger.debug "
|
32
|
-
Process.kill("
|
41
|
+
logger.debug "Terminating server in pgid #{pgid}: #{command}"
|
42
|
+
Process.kill("TERM", -pgid)
|
33
43
|
sleep 0.2
|
34
44
|
rescue Errno::ESRCH
|
35
45
|
end
|
36
46
|
|
47
|
+
if port_open?(host, port)
|
48
|
+
begin
|
49
|
+
logger.debug "Killing server in pgid #{pgid}: #{command}"
|
50
|
+
Process.kill("KILL", -pgid)
|
51
|
+
rescue Errno::ESRCH, Errno::EPERM
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
37
55
|
logger.warn "Cannot stop server at pid #{pid}: #{command}" if port_open?(host, port)
|
38
56
|
exit 0
|
39
57
|
end
|