http-test 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c7b8727e630d398176de4c83f39c70e90fc1182
4
- data.tar.gz: fd07f66e8928769800d0154515dd0ed8fec0fdba
3
+ metadata.gz: 7576a8a3222da33e7a0bf12deb38ff10349c6c10
4
+ data.tar.gz: 443c40c55cfbd964ed1c2042d90796429418680a
5
5
  SHA512:
6
- metadata.gz: 3617c260c1ad84634e743238c3dd370abfcfd8c22b8cf6c4c979171a976a601c31b50ae72c94917371f4578a7aef5673863777b2fd030d1119cd937da0cd2fd8
7
- data.tar.gz: 771e08efc9794c15fc106eb46e870e205b4e0f0c32caa61b54027c4263cf425ebdac4411d3112b0a5c0d9f4b03c29c916591ecc56af2e5e0302e9e5590e4c983
6
+ metadata.gz: 5ed39001ef88bc2599acf94e4bed1be3679a4179d4b631bb6c96fc93ca0a0516d4670cec6f4fba05179d5d602ad55fce94a8a124706a8d9144d616f2de334130
7
+ data.tar.gz: 0faeccf5395a928a6f4009ffe3ff91a038fdcbfd3b67d4d0a1e55e601df2ca034665bcb4aa05043b6abc63ac0a1ec0c31575b03c2ffde83d05aa69f61028a178
data/README.md CHANGED
@@ -53,8 +53,26 @@ to start the server in a non-daemonized mode.
53
53
 
54
54
  **Limitations:**
55
55
 
56
- - The command should read the PORT to listen on from the "PORT" environment value.
57
- - Currently the only supported port is 4444. Expect this to change to a randomized port number.
56
+ - test servers are always running locally. The port is determined randomly by http-test.
57
+ - The command must read the PORT environment value to determine which port to listen on.
58
+ - When being killed the command must kill all its children. For simple servers this is not a problem; for daemonizing servers or servers that start worker processes you might want to add a shell script which sets up servers correctly. This, for example, is a script which properly deals with a phoenix server:
59
+
60
+ #!/bin/bash
61
+ set -eu
62
+
63
+ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
64
+ cd $HERE
65
+
66
+ _term() {
67
+ echo "Caught SIGTERM signal!"
68
+ kill -TERM "$child" 2>/dev/null
69
+ }
70
+
71
+ trap _term SIGTERM
72
+ mix phoenix.server &
73
+
74
+ child=$!
75
+ wait "$child"
58
76
 
59
77
  ## Development
60
78
 
@@ -62,6 +80,10 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
62
80
 
63
81
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
82
 
83
+ Some of these tests talk to a remote server at http://jsonplaceholder.typicode.com. To skip these tests run rake with the following command:
84
+
85
+ SKIP_REMOTE_TESTS=1 rake
86
+
65
87
  ## Contributing
66
88
 
67
89
  Bug reports and pull requests are welcome on GitHub at https://github.com/radiospiel/http-test.
@@ -2,32 +2,68 @@ require "test-unit"
2
2
  require "forwardable"
3
3
 
4
4
  module HttpTest
5
- PORT = 4444
5
+ def self.start_session(session_parameters)
6
+ url_base, command = session_parameters.values_at :url_base, :command
7
+ @session = Session.start! url_base: url_base, command: command
8
+ end
9
+
10
+ def self.stop_session
11
+ @session = nil
12
+ end
13
+
14
+ def self.url_base
15
+ return @session.url_base if @session
6
16
 
7
- def self.url_base(url_base = nil)
8
- @url_base = url_base if url_base
9
- return @url_base if @url_base
10
17
  STDERR.puts <<-MSG
11
18
  Either define a API endpoint via url_base <url>, or define a command to start a test_server via test_server "command"'
12
19
  MSG
13
- exit 1
20
+ #STDERR.puts "called from\n\t#{caller[0,6].join("\n\t")}"
21
+ raise "Missing session definition"
14
22
  end
15
23
 
16
- def self.test_server(command)
17
- Server.start!(command)
18
- url_base "http://localhost:#{PORT}"
19
- end
24
+ # ---------------------------------------------------------------------------
20
25
 
21
26
  module TestUnitAdapter
22
- extend Forwardable
27
+ module ClassMethods
28
+ attr_accessor :session_parameters
29
+ end
30
+
31
+ def self.extended(base)
32
+ base.extend ClassMethods
33
+ end
34
+
35
+ def url_base(url_base)
36
+ class << self
37
+ def startup
38
+ HttpTest.start_session(session_parameters)
39
+ end
40
+
41
+ def shutdown
42
+ HttpTest.stop_session
43
+ end
44
+ end
45
+
46
+ self.session_parameters = { url_base: url_base }
47
+ end
48
+
49
+ def test_server(command)
50
+ class << self
51
+ def startup
52
+ HttpTest.start_session(session_parameters)
53
+ end
54
+
55
+ def shutdown
56
+ HttpTest.stop_session
57
+ end
58
+ end
23
59
 
24
- delegate url_base: HttpTest
25
- delegate test_server: HttpTest
60
+ self.session_parameters = { command: command }
61
+ end
26
62
  end
27
63
  end
28
64
 
29
65
  require_relative "http-test/http_methods"
30
- require_relative "http-test/server"
66
+ require_relative "http-test/session"
31
67
 
32
68
  class HttpTest::TestCase < Test::Unit::TestCase
33
69
  include HttpTest::HttpMethods # include HTTP helper methods, like GET, PUT etc.
@@ -1,63 +1,55 @@
1
- require "socket"
2
- require "timeout"
1
+ require_relative "server/port"
3
2
 
