puma 2.12.0-java → 2.12.1-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

@@ -1,198 +0,0 @@
1
- require "rbconfig"
2
- require 'test/unit'
3
- require 'socket'
4
- require 'openssl'
5
-
6
- require 'puma/minissl'
7
- require 'puma/server'
8
-
9
- require 'net/https'
10
-
11
- class SSLEventsHelper < ::Puma::Events
12
- attr_accessor :addr, :cert, :error
13
-
14
- def ssl_error(server, peeraddr, peercert, error)
15
- self.addr = peeraddr
16
- self.cert = peercert
17
- self.error = error
18
- end
19
- end
20
-
21
- class TestPumaServerSSL < Test::Unit::TestCase
22
-
23
- def setup
24
- @port = 3212
25
- @host = "127.0.0.1"
26
-
27
- @app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
28
-
29
- @ctx = Puma::MiniSSL::Context.new
30
-
31
- if defined?(JRUBY_VERSION)
32
- @ctx.keystore = File.expand_path "../../examples/puma/keystore.jks", __FILE__
33
- @ctx.keystore_pass = 'blahblah'
34
- else
35
- @ctx.key = File.expand_path "../../examples/puma/puma_keypair.pem", __FILE__
36
- @ctx.cert = File.expand_path "../../examples/puma/cert_puma.pem", __FILE__
37
- end
38
-
39
- @ctx.verify_mode = Puma::MiniSSL::VERIFY_NONE
40
-
41
- @events = SSLEventsHelper.new STDOUT, STDERR
42
- @server = Puma::Server.new @app, @events
43
- @server.add_ssl_listener @host, @port, @ctx
44
- @server.run
45
-
46
- @http = Net::HTTP.new @host, @port
47
- @http.use_ssl = true
48
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
49
- end
50
-
51
- def teardown
52
- @server.stop(true)
53
- end
54
-
55
- def test_url_scheme_for_https
56
- body = nil
57
- @http.start do
58
- req = Net::HTTP::Get.new "/", {}
59
-
60
- @http.request(req) do |rep|
61
- body = rep.body
62
- end
63
- end
64
-
65
- assert_equal "https", body
66
- end
67
-
68
- def test_very_large_return
69
- giant = "x" * 2056610
70
-
71
- @server.app = proc do
72
- [200, {}, [giant]]
73
- end
74
-
75
- body = nil
76
- @http.start do
77
- req = Net::HTTP::Get.new "/"
78
- @http.request(req) do |rep|
79
- body = rep.body
80
- end
81
- end
82
-
83
- assert_equal giant.bytesize, body.bytesize
84
- end
85
-
86
- def test_form_submit
87
- body = nil
88
- @http.start do
89
- req = Net::HTTP::Post.new '/'
90
- req.set_form_data('a' => '1', 'b' => '2')
91
-
92
- @http.request(req) do |rep|
93
- body = rep.body
94
- end
95
-
96
- end
97
-
98
- assert_equal "https", body
99
- end
100
-
101
- def test_ssl_v3_rejection
102
- @http.ssl_version='SSLv3'
103
- assert_raises(OpenSSL::SSL::SSLError) do
104
- @http.start do
105
- Net::HTTP::Get.new '/'
106
- end
107
- end
108
- unless defined?(JRUBY_VERSION)
109
- assert_match("wrong version number", @events.error.message) if @events.error
110
- end
111
- end
112
-
113
- end
114
-
115
- # client-side TLS authentication tests
116
- unless defined?(JRUBY_VERSION)
117
- class TestPumaServerSSLClient < Test::Unit::TestCase
118
-
119
- def assert_ssl_client_error_match(error, subject=nil, &blk)
120
- @port = 3212
121
- @host = "127.0.0.1"
122
-
123
- @app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
124
-
125
- @ctx = Puma::MiniSSL::Context.new
126
- @ctx.key = File.expand_path "../../examples/puma/client-certs/server.key", __FILE__
127
- @ctx.cert = File.expand_path "../../examples/puma/client-certs/server.crt", __FILE__
128
- @ctx.ca = File.expand_path "../../examples/puma/client-certs/ca.crt", __FILE__
129
- @ctx.verify_mode = Puma::MiniSSL::VERIFY_PEER | Puma::MiniSSL::VERIFY_FAIL_IF_NO_PEER_CERT
130
-
131
- events = SSLEventsHelper.new STDOUT, STDERR
132
- @server = Puma::Server.new @app, events
133
- @server.add_ssl_listener @host, @port, @ctx
134
- @server.run
135
-
136
- @http = Net::HTTP.new @host, @port
137
- @http.use_ssl = true
138
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
139
-
140
- blk.call(@http)
141
-
142
- client_error = false
143
- begin
144
- @http.start do
145
- req = Net::HTTP::Get.new "/", {}
146
- @http.request(req)
147
- end
148
- rescue OpenSSL::SSL::SSLError
149
- client_error = true
150
- end
151
-
152
- sleep 0.1
153
- assert_equal !!error, client_error
154
- assert_match error, events.error.message if error
155
- assert_equal @host, events.addr if error
156
- assert_equal subject, events.cert.subject.to_s if subject
157
- ensure
158
- @server.stop(true)
159
- end
160
-
161
- def test_verify_fail_if_no_client_cert
162
- assert_ssl_client_error_match 'peer did not return a certificate' do |http|
163
- # nothing
164
- end
165
- end
166
-
167
- def test_verify_fail_if_client_unknown_ca
168
- assert_ssl_client_error_match('self signed certificate in certificate chain', '/DC=net/DC=puma/CN=ca-unknown') do |http|
169
- key = File.expand_path "../../examples/puma/client-certs/client_unknown.key", __FILE__
170
- crt = File.expand_path "../../examples/puma/client-certs/client_unknown.crt", __FILE__
171
- http.key = OpenSSL::PKey::RSA.new File.read(key)
172
- http.cert = OpenSSL::X509::Certificate.new File.read(crt)
173
- http.ca_file = File.expand_path "../../examples/puma/client-certs/unknown_ca.crt", __FILE__
174
- end
175
- end
176
-
177
- def test_verify_fail_if_client_expired_cert
178
- assert_ssl_client_error_match('certificate has expired', '/DC=net/DC=puma/CN=client-expired') do |http|
179
- key = File.expand_path "../../examples/puma/client-certs/client_expired.key", __FILE__
180
- crt = File.expand_path "../../examples/puma/client-certs/client_expired.crt", __FILE__
181
- http.key = OpenSSL::PKey::RSA.new File.read(key)
182
- http.cert = OpenSSL::X509::Certificate.new File.read(crt)
183
- http.ca_file = File.expand_path "../../examples/puma/client-certs/ca.crt", __FILE__
184
- end
185
- end
186
-
187
- def test_verify_client_cert
188
- assert_ssl_client_error_match(nil) do |http|
189
- key = File.expand_path "../../examples/puma/client-certs/client.key", __FILE__
190
- crt = File.expand_path "../../examples/puma/client-certs/client.crt", __FILE__
191
- http.key = OpenSSL::PKey::RSA.new File.read(key)
192
- http.cert = OpenSSL::X509::Certificate.new File.read(crt)
193
- http.ca_file = File.expand_path "../../examples/puma/client-certs/ca.crt", __FILE__
194
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
195
- end
196
- end
197
- end
198
- end
@@ -1,10 +0,0 @@
1
- require 'test/unit'
2
-
3
- class TestPumaUnixSocket < Test::Unit::TestCase
4
- def test_handler
5
- handler = Rack::Handler.get(:puma)
6
- assert_equal Rack::Handler::Puma, handler
7
- handler = Rack::Handler.get('Puma')
8
- assert_equal Rack::Handler::Puma, handler
9
- end
10
- end
@@ -1,140 +0,0 @@
1
- require 'test/unit'
2
- require 'puma'
3
- require 'rack/lint'
4
- require 'test/testhelp'
5
- require 'puma/commonlogger'
6
-
7
- class TestRackServer < Test::Unit::TestCase
8
-
9
- class ErrorChecker
10
- def initialize(app)
11
- @app = app
12
- @exception = nil
13
- @env = nil
14
- end
15
-
16
- attr_reader :exception, :env
17
-
18
- def call(env)
19
- begin
20
- @env = env
21
- return @app.call(env)
22
- rescue Exception => e
23
- @exception = e
24
-
25
- [
26
- 500,
27
- { "X-Exception" => e.message, "X-Exception-Class" => e.class.to_s },
28
- ["Error detected"]
29
- ]
30
- end
31
- end
32
- end
33
-
34
- class ServerLint < Rack::Lint
35
- def call(env)
36
- assert("No env given") { env }
37
- check_env env
38
-
39
- @app.call(env)
40
- end
41
- end
42
-
43
- def setup
44
- @valid_request = "GET / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
45
-
46
- @simple = lambda { |env| [200, { "X-Header" => "Works" }, ["Hello"]] }
47
- @server = Puma::Server.new @simple
48
- @server.add_tcp_listener "127.0.0.1", 9998
49
-
50
- @stopped = false
51
- end
52
-
53
- def stop
54
- @server.stop(true)
55
- @stopped = true
56
- end
57
-
58
- def teardown
59
- @server.stop(true) unless @stopped
60
- end
61
-
62
- def test_lint
63
- @checker = ErrorChecker.new ServerLint.new(@simple)
64
- @server.app = @checker
65
-
66
- @server.run
67
-
68
- hit(['http://127.0.0.1:9998/test'])
69
-
70
- stop
71
-
72
- if exc = @checker.exception
73
- raise exc
74
- end
75
- end
76
-
77
- def test_large_post_body
78
- @checker = ErrorChecker.new ServerLint.new(@simple)
79
- @server.app = @checker
80
-
81
- @server.run
82
-
83
- big = "x" * (1024 * 16)
84
-
85
- Net::HTTP.post_form URI.parse('http://127.0.0.1:9998/test'),
86
- { "big" => big }
87
-
88
- stop
89
-
90
- if exc = @checker.exception
91
- raise exc
92
- end
93
- end
94
-
95
- def test_path_info
96
- input = nil
97
- @server.app = lambda { |env| input = env; @simple.call(env) }
98
- @server.run
99
-
100
- hit(['http://127.0.0.1:9998/test/a/b/c'])
101
-
102
- stop
103
-
104
- assert_equal "/test/a/b/c", input['PATH_INFO']
105
- end
106
-
107
- def test_after_reply
108
- closed = false
109
-
110
- @server.app = lambda do |env|
111
- env['rack.after_reply'] << lambda { closed = true }
112
- @simple.call(env)
113
- end
114
-
115
- @server.run
116
-
117
- hit(['http://127.0.0.1:9998/test'])
118
-
119
- stop
120
-
121
- assert_equal true, closed
122
- end
123
-
124
- def test_common_logger
125
- log = StringIO.new
126
-
127
- logger = Rack::CommonLogger.new(@simple, log)
128
-
129
- @server.app = logger
130
-
131
- @server.run
132
-
133
- hit(['http://127.0.0.1:9998/test'])
134
-
135
- stop
136
-
137
- assert_match %r!GET /test HTTP/1\.1!, log.string
138
- end
139
-
140
- end
@@ -1,42 +0,0 @@
1
- require "rbconfig"
2
- require 'test/unit'
3
- require 'socket'
4
- require 'openssl'
5
-
6
- require 'puma/minissl'
7
- require 'puma/server'
8
-
9
- require 'net/https'
10
-
11
- class TestTCPRack < Test::Unit::TestCase
12
-
13
- def setup
14
- @port = 3212
15
- @host = "127.0.0.1"
16
-
17
- @events = Puma::Events.new STDOUT, STDERR
18
- @server = Puma::Server.new nil, @events
19
- end
20
-
21
- def teardown
22
- @server.stop(true)
23
- end
24
-
25
- def test_passes_the_socket
26
- @server.tcp_mode!
27
-
28
- body = "We sell hats for a discount!\n"
29
-
30
- @server.app = proc do |env, socket|
31
- socket << body
32
- socket.close
33
- end
34
-
35
- @server.add_tcp_listener @host, @port
36
- @server.run
37
-
38
- sock = TCPSocket.new @host, @port
39
-
40
- assert_equal body, sock.read
41
- end
42
- end
@@ -1,229 +0,0 @@
1
- require 'test/unit'
2
-
3
- require 'puma/thread_pool'
4
-
5
- class TestThreadPool < Test::Unit::TestCase
6
-
7
- def teardown
8
- @pool.shutdown if @pool
9
- end
10
-
11
- def new_pool(min, max, &block)
12
- block = proc { } unless block
13
- @pool = Puma::ThreadPool.new(min, max, &block)
14
- end
15
-
16
- def pause
17
- sleep 0.2
18
- end
19
-
20
- def test_append_spawns
21
- saw = []
22
-
23
- pool = new_pool(0, 1) do |work|
24
- saw << work
25
- end
26
-
27
- pool << 1
28
-
29
- pause
30
-
31
- assert_equal [1], saw
32
- assert_equal 1, pool.spawned
33
- end
34
-
35
- def test_converts_pool_sizes
36
- pool = new_pool('0', '1')
37
-
38
- assert_equal 0, pool.spawned
39
-
40
- pool << 1
41
-
42
- assert_equal 1, pool.spawned
43
- end
44
-
45
- def test_append_queues_on_max
46
- finish = false
47
- pool = new_pool(0, 1) { Thread.pass until finish }
48
-
49
- pool << 1
50
- pool << 2
51
- pool << 3
52
-
53
- pause
54
-
55
- assert_equal 2, pool.backlog
56
-
57
- finish = true
58
- end
59
-
60
- def test_trim
61
- pool = new_pool(0, 1)
62
-
63
- pool << 1
64
-
65
- pause
66
-
67
- assert_equal 1, pool.spawned
68
- pool.trim
69
-
70
- pause
71
- assert_equal 0, pool.spawned
72
- end
73
-
74
- def test_trim_leaves_min
75
- finish = false
76
- pool = new_pool(1, 2) { Thread.pass until finish }
77
-
78
- pool << 1
79
- pool << 2
80
-
81
- finish = true
82
-
83
- pause
84
-
85
- assert_equal 2, pool.spawned
86
- pool.trim
87
- pause
88
-
89
- assert_equal 1, pool.spawned
90
- pool.trim
91
- pause
92
-
93
- assert_equal 1, pool.spawned
94
-
95
- end
96
-
97
- def test_force_trim_doesnt_overtrim
98
- finish = false
99
- pool = new_pool(1, 2) { Thread.pass until finish }
100
-
101
- pool << 1
102
- pool << 2
103
-
104
- assert_equal 2, pool.spawned
105
- pool.trim true
106
- pool.trim true
107
-
108
- finish = true
109
-
110
- pause
111
-
112
- assert_equal 1, pool.spawned
113
- end
114
-
115
- def test_trim_is_ignored_if_no_waiting_threads
116
- finish = false
117
- pool = new_pool(1, 2) { Thread.pass until finish }
118
-
119
- pool << 1
120
- pool << 2
121
-
122
- assert_equal 2, pool.spawned
123
- pool.trim
124
- pool.trim
125
-
126
- assert_equal 0, pool.trim_requested
127
-
128
- finish = true
129
-
130
- pause
131
- end
132
-
133
- def test_autotrim
134
- finish = false
135
- pool = new_pool(1, 2) { Thread.pass until finish }
136
-
137
- pool << 1
138
- pool << 2
139
-
140
- assert_equal 2, pool.spawned
141
-
142
- finish = true
143
-
144
- pause
145
-
146
- assert_equal 2, pool.spawned
147
-
148
- pool.auto_trim! 1
149
-
150
- sleep 1
151
-
152
- pause
153
-
154
- assert_equal 1, pool.spawned
155
- end
156
-
157
- def test_cleanliness
158
- values = []
159
- n = 100
160
- mutex = Mutex.new
161
-
162
- finished = false
163
-
164
- pool = new_pool(1,1) {
165
- mutex.synchronize { values.push Thread.current[:foo] }
166
- Thread.current[:foo] = :hai
167
- Thread.pass until finished
168
- }
169
-
170
- pool.clean_thread_locals = true
171
-
172
- n.times { pool << 1 }
173
-
174
- finished = true
175
-
176
- pause
177
-
178
- assert_equal n, values.length
179
-
180
- assert_equal [], values.compact
181
- end
182
-
183
- def test_reap_only_dead_threads
184
- pool = new_pool(2,2) { Thread.current.kill }
185
-
186
- assert_equal 2, pool.spawned
187
-
188
- pool << 1
189
-
190
- pause
191
-
192
- assert_equal 2, pool.spawned
193
-
194
- pool.reap
195
-
196
- assert_equal 1, pool.spawned
197
-
198
- pool << 2
199
-
200
- pause
201
-
202
- assert_equal 1, pool.spawned
203
-
204
- pool.reap
205
-
206
- assert_equal 0, pool.spawned
207
- end
208
-
209
- def test_auto_reap_dead_threads
210
- pool = new_pool(2,2) { Thread.current.kill }
211
-
212
- assert_equal 2, pool.spawned
213
-
214
- pool << 1
215
- pool << 2
216
-
217
- pause
218
-
219
- assert_equal 2, pool.spawned
220
-
221
- pool.auto_reap! 1
222
-
223
- sleep 1
224
-
225
- pause
226
-
227
- assert_equal 0, pool.spawned
228
- end
229
- end