1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
|
import unittest
import io
class TestThreadedTaskDispatcher(unittest.TestCase):
def _makeOne(self):
from waitress.task import ThreadedTaskDispatcher
return ThreadedTaskDispatcher()
def test_handler_thread_task_is_None(self):
inst = self._makeOne()
inst.threads[0] = True
inst.queue.put(None)
inst.handler_thread(0)
self.assertEqual(inst.stop_count, -1)
self.assertEqual(inst.threads, {})
def test_handler_thread_task_raises(self):
from waitress.task import JustTesting
inst = self._makeOne()
inst.threads[0] = True
inst.logger = DummyLogger()
task = DummyTask(JustTesting)
inst.logger = DummyLogger()
inst.queue.put(task)
inst.handler_thread(0)
self.assertEqual(inst.stop_count, -1)
self.assertEqual(inst.threads, {})
self.assertEqual(len(inst.logger.logged), 1)
def test_set_thread_count_increase(self):
inst = self._makeOne()
L = []
inst.start_new_thread = lambda *x: L.append(x)
inst.set_thread_count(1)
self.assertEqual(L, [(inst.handler_thread, (0,))])
def test_set_thread_count_increase_with_existing(self):
inst = self._makeOne()
L = []
inst.threads = {0: 1}
inst.start_new_thread = lambda *x: L.append(x)
inst.set_thread_count(2)
self.assertEqual(L, [(inst.handler_thread, (1,))])
def test_set_thread_count_decrease(self):
inst = self._makeOne()
inst.threads = {'a': 1, 'b': 2}
inst.set_thread_count(1)
self.assertEqual(inst.queue.qsize(), 1)
self.assertEqual(inst.queue.get(), None)
def test_set_thread_count_same(self):
inst = self._makeOne()
L = []
inst.start_new_thread = lambda *x: L.append(x)
inst.threads = {0: 1}
inst.set_thread_count(1)
self.assertEqual(L, [])
def test_add_task(self):
task = DummyTask()
inst = self._makeOne()
inst.add_task(task)
self.assertEqual(inst.queue.qsize(), 1)
self.assertTrue(task.deferred)
def test_add_task_defer_raises(self):
task = DummyTask(ValueError)
inst = self._makeOne()
self.assertRaises(ValueError, inst.add_task, task)
self.assertEqual(inst.queue.qsize(), 0)
self.assertTrue(task.deferred)
self.assertTrue(task.cancelled)
def test_shutdown_one_thread(self):
inst = self._makeOne()
inst.threads[0] = 1
inst.logger = DummyLogger()
task = DummyTask()
inst.queue.put(task)
self.assertEqual(inst.shutdown(timeout=.01), True)
self.assertEqual(inst.logger.logged, ['1 thread(s) still running'])
self.assertEqual(task.cancelled, True)
def test_shutdown_no_threads(self):
inst = self._makeOne()
self.assertEqual(inst.shutdown(timeout=.01), True)
def test_shutdown_no_cancel_pending(self):
inst = self._makeOne()
self.assertEqual(inst.shutdown(cancel_pending=False, timeout=.01),
False)
class TestTask(unittest.TestCase):
def _makeOne(self, channel=None, request=None):
if channel is None:
channel = DummyChannel()
if request is None:
request = DummyParser()
from waitress.task import Task
return Task(channel, request)
def test_ctor_version_not_in_known(self):
request = DummyParser()
request.version = '8.4'
inst = self._makeOne(request=request)
self.assertEqual(inst.version, '1.0')
def test_cancel(self):
inst = self._makeOne()
inst.cancel()
self.assertTrue(inst.close_on_finish)
def test_defer(self):
inst = self._makeOne()
self.assertEqual(inst.defer(), None)
def test_build_response_header_bad_http_version(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '8.4'
self.assertRaises(AssertionError, inst.build_response_header)
def test_build_response_header_v10_keepalive_no_content_length(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.request.headers['CONNECTION'] = 'keep-alive'
inst.version = '1.0'
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 4)
self.assertEqual(lines[0], b'HTTP/1.0 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
self.assertEqual(inst.close_on_finish, True)
self.assertTrue(('Connection', 'close') in inst.response_headers)
def test_build_response_header_v10_keepalive_with_content_length(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.request.headers['CONNECTION'] = 'keep-alive'
inst.response_headers = [('Content-Length', '10')]
inst.version = '1.0'
inst.content_length = 0
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 5)
self.assertEqual(lines[0], b'HTTP/1.0 200 OK')
self.assertEqual(lines[1], b'Connection: Keep-Alive')
self.assertEqual(lines[2], b'Content-Length: 10')
self.assertTrue(lines[3].startswith(b'Date:'))
self.assertEqual(lines[4], b'Server: waitress')
self.assertEqual(inst.close_on_finish, False)
def test_build_response_header_v11_connection_closed_by_client(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '1.1'
inst.request.headers['CONNECTION'] = 'close'
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 5)
self.assertEqual(lines[0], b'HTTP/1.1 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
self.assertEqual(lines[4], b'Transfer-Encoding: chunked')
self.assertTrue(('Connection', 'close') in inst.response_headers)
self.assertEqual(inst.close_on_finish, True)
def test_build_response_header_v11_connection_keepalive_by_client(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.request.headers['CONNECTION'] = 'keep-alive'
inst.version = '1.1'
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 5)
self.assertEqual(lines[0], b'HTTP/1.1 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
self.assertEqual(lines[4], b'Transfer-Encoding: chunked')
self.assertTrue(('Connection', 'close') in inst.response_headers)
self.assertEqual(inst.close_on_finish, True)
def test_build_response_header_v11_200_no_content_length(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '1.1'
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 5)
self.assertEqual(lines[0], b'HTTP/1.1 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
self.assertEqual(lines[4], b'Transfer-Encoding: chunked')
self.assertEqual(inst.close_on_finish, True)
self.assertTrue(('Connection', 'close') in inst.response_headers)
def test_build_response_header_via_added(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '1.0'
inst.response_headers = [('Server', 'abc')]
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 5)
self.assertEqual(lines[0], b'HTTP/1.0 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: abc')
self.assertEqual(lines[4], b'Via: waitress')
def test_build_response_header_date_exists(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '1.0'
inst.response_headers = [('Date', 'date')]
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 4)
self.assertEqual(lines[0], b'HTTP/1.0 200 OK')
self.assertEqual(lines[1], b'Connection: close')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
def test_build_response_header_preexisting_content_length(self):
inst = self._makeOne()
inst.request = DummyParser()
inst.version = '1.1'
inst.content_length = 100
result = inst.build_response_header()
lines = filter_lines(result)
self.assertEqual(len(lines), 4)
self.assertEqual(lines[0], b'HTTP/1.1 200 OK')
self.assertEqual(lines[1], b'Content-Length: 100')
self.assertTrue(lines[2].startswith(b'Date:'))
self.assertEqual(lines[3], b'Server: waitress')
def test_remove_content_length_header(self):
inst = self._makeOne()
inst.response_headers = [('Content-Length', '70')]
inst.remove_content_length_header()
self.assertEqual(inst.response_headers, [])
def test_start(self):
inst = self._makeOne()
inst.start()
self.assertTrue(inst.start_time)
def test_finish_didnt_write_header(self):
inst = self._makeOne()
inst.wrote_header = False
inst.complete = True
inst.finish()
self.assertTrue(inst.channel.written)
def test_finish_wrote_header(self):
inst = self._makeOne()
inst.wrote_header = True
inst.finish()
self.assertFalse(inst.channel.written)
def test_finish_chunked_response(self):
inst = self._makeOne()
inst.wrote_header = True
inst.chunked_response = True
inst.finish()
self.assertEqual(inst.channel.written, b'0\r\n\r\n')
def test_write_wrote_header(self):
inst = self._makeOne()
inst.wrote_header = True
inst.complete = True
inst.content_length = 3
inst.write(b'abc')
self.assertEqual(inst.channel.written, b'abc')
def test_write_header_not_written(self):
inst = self._makeOne()
inst.wrote_header = False
inst.complete = True
inst.write(b'abc')
self.assertTrue(inst.channel.written)
self.assertEqual(inst.wrote_header, True)
def test_write_start_response_uncalled(self):
inst = self._makeOne()
self.assertRaises(RuntimeError, inst.write, b'')
def test_write_chunked_response(self):
inst = self._makeOne()
inst.wrote_header = True
inst.chunked_response = True
inst.complete = True
inst.write(b'abc')
self.assertEqual(inst.channel.written, b'3\r\nabc\r\n')
def test_write_preexisting_content_length(self):
inst = self._makeOne()
inst.wrote_header = True
inst.complete = True
inst.content_length = 1
inst.logger = DummyLogger()
inst.write(b'abc')
self.assertTrue(inst.channel.written)
self.assertEqual(inst.logged_write_excess, True)
self.assertEqual(len(inst.logger.logged), 1)
class TestWSGITask(unittest.TestCase):
def _makeOne(self, channel=None, request=None):
if channel is None:
channel = DummyChannel()
if request is None:
request = DummyParser()
from waitress.task import WSGITask
return WSGITask(channel, request)
def test_service(self):
inst = self._makeOne()
def execute():
inst.executed = True
inst.execute = execute
inst.complete = True
inst.service()
self.assertTrue(inst.start_time)
self.assertTrue(inst.close_on_finish)
self.assertTrue(inst.channel.written)
self.assertEqual(inst.executed, True)
def test_service_server_raises_socket_error(self):
import socket
inst = self._makeOne()
def execute():
raise socket.error
inst.execute = execute
self.assertRaises(socket.error, inst.service)
self.assertTrue(inst.start_time)
self.assertTrue(inst.close_on_finish)
self.assertFalse(inst.channel.written)
def test_execute_app_calls_start_response_twice_wo_exc_info(self):
def app(environ, start_response):
start_response('200 OK', [])
start_response('200 OK', [])
inst = self._makeOne()
inst.channel.server.application = app
self.assertRaises(AssertionError, inst.execute)
def test_execute_app_calls_start_response_w_exc_info_complete(self):
def app(environ, start_response):
start_response('200 OK', [], [ValueError, ValueError(), None])
inst = self._makeOne()
inst.complete = True
inst.channel.server.application = app
self.assertRaises(ValueError, inst.execute)
def test_execute_app_calls_start_response_w_exc_info_incomplete(self):
def app(environ, start_response):
start_response('200 OK', [], [ValueError, None, None])
return [b'a']
inst = self._makeOne()
inst.complete = False
inst.channel.server.application = app
inst.execute()
self.assertTrue(inst.complete)
self.assertEqual(inst.status, '200 OK')
self.assertTrue(inst.channel.written)
def test_execute_bad_header_key(self):
def app(environ, start_response):
start_response('200 OK', [(None, 'a')])
inst = self._makeOne()
inst.channel.server.application = app
self.assertRaises(AssertionError, inst.execute)
def test_execute_bad_header_value(self):
def app(environ, start_response):
start_response('200 OK', [('a', None)])
inst = self._makeOne()
inst.channel.server.application = app
self.assertRaises(AssertionError, inst.execute)
def test_execute_hopbyhop_header(self):
def app(environ, start_response):
start_response('200 OK', [('Connection', 'close')])
inst = self._makeOne()
inst.channel.server.application = app
self.assertRaises(AssertionError, inst.execute)
def test_execute_bad_status_value(self):
def app(environ, start_response):
start_response(None, [])
inst = self._makeOne()
inst.channel.server.application = app
self.assertRaises(AssertionError, inst.execute)
def test_execute_with_content_length_header(self):
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '1')])
return [b'a']
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertEqual(inst.content_length, 1)
def test_execute_app_calls_write(self):
def app(environ, start_response):
write = start_response('200 OK', [('Content-Length', '3')])
write(b'abc')
return []
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertEqual(inst.channel.written[-3:], b'abc')
def test_execute_app_returns_len1_chunk_without_cl(self):
def app(environ, start_response):
start_response('200 OK', [])
return [b'abc']
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertEqual(inst.content_length, 3)
def test_execute_app_returns_empty_chunk_as_first(self):
def app(environ, start_response):
start_response('200 OK', [])
return ['', b'abc']
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertEqual(inst.content_length, None)
def test_execute_app_returns_too_many_bytes(self):
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '1')])
return [b'abc']
inst = self._makeOne()
inst.channel.server.application = app
inst.logger = DummyLogger()
inst.execute()
self.assertEqual(inst.close_on_finish, True)
self.assertEqual(len(inst.logger.logged), 1)
def test_execute_app_returns_too_few_bytes(self):
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '3')])
return [b'a']
inst = self._makeOne()
inst.channel.server.application = app
inst.logger = DummyLogger()
inst.execute()
self.assertEqual(inst.close_on_finish, True)
self.assertEqual(len(inst.logger.logged), 1)
def test_execute_app_do_not_warn_on_head(self):
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '3')])
return [b'']
inst = self._makeOne()
inst.request.command = 'HEAD'
inst.channel.server.application = app
inst.logger = DummyLogger()
inst.execute()
self.assertEqual(inst.close_on_finish, True)
self.assertEqual(len(inst.logger.logged), 0)
def test_execute_app_returns_closeable(self):
class closeable(list):
def close(self):
self.closed = True
foo = closeable([b'abc'])
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '3')])
return foo
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertEqual(foo.closed, True)
def test_execute_app_returns_filewrapper_prepare_returns_True(self):
from waitress.buffers import ReadOnlyFileBasedBuffer
f = io.BytesIO(b'abc')
app_iter = ReadOnlyFileBasedBuffer(f, 8192)
def app(environ, start_response):
start_response('200 OK', [('Content-Length', '3')])
return app_iter
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertTrue(inst.channel.written) # header
self.assertEqual(inst.channel.otherdata, [app_iter])
def test_execute_app_returns_filewrapper_prepare_returns_True_nocl(self):
from waitress.buffers import ReadOnlyFileBasedBuffer
f = io.BytesIO(b'abc')
app_iter = ReadOnlyFileBasedBuffer(f, 8192)
def app(environ, start_response):
start_response('200 OK', [])
return app_iter
inst = self._makeOne()
inst.channel.server.application = app
inst.execute()
self.assertTrue(inst.channel.written) # header
self.assertEqual(inst.channel.otherdata, [app_iter])
self.assertEqual(inst.content_length, 3)
def test_execute_app_returns_filewrapper_prepare_returns_True_badcl(self):
from waitress.buffers import ReadOnlyFileBasedBuffer
f = io.BytesIO(b'abc')
app_iter = ReadOnlyFileBasedBuffer(f, 8192)
def app(environ, start_response):
start_response('200 OK', [])
return app_iter
inst = self._makeOne()
inst.channel.server.application = app
inst.content_length = 10
inst.response_headers = [('Content-Length', '10')]
inst.execute()
self.assertTrue(inst.channel.written) # header
self.assertEqual(inst.channel.otherdata, [app_iter])
self.assertEqual(inst.content_length, 3)
self.assertEqual(dict(inst.response_headers)['Content-Length'], '3')
def test_get_environment_already_cached(self):
inst = self._makeOne()
inst.environ = object()
self.assertEqual(inst.get_environment(), inst.environ)
def test_get_environment_path_startswith_more_than_one_slash(self):
inst = self._makeOne()
request = DummyParser()
request.path = '///abc'
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['PATH_INFO'], '/abc')
def test_get_environment_path_empty(self):
inst = self._makeOne()
request = DummyParser()
request.path = ''
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['PATH_INFO'], '/')
def test_get_environment_no_query(self):
inst = self._makeOne()
request = DummyParser()
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['QUERY_STRING'], '')
def test_get_environment_with_query(self):
inst = self._makeOne()
request = DummyParser()
request.query = 'abc'
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['QUERY_STRING'], 'abc')
def test_get_environ_with_url_prefix_miss(self):
inst = self._makeOne()
inst.channel.server.adj.url_prefix = '/foo'
request = DummyParser()
request.path = '/bar'
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['PATH_INFO'], '/bar')
self.assertEqual(environ['SCRIPT_NAME'], '/foo')
def test_get_environ_with_url_prefix_hit(self):
inst = self._makeOne()
inst.channel.server.adj.url_prefix = '/foo'
request = DummyParser()
request.path = '/foo/fuz'
inst.request = request
environ = inst.get_environment()
self.assertEqual(environ['PATH_INFO'], '/fuz')
self.assertEqual(environ['SCRIPT_NAME'], '/foo')
def test_get_environment_values(self):
import sys
inst = self._makeOne()
request = DummyParser()
request.headers = {
'CONTENT_TYPE': 'abc',
'CONTENT_LENGTH': '10',
'X_FOO': 'BAR',
'CONNECTION': 'close',
}
request.query = 'abc'
inst.request = request
environ = inst.get_environment()
# nail the keys of environ
self.assertEqual(sorted(environ.keys()), [
'CONTENT_LENGTH', 'CONTENT_TYPE', 'HTTP_CONNECTION', 'HTTP_X_FOO',
'PATH_INFO', 'QUERY_STRING', 'REMOTE_ADDR', 'REQUEST_METHOD',
'SCRIPT_NAME', 'SERVER_NAME', 'SERVER_PORT', 'SERVER_PROTOCOL',
'SERVER_SOFTWARE', 'wsgi.errors', 'wsgi.file_wrapper', 'wsgi.input',
'wsgi.multiprocess', 'wsgi.multithread', 'wsgi.run_once',
'wsgi.url_scheme', 'wsgi.version'])
self.assertEqual(environ['REQUEST_METHOD'], 'GET')
self.assertEqual(environ['SERVER_PORT'], '80')
self.assertEqual(environ['SERVER_NAME'], 'localhost')
self.assertEqual(environ['SERVER_SOFTWARE'], 'waitress')
self.assertEqual(environ['SERVER_PROTOCOL'], 'HTTP/1.0')
self.assertEqual(environ['SCRIPT_NAME'], '')
self.assertEqual(environ['HTTP_CONNECTION'], 'close')
self.assertEqual(environ['PATH_INFO'], '/')
self.assertEqual(environ['QUERY_STRING'], 'abc')
self.assertEqual(environ['REMOTE_ADDR'], '127.0.0.1')
self.assertEqual(environ['CONTENT_TYPE'], 'abc')
self.assertEqual(environ['CONTENT_LENGTH'], '10')
self.assertEqual(environ['HTTP_X_FOO'], 'BAR')
self.assertEqual(environ['wsgi.version'], (1, 0))
self.assertEqual(environ['wsgi.url_scheme'], 'http')
self.assertEqual(environ['wsgi.errors'], sys.stderr)
self.assertEqual(environ['wsgi.multithread'], True)
self.assertEqual(environ['wsgi.multiprocess'], False)
self.assertEqual(environ['wsgi.run_once'], False)
self.assertEqual(environ['wsgi.input'], 'stream')
self.assertEqual(inst.environ, environ)
def test_get_environment_values_w_scheme_override(self):
import sys
inst = self._makeOne()
request = DummyParser()
request.headers = {
'CONTENT_TYPE': 'abc',
'CONTENT_LENGTH': '10',
'X_FOO': 'BAR',
'X_WSGI_URL_SCHEME': 'https',
'CONNECTION': 'close',
}
request.query = 'abc'
inst.request = request
environ = inst.get_environment()
# nail the keys of environ
self.assertEqual(sorted(environ.keys()), [
'CONTENT_LENGTH', 'CONTENT_TYPE', 'HTTP_CONNECTION', 'HTTP_X_FOO',
'PATH_INFO', 'QUERY_STRING', 'REMOTE_ADDR', 'REQUEST_METHOD',
'SCRIPT_NAME', 'SERVER_NAME', 'SERVER_PORT', 'SERVER_PROTOCOL',
'SERVER_SOFTWARE', 'wsgi.errors', 'wsgi.file_wrapper', 'wsgi.input',
'wsgi.multiprocess', 'wsgi.multithread', 'wsgi.run_once',
'wsgi.url_scheme', 'wsgi.version'])
self.assertEqual(environ['REQUEST_METHOD'], 'GET')
self.assertEqual(environ['SERVER_PORT'], '80')
self.assertEqual(environ['SERVER_NAME'], 'localhost')
self.assertEqual(environ['SERVER_SOFTWARE'], 'waitress')
self.assertEqual(environ['SERVER_PROTOCOL'], 'HTTP/1.0')
self.assertEqual(environ['SCRIPT_NAME'], '')
self.assertEqual(environ['HTTP_CONNECTION'], 'close')
self.assertEqual(environ['PATH_INFO'], '/')
self.assertEqual(environ['QUERY_STRING'], 'abc')
self.assertEqual(environ['REMOTE_ADDR'], '127.0.0.1')
self.assertEqual(environ['CONTENT_TYPE'], 'abc')
self.assertEqual(environ['CONTENT_LENGTH'], '10')
self.assertEqual(environ['HTTP_X_FOO'], 'BAR')
self.assertEqual(environ['wsgi.version'], (1, 0))
self.assertEqual(environ['wsgi.url_scheme'], 'https')
self.assertEqual(environ['wsgi.errors'], sys.stderr)
self.assertEqual(environ['wsgi.multithread'], True)
self.assertEqual(environ['wsgi.multiprocess'], False)
self.assertEqual(environ['wsgi.run_once'], False)
self.assertEqual(environ['wsgi.input'], 'stream')
self.assertEqual(inst.environ, environ)
class TestErrorTask(unittest.TestCase):
def _makeOne(self, channel=None, request=None):
if channel is None:
channel = DummyChannel()
if request is None:
request = DummyParser()
request.error = DummyError()
from waitress.task import ErrorTask
return ErrorTask(channel, request)
def test_execute_http_10(self):
inst = self._makeOne()
inst.execute()
lines = filter_lines(inst.channel.written)
self.assertEqual(len(lines), 9)
self.assertEqual(lines[0], b'HTTP/1.0 432 Too Ugly')
self.assertEqual(lines[1], b'Connection: close')
self.assertEqual(lines[2], b'Content-Length: 43')
self.assertEqual(lines[3], b'Content-Type: text/plain')
self.assertTrue(lines[4])
self.assertEqual(lines[5], b'Server: waitress')
self.assertEqual(lines[6], b'Too Ugly')
self.assertEqual(lines[7], b'body')
self.assertEqual(lines[8], b'(generated by waitress)')
def test_execute_http_11(self):
inst = self._makeOne()
inst.version = '1.1'
inst.execute()
lines = filter_lines(inst.channel.written)
self.assertEqual(len(lines), 8)
self.assertEqual(lines[0], b'HTTP/1.1 432 Too Ugly')
self.assertEqual(lines[1], b'Content-Length: 43')
self.assertEqual(lines[2], b'Content-Type: text/plain')
self.assertTrue(lines[3])
self.assertEqual(lines[4], b'Server: waitress')
self.assertEqual(lines[5], b'Too Ugly')
self.assertEqual(lines[6], b'body')
self.assertEqual(lines[7], b'(generated by waitress)')
def test_execute_http_11_close(self):
inst = self._makeOne()
inst.version = '1.1'
inst.request.headers['CONNECTION'] = 'close'
inst.execute()
lines = filter_lines(inst.channel.written)
self.assertEqual(len(lines), 9)
self.assertEqual(lines[0], b'HTTP/1.1 432 Too Ugly')
self.assertEqual(lines[1], b'Connection: close')
self.assertEqual(lines[2], b'Content-Length: 43')
self.assertEqual(lines[3], b'Content-Type: text/plain')
self.assertTrue(lines[4])
self.assertEqual(lines[5], b'Server: waitress')
self.assertEqual(lines[6], b'Too Ugly')
self.assertEqual(lines[7], b'body')
self.assertEqual(lines[8], b'(generated by waitress)')
def test_execute_http_11_keep(self):
inst = self._makeOne()
inst.version = '1.1'
inst.request.headers['CONNECTION'] = 'keep-alive'
inst.execute()
lines = filter_lines(inst.channel.written)
self.assertEqual(len(lines), 8)
self.assertEqual(lines[0], b'HTTP/1.1 432 Too Ugly')
self.assertEqual(lines[1], b'Content-Length: 43')
self.assertEqual(lines[2], b'Content-Type: text/plain')
self.assertTrue(lines[3])
self.assertEqual(lines[4], b'Server: waitress')
self.assertEqual(lines[5], b'Too Ugly')
self.assertEqual(lines[6], b'body')
self.assertEqual(lines[7], b'(generated by waitress)')
class DummyError(object):
code = '432'
reason = 'Too Ugly'
body = 'body'
class DummyTask(object):
serviced = False
deferred = False
cancelled = False
def __init__(self, toraise=None):
self.toraise = toraise
def service(self):
self.serviced = True
if self.toraise:
raise self.toraise
def defer(self):
self.deferred = True
if self.toraise:
raise self.toraise
def cancel(self):
self.cancelled = True
class DummyAdj(object):
log_socket_errors = True
ident = 'waitress'
host = '127.0.0.1'
port = 80
url_prefix = ''
class DummyServer(object):
server_name = 'localhost'
effective_port = 80
def __init__(self):
self.adj = DummyAdj()
class DummyChannel(object):
closed_when_done = False
adj = DummyAdj()
creation_time = 0
addr = ['127.0.0.1']
def __init__(self, server=None):
if server is None:
server = DummyServer()
self.server = server
self.written = b''
self.otherdata = []
def write_soon(self, data):
if isinstance(data, bytes):
self.written += data
else:
self.otherdata.append(data)
return len(data)
class DummyParser(object):
version = '1.0'
command = 'GET'
path = '/'
query = ''
url_scheme = 'http'
expect_continue = False
headers_finished = False
def __init__(self):
self.headers = {}
def get_body_stream(self):
return 'stream'
def filter_lines(s):
return list(filter(None, s.split(b'\r\n')))
class DummyLogger(object):
def __init__(self):
self.logged = []
def warning(self, msg):
self.logged.append(msg)
def exception(self, msg):
self.logged.append(msg)
|