4
3
  module HttpTest::Server
5
4
  extend self
6
5
 
7
- PORT = HttpTest::PORT
6
+ # Kills the current server process and checks that the port becomes unavailable.
7
+ def kill!
8
+ return unless @pid
8
9
 
9
- def available!(timeout = 0.1)
10
- s = nil
11
- Timeout.timeout(timeout) do
12
- STDERR.print "."
13
- s = TCPSocket.new("127.0.0.1", PORT)
10
+ Process.kill("TERM", @pid)
11
+ Process.wait
12
+
13
+ if @port && Port.available?(@port)
14
+ STDERR.puts "Could not stop server"
15
+ exit 1
14
16
  end
15
17
 
16
- STDERR.puts "[http-test] test server became available on http://127.0.0.1:#{PORT}"
17
- ensure
18
- s&.close
18
+ @port = nil
19
+ @pid = nil
19
20
  end
20
21
 
21
- def available?(timeout = 0.1)
22
- available!(timeout)
23
- true
24
- rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error
25
- false
22
+ def url_base
23
+ "http://127.0.0.1:#{@port}"
26
24
  end
27
25
 
28
- def wait_for_port(timeout = 10)
29
- (timeout / 0.1).to_i.times do
30
- return true if available?(PORT)
31
- sleep(0.1)
32
- end
26
+ def start!(command)
27
+ return if @started == command && @port
33
28
 
34
- available!
35
- end
29
+ kill! if @started
36
30
 
37
- def started?
38
- @started ? true : false
39
- end
31
+ started_at = Time.now
40
32
 
41
- def start!(command)
42
- return if started?
33
+ @port = Port.choose
43
34
 
44
- pid = fork do
35
+ @pid = fork do
45
36
  # Signal.trap("HUP") { STDERR.puts "Exiting web server"; exit }
46
37
  # # ... do some work ...
47
38
  ENV["RACK_ENV"] = "test"
48
- ENV["PORT"] = PORT.to_s
39
+ ENV["PORT"] = @port.to_s
49
40
 
50
41
  exec command
51
42
  end
52
43
 
53
- STDERR.puts "[http-test] Trying to start test server via '#{command}', as pid: #{pid}"
44
+ Port.wait(@port)
54
45
 
55
- at_exit do
56
- Process.kill("TERM", pid)
57
- Process.wait
58
- end
46
+ secs = Time.now - started_at
47
+ STDERR.puts "\n[http-test##{@pid}] test server '#{command}' running on #{url_base} after #{'%.3f secs' % secs}"
59
48
 
60
- wait_for_port
61
- @started = true
49
+ @started = command
62
50
  end
63
51
  end
52
+
53
+ at_exit do
54
+ HttpTest::Server.kill!
55
+ end
@@ -0,0 +1,52 @@
1
+ require "socket"
2
+ require "timeout"
3
+
4
+ # helper methods for port handling ---------------------------------------
5
+ module HttpTest
6
+ module Server
7
+ module Port
8
+ extend self
9
+
10
+ # return an unused port.
11
+ def choose
12
+ server = TCPServer.new('127.0.0.1', 0)
13
+ port = server.addr[1]
14
+ ensure
15
+ # don't know if this is really necessary
16
+ server&.close
17
+ end
18
+
19
+ # is a given port available? This raises ECONNREFUSED or EHOSTUNREACH if not,
20
+ # and Timeout::Error if we don't know.
21
+ def available!(port, timeout = 0.1)
22
+ s = nil
23
+ Timeout.timeout(timeout) do
24
+ STDERR.print "."
25
+ s = TCPSocket.new("127.0.0.1", port)
26
+ end
27
+ ensure
28
+ s&.close
29
+ end
30
+
31
+ # is a given port available? This returns true if so, and false if not or unsure.
32
+ def available?(port, timeout = 0.1)
33
+ available!(port, timeout)
34
+ true
35
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error
36
+ false
37
+ end
38
+
39
+ # wait until a port becomes available. Raises either ECONNREFUSED, EHOSTUNREACH,
40
+ # or Timeout::Error if the port cannot be established.
41
+ def wait(port, timeout = 10)
42
+ (timeout / 0.1).to_i.times do
43
+ return true if available?(port)
44
+ sleep(0.1)
45
+ end
46
+
47
+ available!(port)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,32 @@
1
+ require_relative "server"
2
+
3
+ module HttpTest
4
+ # A session of the test server. Note that one might think that a session would
5
+ # holds its own test server instance; we do, however, reuse servers between
6
+ # tests in case multiple test cases use the same test server - which is quite
7
+ # likely the most common use case.
8
+ class Session
9
+ attr_reader :url_base
10
+ attr_reader :command
11
+
12
+ def initialize(url_base: nil, command: nil)
13
+ @url_base = url_base
14
+ @command = command
15
+ end
16
+
17
+ private
18
+
19
+ def start!
20
+ return unless @command
21
+
22
+ Server.start! @command
23
+ @url_base = Server.url_base
24
+ end
25
+
26
+ def self.start!(url_base: nil, command: nil)
27
+ session = Session.new url_base: url_base, command: command
28
+ session.send :start!
29
+ session
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpTest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - eno
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-17 00:00:00.000000000 Z
11
+ date: 2017-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -86,6 +86,8 @@ files:
86
86
  - lib/http-test.rb
87
87
  - lib/http-test/http_methods.rb
88
88
  - lib/http-test/server.rb
89
+ - lib/http-test/server/port.rb
90
+ - lib/http-test/session.rb
89
91
  - lib/http-test/version.rb
90
92
  homepage:
91
93
  licenses: