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
|
import unittest
import io
class TestHTTPChannel(unittest.TestCase):
def _makeOne(self, sock, addr, adj, map=None):
from waitress.channel import HTTPChannel
server = DummyServer()
return HTTPChannel(server, sock, addr, adj=adj, map=map)
def _makeOneWithMap(self, adj=None):
if adj is None:
adj = DummyAdjustments()
sock = DummySock()
map = {}
inst = self._makeOne(sock, '127.0.0.1', adj, map=map)
inst.outbuf_lock = DummyLock()
return inst, sock, map
def test_ctor(self):
inst, _, map = self._makeOneWithMap()
self.assertEqual(inst.addr, '127.0.0.1')
self.assertEqual(inst.sendbuf_len, 2048)
self.assertEqual(map[100], inst)
def test_total_outbufs_len_an_outbuf_size_gt_sys_maxint(self):
from waitress.compat import MAXINT
inst, _, map = self._makeOneWithMap()
class DummyBuffer(object):
chunks = []
def append(self, data):
self.chunks.append(data)
class DummyData(object):
def __len__(self):
return MAXINT
inst.total_outbufs_len = 1
inst.outbufs = [DummyBuffer()]
inst.write_soon(DummyData())
# we are testing that this method does not raise an OverflowError
# (see https://github.com/Pylons/waitress/issues/47)
self.assertEqual(inst.total_outbufs_len, MAXINT+1)
def test_writable_something_in_outbuf(self):
inst, sock, map = self._makeOneWithMap()
inst.total_outbufs_len = 3
self.assertTrue(inst.writable())
def test_writable_nothing_in_outbuf(self):
inst, sock, map = self._makeOneWithMap()
self.assertFalse(inst.writable())
def test_writable_nothing_in_outbuf_will_close(self):
inst, sock, map = self._makeOneWithMap()
inst.will_close = True
self.assertTrue(inst.writable())
def test_handle_write_not_connected(self):
inst, sock, map = self._makeOneWithMap()
inst.connected = False
self.assertFalse(inst.handle_write())
def test_handle_write_with_requests(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = True
inst.last_activity = 0
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.last_activity, 0)
def test_handle_write_no_request_with_outbuf(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
inst.outbufs = [DummyBuffer(b'abc')]
inst.total_outbufs_len = len(inst.outbufs[0])
inst.last_activity = 0
result = inst.handle_write()
self.assertEqual(result, None)
self.assertNotEqual(inst.last_activity, 0)
self.assertEqual(sock.sent, b'abc')
def test_handle_write_outbuf_raises_socketerror(self):
import socket
inst, sock, map = self._makeOneWithMap()
inst.requests = []
outbuf = DummyBuffer(b'abc', socket.error)
inst.outbufs = [outbuf]
inst.total_outbufs_len = len(outbuf)
inst.last_activity = 0
inst.logger = DummyLogger()
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.last_activity, 0)
self.assertEqual(sock.sent, b'')
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(outbuf.closed)
def test_handle_write_outbuf_raises_othererror(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
outbuf = DummyBuffer(b'abc', IOError)
inst.outbufs = [outbuf]
inst.total_outbufs_len = len(outbuf)
inst.last_activity = 0
inst.logger = DummyLogger()
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.last_activity, 0)
self.assertEqual(sock.sent, b'')
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(outbuf.closed)
def test_handle_write_no_requests_no_outbuf_will_close(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
outbuf = DummyBuffer(b'')
inst.outbufs = [outbuf]
inst.will_close = True
inst.last_activity = 0
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.connected, False)
self.assertEqual(sock.closed, True)
self.assertEqual(inst.last_activity, 0)
self.assertTrue(outbuf.closed)
def test_handle_write_no_requests_outbuf_gt_send_bytes(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = [True]
inst.outbufs = [DummyBuffer(b'abc')]
inst.total_outbufs_len = len(inst.outbufs[0])
inst.adj.send_bytes = 2
inst.will_close = False
inst.last_activity = 0
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.will_close, False)
self.assertTrue(inst.outbuf_lock.acquired)
self.assertEqual(sock.sent, b'abc')
def test_handle_write_close_when_flushed(self):
inst, sock, map = self._makeOneWithMap()
outbuf = DummyBuffer(b'abc')
inst.outbufs = [outbuf]
inst.total_outbufs_len = len(outbuf)
inst.will_close = False
inst.close_when_flushed = True
inst.last_activity = 0
result = inst.handle_write()
self.assertEqual(result, None)
self.assertEqual(inst.will_close, True)
self.assertEqual(inst.close_when_flushed, False)
self.assertEqual(sock.sent, b'abc')
self.assertTrue(outbuf.closed)
def test_readable_no_requests_not_will_close(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
inst.will_close = False
self.assertEqual(inst.readable(), True)
def test_readable_no_requests_will_close(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
inst.will_close = True
self.assertEqual(inst.readable(), False)
def test_readable_with_requests(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = True
self.assertEqual(inst.readable(), False)
def test_handle_read_no_error(self):
inst, sock, map = self._makeOneWithMap()
inst.will_close = False
inst.recv = lambda *arg: b'abc'
inst.last_activity = 0
L = []
inst.received = lambda x: L.append(x)
result = inst.handle_read()
self.assertEqual(result, None)
self.assertNotEqual(inst.last_activity, 0)
self.assertEqual(L, [b'abc'])
def test_handle_read_error(self):
import socket
inst, sock, map = self._makeOneWithMap()
inst.will_close = False
def recv(b):
raise socket.error
inst.recv = recv
inst.last_activity = 0
inst.logger = DummyLogger()
result = inst.handle_read()
self.assertEqual(result, None)
self.assertEqual(inst.last_activity, 0)
self.assertEqual(len(inst.logger.exceptions), 1)
def test_write_soon_empty_byte(self):
inst, sock, map = self._makeOneWithMap()
wrote = inst.write_soon(b'')
self.assertEqual(wrote, 0)
self.assertEqual(len(inst.outbufs[0]), 0)
def test_write_soon_nonempty_byte(self):
inst, sock, map = self._makeOneWithMap()
wrote = inst.write_soon(b'a')
self.assertEqual(wrote, 1)
self.assertEqual(len(inst.outbufs[0]), 1)
def test_write_soon_filewrapper(self):
from waitress.buffers import ReadOnlyFileBasedBuffer
f = io.BytesIO(b'abc')
wrapper = ReadOnlyFileBasedBuffer(f, 8192)
wrapper.prepare()
inst, sock, map = self._makeOneWithMap()
outbufs = inst.outbufs
orig_outbuf = outbufs[0]
wrote = inst.write_soon(wrapper)
self.assertEqual(wrote, 3)
self.assertEqual(len(outbufs), 3)
self.assertEqual(outbufs[0], orig_outbuf)
self.assertEqual(outbufs[1], wrapper)
self.assertEqual(outbufs[2].__class__.__name__, 'OverflowableBuffer')
def test_write_soon_disconnected(self):
from waitress.channel import ClientDisconnected
inst, sock, map = self._makeOneWithMap()
inst.connected = False
self.assertRaises(ClientDisconnected, lambda: inst.write_soon(b'stuff'))
def test__flush_some_empty_outbuf(self):
inst, sock, map = self._makeOneWithMap()
result = inst._flush_some()
self.assertEqual(result, False)
def test__flush_some_full_outbuf_socket_returns_nonzero(self):
inst, sock, map = self._makeOneWithMap()
inst.outbufs[0].append(b'abc')
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
result = inst._flush_some()
self.assertEqual(result, True)
def test__flush_some_full_outbuf_socket_returns_zero(self):
inst, sock, map = self._makeOneWithMap()
sock.send = lambda x: False
inst.outbufs[0].append(b'abc')
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
result = inst._flush_some()
self.assertEqual(result, False)
def test_flush_some_multiple_buffers_first_empty(self):
inst, sock, map = self._makeOneWithMap()
sock.send = lambda x: len(x)
buffer = DummyBuffer(b'abc')
inst.outbufs.append(buffer)
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
result = inst._flush_some()
self.assertEqual(result, True)
self.assertEqual(buffer.skipped, 3)
self.assertEqual(inst.outbufs, [buffer])
def test_flush_some_multiple_buffers_close_raises(self):
inst, sock, map = self._makeOneWithMap()
sock.send = lambda x: len(x)
buffer = DummyBuffer(b'abc')
inst.outbufs.append(buffer)
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
inst.logger = DummyLogger()
def doraise():
raise NotImplementedError
inst.outbufs[0].close = doraise
result = inst._flush_some()
self.assertEqual(result, True)
self.assertEqual(buffer.skipped, 3)
self.assertEqual(inst.outbufs, [buffer])
self.assertEqual(len(inst.logger.exceptions), 1)
def test__flush_some_outbuf_len_gt_sys_maxint(self):
from waitress.compat import MAXINT
inst, sock, map = self._makeOneWithMap()
class DummyHugeOutbuffer(object):
def __init__(self):
self.length = MAXINT + 1
def __len__(self):
return self.length
def get(self, numbytes):
self.length = 0
return b'123'
def skip(self, *args): pass
buf = DummyHugeOutbuffer()
inst.outbufs = [buf]
inst.send = lambda *arg: 0
result = inst._flush_some()
# we are testing that _flush_some doesn't raise an OverflowError
# when one of its outbufs has a __len__ that returns gt sys.maxint
self.assertEqual(result, False)
def test_handle_close(self):
inst, sock, map = self._makeOneWithMap()
inst.handle_close()
self.assertEqual(inst.connected, False)
self.assertEqual(sock.closed, True)
def test_handle_close_outbuf_raises_on_close(self):
inst, sock, map = self._makeOneWithMap()
def doraise():
raise NotImplementedError
inst.outbufs[0].close = doraise
inst.logger = DummyLogger()
inst.handle_close()
self.assertEqual(inst.connected, False)
self.assertEqual(sock.closed, True)
self.assertEqual(len(inst.logger.exceptions), 1)
def test_add_channel(self):
inst, sock, map = self._makeOneWithMap()
fileno = inst._fileno
inst.add_channel(map)
self.assertEqual(map[fileno], inst)
self.assertEqual(inst.server.active_channels[fileno], inst)
def test_del_channel(self):
inst, sock, map = self._makeOneWithMap()
fileno = inst._fileno
inst.server.active_channels[fileno] = True
inst.del_channel(map)
self.assertEqual(map.get(fileno), None)
self.assertEqual(inst.server.active_channels.get(fileno), None)
def test_received(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.server.tasks, [inst])
self.assertTrue(inst.requests)
def test_received_no_chunk(self):
inst, sock, map = self._makeOneWithMap()
self.assertEqual(inst.received(b''), False)
def test_received_preq_not_completed(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.completed = False
preq.empty = True
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.requests, ())
self.assertEqual(inst.server.tasks, [])
def test_received_preq_completed_empty(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.completed = True
preq.empty = True
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.request, None)
self.assertEqual(inst.server.tasks, [])
def test_received_preq_error(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.completed = True
preq.error = True
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.request, None)
self.assertEqual(len(inst.server.tasks), 1)
self.assertTrue(inst.requests)
def test_received_preq_completed_connection_close(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.completed = True
preq.empty = True
preq.connection_close = True
inst.received(b'GET / HTTP/1.1\n\n' + b'a' * 50000)
self.assertEqual(inst.request, None)
self.assertEqual(inst.server.tasks, [])
def test_received_preq_completed_n_lt_data(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.completed = True
preq.empty = False
line = b'GET / HTTP/1.1\n\n'
preq.retval = len(line)
inst.received(line + line)
self.assertEqual(inst.request, None)
self.assertEqual(len(inst.requests), 2)
self.assertEqual(len(inst.server.tasks), 1)
def test_received_headers_finished_expect_continue_false(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.expect_continue = False
preq.headers_finished = True
preq.completed = False
preq.empty = False
preq.retval = 1
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.request, preq)
self.assertEqual(inst.server.tasks, [])
self.assertEqual(inst.outbufs[0].get(100), b'')
def test_received_headers_finished_expect_continue_true(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.expect_continue = True
preq.headers_finished = True
preq.completed = False
preq.empty = False
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.request, preq)
self.assertEqual(inst.server.tasks, [])
self.assertEqual(sock.sent, b'HTTP/1.1 100 Continue\r\n\r\n')
self.assertEqual(inst.sent_continue, True)
self.assertEqual(preq.completed, False)
def test_received_headers_finished_expect_continue_true_sent_true(self):
inst, sock, map = self._makeOneWithMap()
inst.server = DummyServer()
preq = DummyParser()
inst.request = preq
preq.expect_continue = True
preq.headers_finished = True
preq.completed = False
preq.empty = False
inst.sent_continue = True
inst.received(b'GET / HTTP/1.1\n\n')
self.assertEqual(inst.request, preq)
self.assertEqual(inst.server.tasks, [])
self.assertEqual(sock.sent, b'')
self.assertEqual(inst.sent_continue, True)
self.assertEqual(preq.completed, False)
def test_service_no_requests(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = []
inst.service()
self.assertEqual(inst.requests, [])
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
def test_service_with_one_request(self):
inst, sock, map = self._makeOneWithMap()
request = DummyRequest()
inst.task_class = DummyTaskClass()
inst.requests = [request]
inst.service()
self.assertEqual(inst.requests, [])
self.assertTrue(request.serviced)
self.assertTrue(request.closed)
def test_service_with_one_error_request(self):
inst, sock, map = self._makeOneWithMap()
request = DummyRequest()
request.error = DummyError()
inst.error_task_class = DummyTaskClass()
inst.requests = [request]
inst.service()
self.assertEqual(inst.requests, [])
self.assertTrue(request.serviced)
self.assertTrue(request.closed)
def test_service_with_multiple_requests(self):
inst, sock, map = self._makeOneWithMap()
request1 = DummyRequest()
request2 = DummyRequest()
inst.task_class = DummyTaskClass()
inst.requests = [request1, request2]
inst.service()
self.assertEqual(inst.requests, [])
self.assertTrue(request1.serviced)
self.assertTrue(request2.serviced)
self.assertTrue(request1.closed)
self.assertTrue(request2.closed)
def test_service_with_request_raises(self):
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = False
inst.server = DummyServer()
request = DummyRequest()
inst.requests = [request]
inst.task_class = DummyTaskClass(ValueError)
inst.task_class.wrote_header = False
inst.error_task_class = DummyTaskClass()
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertFalse(inst.will_close)
self.assertEqual(inst.error_task_class.serviced, True)
self.assertTrue(request.closed)
def test_service_with_requests_raises_already_wrote_header(self):
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = False
inst.server = DummyServer()
request = DummyRequest()
inst.requests = [request]
inst.task_class = DummyTaskClass(ValueError)
inst.error_task_class = DummyTaskClass()
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertTrue(inst.close_when_flushed)
self.assertEqual(inst.error_task_class.serviced, False)
self.assertTrue(request.closed)
def test_service_with_requests_raises_didnt_write_header_expose_tbs(self):
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = True
inst.server = DummyServer()
request = DummyRequest()
inst.requests = [request]
inst.task_class = DummyTaskClass(ValueError)
inst.task_class.wrote_header = False
inst.error_task_class = DummyTaskClass()
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertFalse(inst.will_close)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertEqual(inst.error_task_class.serviced, True)
self.assertTrue(request.closed)
def test_service_with_requests_raises_didnt_write_header(self):
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = False
inst.server = DummyServer()
request = DummyRequest()
inst.requests = [request]
inst.task_class = DummyTaskClass(ValueError)
inst.task_class.wrote_header = False
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertTrue(inst.close_when_flushed)
self.assertTrue(request.closed)
def test_service_with_request_raises_disconnect(self):
from waitress.channel import ClientDisconnected
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = False
inst.server = DummyServer()
request = DummyRequest()
inst.requests = [request]
inst.task_class = DummyTaskClass(ClientDisconnected)
inst.error_task_class = DummyTaskClass()
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.infos), 1)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertFalse(inst.will_close)
self.assertEqual(inst.error_task_class.serviced, False)
self.assertTrue(request.closed)
def test_service_with_request_error_raises_disconnect(self):
from waitress.channel import ClientDisconnected
inst, sock, map = self._makeOneWithMap()
inst.adj.expose_tracebacks = False
inst.server = DummyServer()
request = DummyRequest()
err_request = DummyRequest()
inst.requests = [request]
inst.parser_class = lambda x: err_request
inst.task_class = DummyTaskClass(RuntimeError)
inst.task_class.wrote_header = False
inst.error_task_class = DummyTaskClass(ClientDisconnected)
inst.logger = DummyLogger()
inst.service()
self.assertTrue(request.serviced)
self.assertTrue(err_request.serviced)
self.assertEqual(inst.requests, [])
self.assertEqual(len(inst.logger.exceptions), 1)
self.assertEqual(len(inst.logger.infos), 0)
self.assertTrue(inst.server.trigger_pulled)
self.assertTrue(inst.last_activity)
self.assertFalse(inst.will_close)
self.assertEqual(inst.task_class.serviced, True)
self.assertEqual(inst.error_task_class.serviced, True)
self.assertTrue(request.closed)
def test_cancel_no_requests(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = ()
inst.cancel()
self.assertEqual(inst.requests, [])
def test_cancel_with_requests(self):
inst, sock, map = self._makeOneWithMap()
inst.requests = [None]
inst.cancel()
self.assertEqual(inst.requests, [])
class DummySock(object):
blocking = False
closed = False
def __init__(self):
self.sent = b''
def setblocking(self, *arg):
self.blocking = True
def fileno(self):
return 100
def getpeername(self):
return '127.0.0.1'
def getsockopt(self, level, option):
return 2048
def close(self):
self.closed = True
def send(self, data):
self.sent += data
return len(data)
class DummyLock(object):
def __init__(self, acquirable=True):
self.acquirable = acquirable
def acquire(self, val):
self.val = val
self.acquired = True
return self.acquirable
def release(self):
self.released = True
def __exit__(self, type, val, traceback):
self.acquire(True)
def __enter__(self):
pass
class DummyBuffer(object):
closed = False
def __init__(self, data, toraise=None):
self.data = data
self.toraise = toraise
def get(self, *arg):
if self.toraise:
raise self.toraise
data = self.data
self.data = b''
return data
def skip(self, num, x):
self.skipped = num
def __len__(self):
return len(self.data)
def close(self):
self.closed = True
class DummyAdjustments(object):
outbuf_overflow = 1048576
inbuf_overflow = 512000
cleanup_interval = 900
url_scheme = 'http'
channel_timeout = 300
log_socket_errors = True
recv_bytes = 8192
send_bytes = 1
expose_tracebacks = True
ident = 'waitress'
max_request_header_size = 10000
class DummyServer(object):
trigger_pulled = False
adj = DummyAdjustments()
def __init__(self):
self.tasks = []
self.active_channels = {}
def add_task(self, task):
self.tasks.append(task)
def pull_trigger(self):
self.trigger_pulled = True
class DummyParser(object):
version = 1
data = None
completed = True
empty = False
headers_finished = False
expect_continue = False
retval = None
error = None
connection_close = False
def received(self, data):
self.data = data
if self.retval is not None:
return self.retval
return len(data)
class DummyRequest(object):
error = None
path = '/'
version = '1.0'
closed = False
def __init__(self):
self.headers = {}
def close(self):
self.closed = True
class DummyLogger(object):
def __init__(self):
self.exceptions = []
self.infos = []
self.warnings = []
def info(self, msg):
self.infos.append(msg)
def exception(self, msg):
self.exceptions.append(msg)
class DummyError(object):
code = '431'
reason = 'Bleh'
body = 'My body'
class DummyTaskClass(object):
wrote_header = True
close_on_finish = False
serviced = False
def __init__(self, toraise=None):
self.toraise = toraise
def __call__(self, channel, request):
self.request = request
return self
def service(self):
self.serviced = True
self.request.serviced = True
if self.toraise:
raise self.toraise
|