rack-test_server 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -19
- data/Rakefile +1 -2
- data/lib/rack/test_server/version.rb +1 -1
- data/lib/rack/test_server.rb +19 -20
- metadata +2 -5
- data/tests/test_launch_server.rb +0 -49
- data/tests/test_stop_server.rb +0 -39
- data/tests/test_version.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2791fd8d73e918ebc3f60fab74d0f00962c42f523b1b6958ec0adc02c2a7736
|
4
|
+
data.tar.gz: 1c332c69e9975660dfb7098c29e84a47be10f6225b8d42201eefbfe5b96132a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c420c29cc54f9afb3c4aea3ab866837ef0305b07ece9f96e5e8bb680e122c04d5fe119463e5a986cc2adc40e67f8403c96cb5922038b6199af810c7cf6302ba2
|
7
|
+
data.tar.gz: e23ddf59637e0d34a77adaa98621d8714b1c21d570eb29d1601de2fda4153bd815e0564ad0d209864edeedc68d98fdfea05d3151b4a352fb7f331d620e7407cb
|
data/README.md
CHANGED
@@ -1,38 +1,72 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rack/test_server`. To experiment with that code, run `bin/console` for an interactive prompt.
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/rack-test_server.svg)](https://badge.fury.io/rb/rack-test_server)
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
## Installation
|
3
|
+
# Rack::TestServer
|
8
4
|
|
9
|
-
|
5
|
+
Just launch HTTP server for testing your Rack application.
|
10
6
|
|
11
7
|
```ruby
|
12
|
-
|
8
|
+
require 'rack/test_server'
|
9
|
+
|
10
|
+
# Configuration with `rackup` compatible options.
|
11
|
+
server = Rack::TestServer.new(
|
12
|
+
app: Rails.application,
|
13
|
+
server: :puma,
|
14
|
+
Host: '127.0.0.1',
|
15
|
+
Port: 3000)
|
16
|
+
|
17
|
+
before(:suite) do
|
18
|
+
# Just launch it on a Thread
|
19
|
+
server.start_async
|
20
|
+
server.wait_for_ready
|
21
|
+
end
|
13
22
|
```
|
14
23
|
|
15
|
-
|
24
|
+
## Background
|
16
25
|
|
17
|
-
|
26
|
+
"System testing", introduced in Rails 5.1, consists of two remarkable features: [Sharing a DB connection](https://github.com/rails/rails/pull/28083) and [Capybara integration](https://github.com/rails/rails/pull/26703).
|
18
27
|
|
19
|
-
|
28
|
+
Since transactional fixtures are available by sharing a DB connection, we can see any mocked situation (ex. logged in as admin, receiving a lot of notifications, ...) from real browsers, which dramatically improved the testing experiences. Only one limitations for using this feature is that **we have to launch the HTTP server within the same process as test runnner's**.
|
20
29
|
|
21
|
-
|
30
|
+
This library is designed **just for launching Rack application in a Thread**. We can simply use [Selenium](https://rubygems.org/gems/selenium-webdriver), [Playwright](https://playwright-ruby-client.vercel.app/) or other browser automation libraries in system testing, without studying any DSL :)
|
22
31
|
|
23
|
-
## Usage
|
24
32
|
|
25
|
-
|
33
|
+
## Installation
|
26
34
|
|
27
|
-
|
35
|
+
```ruby
|
36
|
+
gem 'rack-test_server'
|
37
|
+
```
|
28
38
|
|
29
|
-
|
39
|
+
and then `bundle install`.
|
30
40
|
|
31
|
-
|
41
|
+
## Usage
|
32
42
|
|
33
|
-
|
43
|
+
If you are working with Rails application, add configuration like below in spec/support/system_testing_helper.rb:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'rack/test_server'
|
47
|
+
|
48
|
+
# Configure Rack server
|
49
|
+
#
|
50
|
+
# options for Rack::Server
|
51
|
+
# @see https://github.com/rack/rack/blob/2.2.3/lib/rack/server.rb#L173
|
52
|
+
# options for Rack::Handler::Puma
|
53
|
+
# @see https://github.com/puma/puma/blob/v5.4.0/lib/rack/handler/puma.rb#L84
|
54
|
+
server = Rack::TestServer.new(
|
55
|
+
app: Rails.application,
|
56
|
+
server: :puma,
|
57
|
+
Host: '127.0.0.1',
|
58
|
+
Port: 3000)
|
59
|
+
|
60
|
+
RSpec.configure do
|
61
|
+
# Launch Rails application.
|
62
|
+
config.before(:suite) do
|
63
|
+
server.start_async
|
64
|
+
server.wait_for_ready
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
34
68
|
|
35
|
-
|
69
|
+
If you are not Rails user, just replace `Rails.application` with your Rack application, and put the configuration file as you prefer. :)
|
36
70
|
|
37
71
|
## License
|
38
72
|
|
data/Rakefile
CHANGED
data/lib/rack/test_server.rb
CHANGED
@@ -6,16 +6,15 @@ require 'timeout'
|
|
6
6
|
require 'rack/test_server/version'
|
7
7
|
|
8
8
|
module Rack
|
9
|
-
# An utility class for launching HTTP server with Rack::Server
|
9
|
+
# An utility class for launching HTTP server with Rack::Server#start
|
10
10
|
# and waiting for the server available with checking a healthcheck endpoint with net/http.
|
11
11
|
#
|
12
12
|
# The most typical usage is:
|
13
13
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# ```
|
14
|
+
# server = Rack::TestServer.new(app: myapp, Port: 3000)
|
15
|
+
# server.start_async
|
16
|
+
# server.wait_for_ready
|
17
|
+
#
|
19
18
|
class TestServer
|
20
19
|
# @param app [Proc] Rack application to run.
|
21
20
|
#
|
@@ -32,7 +31,7 @@ module Rack
|
|
32
31
|
@port = @server.options[:Port] || @server.default_options[:Port]
|
33
32
|
end
|
34
33
|
|
35
|
-
# @
|
34
|
+
# @return [String]
|
36
35
|
def base_url
|
37
36
|
if @host == '0.0.0.0'
|
38
37
|
"http://127.0.0.1:#{@port}"
|
@@ -41,8 +40,9 @@ module Rack
|
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
44
|
-
# Start HTTP server.
|
45
|
-
#
|
43
|
+
# Start HTTP server with blocking current thread.
|
44
|
+
#
|
45
|
+
# @note This method will block the thread, and in most cases {#start_async} is suitable.
|
46
46
|
def start
|
47
47
|
@server.start do |server|
|
48
48
|
# server can be a Puma::Launcher, Webrick::Server, Thin::Server
|
@@ -52,26 +52,25 @@ module Rack
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
# Start HTTP server.
|
56
|
-
#
|
55
|
+
# Start HTTP server, typically used together with {#wait_for_ready} method.
|
56
|
+
#
|
57
|
+
# server = Rack::TestServer.new(app: myapp)
|
58
|
+
# server.start_async
|
59
|
+
# server.wait_for_ready
|
57
60
|
#
|
58
|
-
# ```
|
59
|
-
# server = Rack::TestServer.new(app: myapp)
|
60
|
-
# server.start_async
|
61
|
-
# server.wait_for_ready
|
62
|
-
# ```
|
63
61
|
def start_async
|
64
62
|
Thread.new { start }
|
65
63
|
end
|
66
64
|
|
67
|
-
# Stop HTTP server
|
68
|
-
#
|
69
|
-
#
|
65
|
+
# Stop HTTP server.
|
66
|
+
#
|
67
|
+
# @note This method doesn't wait for the shutdown process,
|
68
|
+
# and use {#wait_for_stopped} to ensure the server is actually stopped.
|
70
69
|
def stop_async
|
71
70
|
Thread.new { @stop_proc.call }
|
72
71
|
end
|
73
72
|
|
74
|
-
# @
|
73
|
+
# @return [Boolean]
|
75
74
|
#
|
76
75
|
# Check if HTTP server actually responds.
|
77
76
|
def ready?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-test_server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YusukeIwaki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -126,9 +126,6 @@ files:
|
|
126
126
|
- lib/rack/test_server.rb
|
127
127
|
- lib/rack/test_server/version.rb
|
128
128
|
- rack-test_server.gemspec
|
129
|
-
- tests/test_launch_server.rb
|
130
|
-
- tests/test_stop_server.rb
|
131
|
-
- tests/test_version.rb
|
132
129
|
homepage: https://github.com/YusukeIwaki/rack-test_server
|
133
130
|
licenses:
|
134
131
|
- MIT
|
data/tests/test_launch_server.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require 'rack/test_server'
|
5
|
-
require 'sinatra/base'
|
6
|
-
|
7
|
-
class TestLaunchServer < Minitest::Test
|
8
|
-
class MyApp < Sinatra::Base
|
9
|
-
get('/') { '<h1>It works!</h1>' }
|
10
|
-
end
|
11
|
-
|
12
|
-
def _test(server)
|
13
|
-
server.start_async
|
14
|
-
server.wait_for_ready
|
15
|
-
|
16
|
-
html = Net::HTTP.get(URI("#{server.base_url}/"))
|
17
|
-
assert_equal '<h1>It works!</h1>', html
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_launch_without_no_options
|
21
|
-
server = Rack::TestServer.new(app: MyApp)
|
22
|
-
_test(server)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_launch_with_port
|
26
|
-
server = Rack::TestServer.new(app: MyApp, Port: 8001)
|
27
|
-
_test(server)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_launch_with_host_and_port
|
31
|
-
server = Rack::TestServer.new(app: MyApp, Host: '127.0.0.1', Port: 8002)
|
32
|
-
_test(server)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_launch_webrick
|
36
|
-
server = Rack::TestServer.new(app: MyApp, Port: 8003, server: :webrick)
|
37
|
-
_test(server)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_launch_puma
|
41
|
-
server = Rack::TestServer.new(app: MyApp, Port: 8004, server: :puma)
|
42
|
-
_test(server)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_launch_thin
|
46
|
-
server = Rack::TestServer.new(app: MyApp, Port: 8005, server: :thin)
|
47
|
-
_test(server)
|
48
|
-
end
|
49
|
-
end
|
data/tests/test_stop_server.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require 'rack/test_server'
|
5
|
-
|
6
|
-
class TestStopServer < Minitest::Test
|
7
|
-
APP = ->(_env) { [200, {}, 'OK'] }
|
8
|
-
|
9
|
-
def _test(server)
|
10
|
-
assert !server.ready?
|
11
|
-
|
12
|
-
server.start_async
|
13
|
-
server.wait_for_ready
|
14
|
-
assert server.ready?
|
15
|
-
|
16
|
-
server.stop_async
|
17
|
-
server.wait_for_stopped
|
18
|
-
assert !server.ready?
|
19
|
-
|
20
|
-
server.start_async
|
21
|
-
server.wait_for_ready
|
22
|
-
assert server.ready?
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_stop_puma
|
26
|
-
server = Rack::TestServer.new(app: APP, server: :puma, Port: 8081)
|
27
|
-
_test(server)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_stop_webrick
|
31
|
-
server = Rack::TestServer.new(app: APP, server: :webrick, Port: 8082)
|
32
|
-
_test(server)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_stop_thin
|
36
|
-
server = Rack::TestServer.new(app: APP, server: :thin, Port: 8083)
|
37
|
-
_test(server)
|
38
|
-
end
|
39
|
-
end
|
data/tests/test_version.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require 'rack/test_server/version'
|
5
|
-
|
6
|
-
class TestVersion < Minitest::Test
|
7
|
-
def test_version_present
|
8
|
-
version = Gem::Version.new(Rack::TestServer::VERSION)
|
9
|
-
assert version >= Gem::Version.new('0.0.1')
|
10
|
-
end
|
11
|
-
end
|