test_ruby_application.py (1467:195fe0a92670) test_ruby_application.py (1477:b93d1acf81bd)
1import unittest
1import unittest
2
2from unit.applications.lang.ruby import TestApplicationRuby
3
4
5class TestRubyApplication(TestApplicationRuby):
6 prerequisites = {'modules': {'ruby': 'all'}}
7
8 def test_ruby_application(self):
9 self.load('variables')
10
11 body = 'Test body string.'
12
13 resp = self.post(
14 headers={
15 'Host': 'localhost',
16 'Content-Type': 'text/html',
17 'Custom-Header': 'blah',
18 'Connection': 'close',
19 },
20 body=body,
21 )
22
23 self.assertEqual(resp['status'], 200, 'status')
24 headers = resp['headers']
25 header_server = headers.pop('Server')
26 self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
27 self.assertEqual(
28 headers.pop('Server-Software'),
29 header_server,
30 'server software header',
31 )
32
33 date = headers.pop('Date')
34 self.assertEqual(date[-4:], ' GMT', 'date header timezone')
35 self.assertLess(
36 abs(self.date_to_sec_epoch(date) - self.sec_epoch()),
37 5,
38 'date header',
39 )
40
41 self.assertDictEqual(
42 headers,
43 {
44 'Connection': 'close',
45 'Content-Length': str(len(body)),
46 'Content-Type': 'text/html',
47 'Request-Method': 'POST',
48 'Request-Uri': '/',
49 'Http-Host': 'localhost',
50 'Server-Protocol': 'HTTP/1.1',
51 'Custom-Header': 'blah',
52 'Rack-Version': '13',
53 'Rack-Url-Scheme': 'http',
54 'Rack-Multithread': 'false',
55 'Rack-Multiprocess': 'true',
56 'Rack-Run-Once': 'false',
57 'Rack-Hijack-Q': 'false',
58 'Rack-Hijack': '',
59 'Rack-Hijack-IO': '',
60 },
61 'headers',
62 )
63 self.assertEqual(resp['body'], body, 'body')
64
65 def test_ruby_application_query_string(self):
66 self.load('query_string')
67
68 resp = self.get(url='/?var1=val1&var2=val2')
69
70 self.assertEqual(
71 resp['headers']['Query-String'],
72 'var1=val1&var2=val2',
73 'Query-String header',
74 )
75
76 def test_ruby_application_query_string_empty(self):
77 self.load('query_string')
78
79 resp = self.get(url='/?')
80
81 self.assertEqual(resp['status'], 200, 'query string empty status')
82 self.assertEqual(
83 resp['headers']['Query-String'], '', 'query string empty'
84 )
85
86 def test_ruby_application_query_string_absent(self):
87 self.load('query_string')
88
89 resp = self.get()
90
91 self.assertEqual(resp['status'], 200, 'query string absent status')
92 self.assertEqual(
93 resp['headers']['Query-String'], '', 'query string absent'
94 )
95
96 @unittest.skip('not yet')
97 def test_ruby_application_server_port(self):
98 self.load('server_port')
99
100 self.assertEqual(
101 self.get()['headers']['Server-Port'], '7080', 'Server-Port header'
102 )
103
104 def test_ruby_application_status_int(self):
105 self.load('status_int')
106
107 self.assertEqual(self.get()['status'], 200, 'status int')
108
109 def test_ruby_application_input_read_empty(self):
110 self.load('input_read_empty')
111
112 self.assertEqual(self.get()['body'], '', 'read empty')
113
114 def test_ruby_application_input_read_parts(self):
115 self.load('input_read_parts')
116
117 self.assertEqual(
118 self.post(body='0123456789')['body'],
119 '012345678',
120 'input read parts',
121 )
122
123 def test_ruby_application_input_read_buffer(self):
124 self.load('input_read_buffer')
125
126 self.assertEqual(
127 self.post(body='0123456789')['body'],
128 '0123456789',
129 'input read buffer',
130 )
131
132 def test_ruby_application_input_read_buffer_not_empty(self):
133 self.load('input_read_buffer_not_empty')
134
135 self.assertEqual(
136 self.post(body='0123456789')['body'],
137 '0123456789',
138 'input read buffer not empty',
139 )
140
141 def test_ruby_application_input_gets(self):
142 self.load('input_gets')
143
144 body = '0123456789'
145
146 self.assertEqual(self.post(body=body)['body'], body, 'input gets')
147
148 def test_ruby_application_input_gets_2(self):
149 self.load('input_gets')
150
151 self.assertEqual(
152 self.post(body='01234\n56789\n')['body'], '01234\n', 'input gets 2'
153 )
154
155 def test_ruby_application_input_gets_all(self):
156 self.load('input_gets_all')
157
158 body = '\n01234\n56789\n\n'
159
160 self.assertEqual(self.post(body=body)['body'], body, 'input gets all')
161
162 def test_ruby_application_input_each(self):
163 self.load('input_each')
164
165 body = '\n01234\n56789\n\n'
166
167 self.assertEqual(self.post(body=body)['body'], body, 'input each')
168
169 @unittest.skip('not yet')
170 def test_ruby_application_input_rewind(self):
171 self.load('input_rewind')
172
173 body = '0123456789'
174
175 self.assertEqual(self.post(body=body)['body'], body, 'input rewind')
176
177 @unittest.skip('not yet')
178 def test_ruby_application_syntax_error(self):
179 self.skip_alerts.extend(
180 [
181 r'Failed to parse rack script',
182 r'syntax error',
183 r'new_from_string',
184 r'parse_file',
185 ]
186 )
187 self.load('syntax_error')
188
189 self.assertEqual(self.get()['status'], 500, 'syntax error')
190
191 def test_ruby_application_errors_puts(self):
192 self.load('errors_puts')
193
194 self.get()
195
196 self.stop()
197
198 self.assertIsNotNone(
199 self.wait_for_record(r'\[error\].+Error in application'),
200 'errors puts',
201 )
202
203 def test_ruby_application_errors_puts_int(self):
204 self.load('errors_puts_int')
205
206 self.get()
207
208 self.stop()
209
210 self.assertIsNotNone(
211 self.wait_for_record(r'\[error\].+1234567890'), 'errors puts int'
212 )
213
214 def test_ruby_application_errors_write(self):
215 self.load('errors_write')
216
217 self.get()
218
219 self.stop()
220
221 self.assertIsNotNone(
222 self.wait_for_record(r'\[error\].+Error in application'),
223 'errors write',
224 )
225
226 def test_ruby_application_errors_write_to_s_custom(self):
227 self.load('errors_write_to_s_custom')
228
229 self.assertEqual(self.get()['status'], 200, 'errors write to_s custom')
230
231 def test_ruby_application_errors_write_int(self):
232 self.load('errors_write_int')
233
234 self.get()
235
236 self.stop()
237
238 self.assertIsNotNone(
239 self.wait_for_record(r'\[error\].+1234567890'), 'errors write int'
240 )
241
242 def test_ruby_application_at_exit(self):
243 self.load('at_exit')
244
245 self.get()
246
247 self.conf({"listeners": {}, "applications": {}})
248
249 self.stop()
250
251 self.assertIsNotNone(
252 self.wait_for_record(r'\[error\].+At exit called\.'), 'at exit'
253 )
254
255 def test_ruby_application_header_custom(self):
256 self.load('header_custom')
257
258 resp = self.post(body="\ntc=one,two\ntc=three,four,\n\n")
259
260 self.assertEqual(
261 resp['headers']['Custom-Header'],
262 ['', 'tc=one,two', 'tc=three,four,', '', ''],
263 'header custom',
264 )
265
266 @unittest.skip('not yet')
267 def test_ruby_application_header_custom_non_printable(self):
268 self.load('header_custom')
269
270 self.assertEqual(
271 self.post(body='\b')['status'], 500, 'header custom non printable'
272 )
273
274 def test_ruby_application_header_status(self):
275 self.load('header_status')
276
277 self.assertEqual(self.get()['status'], 200, 'header status')
278
279 @unittest.skip('not yet')
280 def test_ruby_application_header_rack(self):
281 self.load('header_rack')
282
283 self.assertEqual(self.get()['status'], 500, 'header rack')
284
285 def test_ruby_application_body_empty(self):
286 self.load('body_empty')
287
288 self.assertEqual(self.get()['body'], '', 'body empty')
289
290 def test_ruby_application_body_array(self):
291 self.load('body_array')
292
293 self.assertEqual(self.get()['body'], '0123456789', 'body array')
294
295 def test_ruby_application_body_large(self):
296 self.load('mirror')
297
298 body = '0123456789' * 1000
299
300 self.assertEqual(self.post(body=body)['body'], body, 'body large')
301
302 @unittest.skip('not yet')
303 def test_ruby_application_body_each_error(self):
304 self.load('body_each_error')
305
306 self.assertEqual(self.get()['status'], 500, 'body each error status')
307
308 self.stop()
309
310 self.assertIsNotNone(
311 self.wait_for_record(r'\[error\].+Failed to run ruby script'),
312 'body each error',
313 )
314
315 def test_ruby_application_body_file(self):
316 self.load('body_file')
317
318 self.assertEqual(self.get()['body'], 'body\n', 'body file')
319
320 def test_ruby_keepalive_body(self):
321 self.load('mirror')
322
323 self.assertEqual(self.get()['status'], 200, 'init')
324
325 body = '0123456789' * 500
326 (resp, sock) = self.post(
327 headers={
328 'Host': 'localhost',
329 'Connection': 'keep-alive',
330 'Content-Type': 'text/html',
331 },
332 start=True,
333 body=body,
334 read_timeout=1,
335 )
336
337 self.assertEqual(resp['body'], body, 'keep-alive 1')
338
339 body = '0123456789'
340 resp = self.post(
341 headers={
342 'Host': 'localhost',
343 'Connection': 'close',
344 'Content-Type': 'text/html',
345 },
346 sock=sock,
347 body=body,
348 )
349
350 self.assertEqual(resp['body'], body, 'keep-alive 2')
351
352 def test_ruby_application_constants(self):
353 self.load('constants')
354
355 resp = self.get()
356
357 self.assertEqual(resp['status'], 200, 'status')
358
359 headers = resp['headers']
360 self.assertGreater(len(headers['X-Copyright']), 0, 'RUBY_COPYRIGHT')
361 self.assertGreater(
362 len(headers['X-Description']), 0, 'RUBY_DESCRIPTION'
363 )
364 self.assertGreater(len(headers['X-Engine']), 0, 'RUBY_ENGINE')
365 self.assertGreater(
366 len(headers['X-Engine-Version']), 0, 'RUBY_ENGINE_VERSION'
367 )
368 self.assertGreater(len(headers['X-Patchlevel']), 0, 'RUBY_PATCHLEVEL')
369 self.assertGreater(len(headers['X-Platform']), 0, 'RUBY_PLATFORM')
370 self.assertGreater(
371 len(headers['X-Release-Date']), 0, 'RUBY_RELEASE_DATE'
372 )
373 self.assertGreater(len(headers['X-Revision']), 0, 'RUBY_REVISION')
374 self.assertGreater(len(headers['X-Version']), 0, 'RUBY_VERSION')
375
376if __name__ == '__main__':
377 TestRubyApplication.main()
3from unit.applications.lang.ruby import TestApplicationRuby
4
5
6class TestRubyApplication(TestApplicationRuby):
7 prerequisites = {'modules': {'ruby': 'all'}}
8
9 def test_ruby_application(self):
10 self.load('variables')
11
12 body = 'Test body string.'
13
14 resp = self.post(
15 headers={
16 'Host': 'localhost',
17 'Content-Type': 'text/html',
18 'Custom-Header': 'blah',
19 'Connection': 'close',
20 },
21 body=body,
22 )
23
24 self.assertEqual(resp['status'], 200, 'status')
25 headers = resp['headers']
26 header_server = headers.pop('Server')
27 self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
28 self.assertEqual(
29 headers.pop('Server-Software'),
30 header_server,
31 'server software header',
32 )
33
34 date = headers.pop('Date')
35 self.assertEqual(date[-4:], ' GMT', 'date header timezone')
36 self.assertLess(
37 abs(self.date_to_sec_epoch(date) - self.sec_epoch()),
38 5,
39 'date header',
40 )
41
42 self.assertDictEqual(
43 headers,
44 {
45 'Connection': 'close',
46 'Content-Length': str(len(body)),
47 'Content-Type': 'text/html',
48 'Request-Method': 'POST',
49 'Request-Uri': '/',
50 'Http-Host': 'localhost',
51 'Server-Protocol': 'HTTP/1.1',
52 'Custom-Header': 'blah',
53 'Rack-Version': '13',
54 'Rack-Url-Scheme': 'http',
55 'Rack-Multithread': 'false',
56 'Rack-Multiprocess': 'true',
57 'Rack-Run-Once': 'false',
58 'Rack-Hijack-Q': 'false',
59 'Rack-Hijack': '',
60 'Rack-Hijack-IO': '',
61 },
62 'headers',
63 )
64 self.assertEqual(resp['body'], body, 'body')
65
66 def test_ruby_application_query_string(self):
67 self.load('query_string')
68
69 resp = self.get(url='/?var1=val1&var2=val2')
70
71 self.assertEqual(
72 resp['headers']['Query-String'],
73 'var1=val1&var2=val2',
74 'Query-String header',
75 )
76
77 def test_ruby_application_query_string_empty(self):
78 self.load('query_string')
79
80 resp = self.get(url='/?')
81
82 self.assertEqual(resp['status'], 200, 'query string empty status')
83 self.assertEqual(
84 resp['headers']['Query-String'], '', 'query string empty'
85 )
86
87 def test_ruby_application_query_string_absent(self):
88 self.load('query_string')
89
90 resp = self.get()
91
92 self.assertEqual(resp['status'], 200, 'query string absent status')
93 self.assertEqual(
94 resp['headers']['Query-String'], '', 'query string absent'
95 )
96
97 @unittest.skip('not yet')
98 def test_ruby_application_server_port(self):
99 self.load('server_port')
100
101 self.assertEqual(
102 self.get()['headers']['Server-Port'], '7080', 'Server-Port header'
103 )
104
105 def test_ruby_application_status_int(self):
106 self.load('status_int')
107
108 self.assertEqual(self.get()['status'], 200, 'status int')
109
110 def test_ruby_application_input_read_empty(self):
111 self.load('input_read_empty')
112
113 self.assertEqual(self.get()['body'], '', 'read empty')
114
115 def test_ruby_application_input_read_parts(self):
116 self.load('input_read_parts')
117
118 self.assertEqual(
119 self.post(body='0123456789')['body'],
120 '012345678',
121 'input read parts',
122 )
123
124 def test_ruby_application_input_read_buffer(self):
125 self.load('input_read_buffer')
126
127 self.assertEqual(
128 self.post(body='0123456789')['body'],
129 '0123456789',
130 'input read buffer',
131 )
132
133 def test_ruby_application_input_read_buffer_not_empty(self):
134 self.load('input_read_buffer_not_empty')
135
136 self.assertEqual(
137 self.post(body='0123456789')['body'],
138 '0123456789',
139 'input read buffer not empty',
140 )
141
142 def test_ruby_application_input_gets(self):
143 self.load('input_gets')
144
145 body = '0123456789'
146
147 self.assertEqual(self.post(body=body)['body'], body, 'input gets')
148
149 def test_ruby_application_input_gets_2(self):
150 self.load('input_gets')
151
152 self.assertEqual(
153 self.post(body='01234\n56789\n')['body'], '01234\n', 'input gets 2'
154 )
155
156 def test_ruby_application_input_gets_all(self):
157 self.load('input_gets_all')
158
159 body = '\n01234\n56789\n\n'
160
161 self.assertEqual(self.post(body=body)['body'], body, 'input gets all')
162
163 def test_ruby_application_input_each(self):
164 self.load('input_each')
165
166 body = '\n01234\n56789\n\n'
167
168 self.assertEqual(self.post(body=body)['body'], body, 'input each')
169
170 @unittest.skip('not yet')
171 def test_ruby_application_input_rewind(self):
172 self.load('input_rewind')
173
174 body = '0123456789'
175
176 self.assertEqual(self.post(body=body)['body'], body, 'input rewind')
177
178 @unittest.skip('not yet')
179 def test_ruby_application_syntax_error(self):
180 self.skip_alerts.extend(
181 [
182 r'Failed to parse rack script',
183 r'syntax error',
184 r'new_from_string',
185 r'parse_file',
186 ]
187 )
188 self.load('syntax_error')
189
190 self.assertEqual(self.get()['status'], 500, 'syntax error')
191
192 def test_ruby_application_errors_puts(self):
193 self.load('errors_puts')
194
195 self.get()
196
197 self.stop()
198
199 self.assertIsNotNone(
200 self.wait_for_record(r'\[error\].+Error in application'),
201 'errors puts',
202 )
203
204 def test_ruby_application_errors_puts_int(self):
205 self.load('errors_puts_int')
206
207 self.get()
208
209 self.stop()
210
211 self.assertIsNotNone(
212 self.wait_for_record(r'\[error\].+1234567890'), 'errors puts int'
213 )
214
215 def test_ruby_application_errors_write(self):
216 self.load('errors_write')
217
218 self.get()
219
220 self.stop()
221
222 self.assertIsNotNone(
223 self.wait_for_record(r'\[error\].+Error in application'),
224 'errors write',
225 )
226
227 def test_ruby_application_errors_write_to_s_custom(self):
228 self.load('errors_write_to_s_custom')
229
230 self.assertEqual(self.get()['status'], 200, 'errors write to_s custom')
231
232 def test_ruby_application_errors_write_int(self):
233 self.load('errors_write_int')
234
235 self.get()
236
237 self.stop()
238
239 self.assertIsNotNone(
240 self.wait_for_record(r'\[error\].+1234567890'), 'errors write int'
241 )
242
243 def test_ruby_application_at_exit(self):
244 self.load('at_exit')
245
246 self.get()
247
248 self.conf({"listeners": {}, "applications": {}})
249
250 self.stop()
251
252 self.assertIsNotNone(
253 self.wait_for_record(r'\[error\].+At exit called\.'), 'at exit'
254 )
255
256 def test_ruby_application_header_custom(self):
257 self.load('header_custom')
258
259 resp = self.post(body="\ntc=one,two\ntc=three,four,\n\n")
260
261 self.assertEqual(
262 resp['headers']['Custom-Header'],
263 ['', 'tc=one,two', 'tc=three,four,', '', ''],
264 'header custom',
265 )
266
267 @unittest.skip('not yet')
268 def test_ruby_application_header_custom_non_printable(self):
269 self.load('header_custom')
270
271 self.assertEqual(
272 self.post(body='\b')['status'], 500, 'header custom non printable'
273 )
274
275 def test_ruby_application_header_status(self):
276 self.load('header_status')
277
278 self.assertEqual(self.get()['status'], 200, 'header status')
279
280 @unittest.skip('not yet')
281 def test_ruby_application_header_rack(self):
282 self.load('header_rack')
283
284 self.assertEqual(self.get()['status'], 500, 'header rack')
285
286 def test_ruby_application_body_empty(self):
287 self.load('body_empty')
288
289 self.assertEqual(self.get()['body'], '', 'body empty')
290
291 def test_ruby_application_body_array(self):
292 self.load('body_array')
293
294 self.assertEqual(self.get()['body'], '0123456789', 'body array')
295
296 def test_ruby_application_body_large(self):
297 self.load('mirror')
298
299 body = '0123456789' * 1000
300
301 self.assertEqual(self.post(body=body)['body'], body, 'body large')
302
303 @unittest.skip('not yet')
304 def test_ruby_application_body_each_error(self):
305 self.load('body_each_error')
306
307 self.assertEqual(self.get()['status'], 500, 'body each error status')
308
309 self.stop()
310
311 self.assertIsNotNone(
312 self.wait_for_record(r'\[error\].+Failed to run ruby script'),
313 'body each error',
314 )
315
316 def test_ruby_application_body_file(self):
317 self.load('body_file')
318
319 self.assertEqual(self.get()['body'], 'body\n', 'body file')
320
321 def test_ruby_keepalive_body(self):
322 self.load('mirror')
323
324 self.assertEqual(self.get()['status'], 200, 'init')
325
326 body = '0123456789' * 500
327 (resp, sock) = self.post(
328 headers={
329 'Host': 'localhost',
330 'Connection': 'keep-alive',
331 'Content-Type': 'text/html',
332 },
333 start=True,
334 body=body,
335 read_timeout=1,
336 )
337
338 self.assertEqual(resp['body'], body, 'keep-alive 1')
339
340 body = '0123456789'
341 resp = self.post(
342 headers={
343 'Host': 'localhost',
344 'Connection': 'close',
345 'Content-Type': 'text/html',
346 },
347 sock=sock,
348 body=body,
349 )
350
351 self.assertEqual(resp['body'], body, 'keep-alive 2')
352
353 def test_ruby_application_constants(self):
354 self.load('constants')
355
356 resp = self.get()
357
358 self.assertEqual(resp['status'], 200, 'status')
359
360 headers = resp['headers']
361 self.assertGreater(len(headers['X-Copyright']), 0, 'RUBY_COPYRIGHT')
362 self.assertGreater(
363 len(headers['X-Description']), 0, 'RUBY_DESCRIPTION'
364 )
365 self.assertGreater(len(headers['X-Engine']), 0, 'RUBY_ENGINE')
366 self.assertGreater(
367 len(headers['X-Engine-Version']), 0, 'RUBY_ENGINE_VERSION'
368 )
369 self.assertGreater(len(headers['X-Patchlevel']), 0, 'RUBY_PATCHLEVEL')
370 self.assertGreater(len(headers['X-Platform']), 0, 'RUBY_PLATFORM')
371 self.assertGreater(
372 len(headers['X-Release-Date']), 0, 'RUBY_RELEASE_DATE'
373 )
374 self.assertGreater(len(headers['X-Revision']), 0, 'RUBY_REVISION')
375 self.assertGreater(len(headers['X-Version']), 0, 'RUBY_VERSION')
376
377if __name__ == '__main__':
378 TestRubyApplication.main()