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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb7b0f9e10d19dc895ee1c4bbe8956a1915db3a608f4ecf9cc9e40f04353ba09
4
- data.tar.gz: 4009b6a64fd4254ee6e5beb89f12932a734785eee7b4d6e550eaea56e101252e
3
+ metadata.gz: f2791fd8d73e918ebc3f60fab74d0f00962c42f523b1b6958ec0adc02c2a7736
4
+ data.tar.gz: 1c332c69e9975660dfb7098c29e84a47be10f6225b8d42201eefbfe5b96132a4
5
5
  SHA512:
6
- metadata.gz: 65a25cfe539f001167decc4a20875dec33ba6c7e62bc7ee4e0feae500ce116bbcf9e72d347a5f97af3f9aeb3688e957250642feb6846ea3015f8738e20e95088
7
- data.tar.gz: b6a41a945783e53fb00e4799350fc492d7137148e88ca07c6484fb66fccf5cc41019a4df88e0b0d1997a125e5ae501d78fca5050af0a40083154075ae5982c2a
6
+ metadata.gz: c420c29cc54f9afb3c4aea3ab866837ef0305b07ece9f96e5e8bb680e122c04d5fe119463e5a986cc2adc40e67f8403c96cb5922038b6199af810c7cf6302ba2
7
+ data.tar.gz: e23ddf59637e0d34a77adaa98621d8714b1c21d570eb29d1601de2fda4153bd815e0564ad0d209864edeedc68d98fdfea05d3151b4a352fb7f331d620e7407cb
data/README.md CHANGED
@@ -1,38 +1,72 @@
1
- # Rack::TestServer
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
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
3
+ # Rack::TestServer
8
4
 
9
- Add this line to your application's Gemfile:
5
+ Just launch HTTP server for testing your Rack application.
10
6
 
11
7
  ```ruby
12
- gem 'rack-test_server'
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
- And then execute:
24
+ ## Background
16
25
 
17
- $ bundle
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
- Or install it yourself as:
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
- $ gem install rack-test_server
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
- TODO: Write usage instructions here
33
+ ## Installation
26
34
 
27
- ## Development
35
+ ```ruby
36
+ gem 'rack-test_server'
37
+ ```
28
38
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+ and then `bundle install`.
30
40
 
31
- 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).
41
+ ## Usage
32
42
 
33
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-test_server. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
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
@@ -4,7 +4,6 @@ require 'bundler/gem_tasks'
4
4
  require 'rake/testtask'
5
5
 
6
6
  Rake::TestTask.new(:test) do |t|
7
- t.libs << 'test'
8
7
  t.libs << 'lib'
9
- t.test_files = FileList['tests/**/test_*.rb']
8
+ t.test_files = FileList['test/**/test_*.rb']
10
9
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rack
4
4
  class TestServer
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  end
@@ -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.start
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
- # server = Rack::TestServer.new(app: myapp, Port: 3000)
16
- # server.start_async
17
- # server.wait_for_ready
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
- # @returns [String]
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
- # Note that this method will block the thread, and in most cases #start_async is suitable.
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
- # This method is typically used together with `#wait_for_ready` method.
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
- # This method doesn't always wait for the shutdown process,
69
- # and use #wait_for_stopped to ensure the server is actually stopped.
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
- # @returns [true|false]
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.0
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 00:00:00.000000000 Z
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
@@ -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
@@ -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
@@ -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