-
-
Notifications
You must be signed in to change notification settings - Fork 947
/
helpers.py
1416 lines (1111 loc) · 52.8 KB
/
helpers.py
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2013 by Rackspace Hosting, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Testing utilities.
This module contains various testing utilities that can be accessed
directly from the `testing` package::
from falcon import testing
wsgi_environ = testing.create_environ()
"""
import asyncio
import cgi
from collections import defaultdict
from collections import deque
import contextlib
from enum import Enum
import io
import itertools
import json
import random
import re
import socket
import sys
import time
from typing import Any
from typing import Dict
from typing import Iterable
from typing import Optional
from typing import Union
import falcon
from falcon import errors as falcon_errors
from falcon.asgi_spec import EventType
from falcon.asgi_spec import ScopeType
from falcon.asgi_spec import WSCloseCode
from falcon.constants import SINGLETON_HEADERS
import falcon.request
from falcon.util import uri
# NOTE(kgriffs): Changed in 3.0 from 'curl/7.24.0 (x86_64-apple-darwin12.0)'
DEFAULT_UA = 'falcon-client/' + falcon.__version__
DEFAULT_HOST = 'falconframework.org'
class ASGILifespanEventEmitter:
"""Emits ASGI lifespan events to an ASGI app.
This class can be used to drive a standard ASGI app callable in order to
perform functional tests on the app in question.
When simulating both lifespan and per-request events, each event stream
will require a separate invocation of the ASGI callable; one with a
lifespan event emitter, and one with a request event emitter. An
asyncio :class:`~asyncio.Condition` can be used to pause the
lifespan emitter until all of the desired request events have been
emitted.
Keyword Args:
shutting_down (asyncio.Condition): An instance of
:class:`asyncio.Condition` that will be awaited before
emitting the final shutdown event (``'lifespan.shutdown``).
"""
def __init__(self, shutting_down):
self._state = 0
self._shutting_down = shutting_down
async def emit(self):
if self._state == 0:
self._state += 1
return {'type': EventType.LIFESPAN_STARTUP}
if self._state == 1:
self._state += 1
# NOTE(kgriffs): This verifies the app ignores events it does
# not recognize.
return {'type': 'lifespan._nonstandard_event'}
async with self._shutting_down:
await self._shutting_down.wait()
return {'type': EventType.LIFESPAN_SHUTDOWN}
__call__ = emit
class ASGIRequestEventEmitter:
"""Emits events on-demand to an ASGI app.
This class can be used to drive a standard ASGI app callable in order to
perform functional tests on the app in question.
Note:
In order to ensure the app is able to handle subtle variations
in the ASGI events that are allowed by the specification, such
variations are applied to the emitted events at unspecified
intervals. This includes whether or not the `more_body` field
is explicitly set, or whether or not the request `body` chunk in
the event is occasionally empty,
Keyword Args:
body (str): The body content to use when emitting http.request
events. May be an empty string. If a byte string, it will
be used as-is; otherwise it will be encoded as UTF-8
(default ``b''``).
chunk_size (int): The maximum number of bytes to include in
a single http.request event (default 4096).
disconnect_at (float): The Unix timestamp after which to begin
emitting ``'http.disconnect'`` events (default now + 30s). The
value may be either an ``int`` or a ``float``, depending
on the precision required. Setting `disconnect_at` to
``0`` is treated as a special case, and will result in an
``'http.disconnect'`` event being immediately emitted (rather than
first emitting an ``'http.request'`` event).
Attributes:
disconnected (bool): Returns ``True`` if the simulated client
connection is in a "disconnected" state.
"""
# TODO(kgriffs): If this pattern later becomes useful elsewhere,
# factor out into a standalone helper class.
_branch_decider = defaultdict(bool) # type: defaultdict
def __init__(
self,
body: Union[str, bytes] = None,
chunk_size: int = None,
disconnect_at: Union[int, float] = None,
):
if body is None:
body = b''
elif not isinstance(body, bytes):
body = body.encode()
body = memoryview(body)
if disconnect_at is None:
disconnect_at = time.time() + 30
if chunk_size is None:
chunk_size = 4096
self._body = body # type: Optional[memoryview]
self._chunk_size = chunk_size
self._emit_empty_chunks = True
self._disconnect_at = disconnect_at
self._disconnected = False
self._exhaust_body = True
self._emitted_empty_chunk_a = False
self._emitted_empty_chunk_b = False
@property
def disconnected(self):
return self._disconnected or (self._disconnect_at <= time.time())
def disconnect(self, exhaust_body: bool = None):
"""Set the client connection state to disconnected.
Call this method to simulate an immediate client disconnect and
begin emitting ``'http.disconnect'`` events.
Arguments:
exhaust_body (bool): Set to ``False`` in order to
begin emitting ``'http.disconnect'`` events without first
emitting at least one ``'http.request'`` event.
"""
if exhaust_body is not None:
self._exhaust_body = exhaust_body
self._disconnected = True
async def emit(self) -> Dict[str, Any]:
# NOTE(kgriffs): Special case: if we are immediately disconnected,
# the first event should be 'http.disconnnect'
if self._disconnect_at == 0:
return {'type': EventType.HTTP_DISCONNECT}
#
# NOTE(kgriffs): Based on my reading of the ASGI spec, at least one
# 'http.request' event should be emitted before 'http.disconnect'
# for normal requests. However, the server may choose to
# immediately abandon a connection for some reason, in which case
# an 'http.request' event may never be sent.
#
# See also: https://asgi.readthedocs.io/en/latest/specs/main.html#events
#
if self._body is None or not self._exhaust_body:
# NOTE(kgriffs): When there are no more events, an ASGI
# server will hang until the client connection
# disconnects.
while not self.disconnected:
await asyncio.sleep(0.001)
return {'type': EventType.HTTP_DISCONNECT}
event = {'type': EventType.HTTP_REQUEST} # type: Dict[str, Any]
if self._emit_empty_chunks:
# NOTE(kgriffs): Return a couple variations on empty chunks
# every time, to ensure test coverage.
if not self._emitted_empty_chunk_a:
self._emitted_empty_chunk_a = True
event['more_body'] = True
return event
if not self._emitted_empty_chunk_b:
self._emitted_empty_chunk_b = True
event['more_body'] = True
event['body'] = b''
return event
# NOTE(kgriffs): Part of the time just return an
# empty chunk to make sure the app handles that
# correctly.
if self._toggle_branch('return_empty_chunk'):
event['more_body'] = True
# NOTE(kgriffs): Since ASGI specifies that
# 'body' is optional, we toggle whether
# or not to explicitly set it to b'' to ensure
# the app handles both correctly.
if self._toggle_branch('explicit_empty_body_1'):
event['body'] = b''
return event
chunk = self._body[:self._chunk_size]
self._body = self._body[self._chunk_size:] or None
if chunk:
event['body'] = bytes(chunk)
elif self._toggle_branch('explicit_empty_body_2'):
# NOTE(kgriffs): Since ASGI specifies that
# 'body' is optional, we toggle whether
# or not to explicitly set it to b'' to ensure
# the app handles both correctly.
event['body'] = b''
if self._body:
event['more_body'] = True
elif self._toggle_branch('set_more_body_false'):
# NOTE(kgriffs): The ASGI spec allows leaving off
# the 'more_body' key when it would be set to
# False, so toggle one of the approaches
# to make sure the app handles both cases.
event['more_body'] = False
return event
__call__ = emit
def _toggle_branch(self, name: str):
self._branch_decider[name] = not self._branch_decider[name]
return self._branch_decider[name]
class ASGIResponseEventCollector:
"""Collects and validates ASGI events returned by an app.
Attributes:
events (iterable): An iterable of events that were emitted by
the app, collected as-is from the app.
headers (iterable): An iterable of (str, str) tuples representing
the ISO-8859-1 decoded headers emitted by the app in the body of
the ``'http.response.start'`` event.
status (int): HTTP status code emitted by the app in the body of
the ``'http.response.start'`` event.
body_chunks (iterable): An iterable of ``bytes`` objects emitted
by the app via ``'http.response.body'`` events.
more_body (bool): Whether or not the app expects to emit more
body chunks. Will be ``None`` if unknown (i.e., the app has
not yet emitted any ``'http.response.body'`` events.)
Raises:
TypeError: An event field emitted by the app was of an unexpected type.
ValueError: Invalid event name or field value.
"""
_LIFESPAN_EVENT_TYPES = frozenset([
'lifespan.startup.complete',
'lifespan.startup.failed',
'lifespan.shutdown.complete',
'lifespan.shutdown.failed',
])
_HEADER_NAME_RE = re.compile(br'^[a-zA-Z][a-zA-Z0-9\-_]*$')
_BAD_HEADER_VALUE_RE = re.compile(br'[\000-\037]')
def __init__(self):
self.events = []
self.headers = []
self.status = None
self.body_chunks = []
self.more_body = None
async def collect(self, event: Dict[str, Any]):
if self.more_body is False:
# NOTE(kgriffs): According to the ASGI spec, once we get a
# message setting more_body to False, any further messages
# on the channel are ignored.
return
self.events.append(event)
event_type = event['type']
if not isinstance(event_type, str):
raise TypeError('ASGI event type must be a Unicode string')
if event_type == EventType.HTTP_RESPONSE_START:
for name, value in event.get('headers', []):
if not isinstance(name, bytes):
raise TypeError('ASGI header names must be byte strings')
if not isinstance(value, bytes):
raise TypeError('ASGI header names must be byte strings')
# NOTE(vytas): Ported basic validation from wsgiref.validate.
if not self._HEADER_NAME_RE.match(name):
raise ValueError('Bad header name: {!r}'.format(name))
if self._BAD_HEADER_VALUE_RE.search(value):
raise ValueError('Bad header value: {!r}'.format(value))
# NOTE(vytas): After the name validation above, the name is
# guaranteed to only contain a subset of ASCII.
name_decoded = name.decode()
if not name_decoded.islower():
raise ValueError('ASGI header names must be lowercase')
self.headers.append((name_decoded, value.decode('latin1')))
self.status = event['status']
if not isinstance(self.status, int):
raise TypeError('ASGI status must be an int')
elif event_type == EventType.HTTP_RESPONSE_BODY:
chunk = event.get('body', b'')
if not isinstance(chunk, bytes):
raise TypeError('ASGI body content must be a byte string')
self.body_chunks.append(chunk)
self.more_body = event.get('more_body', False)
if not isinstance(self.more_body, bool):
raise TypeError('ASGI more_body flag must be a bool')
elif event_type not in self._LIFESPAN_EVENT_TYPES:
raise ValueError('Invalid ASGI event type: ' + event_type)
__call__ = collect
_WebSocketState = Enum('_WebSocketState', 'CONNECT HANDSHAKE ACCEPTED DENIED CLOSED')
class ASGIWebSocketSimulator:
"""Simulates a WebSocket client for testing a Falcon ASGI app.
This class provides a way to test WebSocket endpoints in a Falcon ASGI app
without having to interact with an actual ASGI server. While it is certainly
important to test against a real server, a number of functional tests can be
satisfied more efficiently and transparently with a simulated connection.
Note:
The ASGIWebSocketSimulator class is not designed to be instantiated
directly; rather it should be obtained via
:meth:`~falcon.testing.ASGIConductor.simulate_ws`.
Attributes:
ready (bool): ``True`` if the WebSocket connection has been
accepted and the client is still connected, ``False`` otherwise.
closed (bool): ``True`` if the WebSocket connection has been
denied or closed by the app, or the client has disconnected.
close_code (int): The WebSocket close code provided by the app if
the connection is closed, or ``None`` if the connection is open.
subprotocol (str): The subprotocol the app wishes to accept, or
``None`` if not specified.
headers (Iterable[Iterable[bytes]]): An iterable of ``[name, value]``
two-item iterables, where *name* is the header name, and *value* is
the header value for each header returned by the app when
it accepted the WebSocket connection. This property resolves to
``None`` if the connection has not been accepted.
"""
def __init__(self):
self.__msgpack = None
self._state = _WebSocketState.CONNECT
self._disconnect_emitted = False
self._close_code = None
self._accepted_subprotocol = None
self._accepted_headers = None
self._collected_server_events = deque()
self._collected_client_events = deque()
self._event_handshake_complete = asyncio.Event()
@property
def ready(self) -> bool:
return self._state == _WebSocketState.ACCEPTED
@property
def closed(self) -> bool:
return self._state in {_WebSocketState.DENIED, _WebSocketState.CLOSED}
@property
def close_code(self) -> int:
return self._close_code
@property
def subprotocol(self) -> str:
return self._accepted_subprotocol
@property
def headers(self) -> Iterable[Iterable[bytes]]:
return self._accepted_headers
async def wait_ready(self, timeout: Optional[int] = 5):
"""Wait until the connection has been accepted or denied.
This coroutine can be awaited in order to pause execution until the
app has accepted or denied the connection. In the latter case, an
error will be raised to the caller.
Keyword Args:
timeout (int): Number of seconds to wait before giving up and
raising an error (default: ``5``).
"""
try:
await asyncio.wait_for(self._event_handshake_complete.wait(), timeout)
except asyncio.TimeoutError:
msg = (
'Timed out after waiting {} seconds for '
'the WebSocket handshake to complete. Check the '
'on_websocket responder and '
'any middleware for any conditions that may be stalling the '
'request flow.'
).format(timeout)
raise asyncio.TimeoutError(msg)
self._require_accepted()
# NOTE(kgriffs): This is a coroutine just in case we need it to be
# in a future code revision. It also makes it more consistent
# with the other methods.
async def close(self, code: Optional[int] = None):
"""Close the simulated connection.
Keyword Args:
code (int): The WebSocket close code to send to the application
per the WebSocket spec (default: ``1000``).
"""
# NOTE(kgriffs): Give our collector a chance in case the
# server is trying to close at the same time (e.g., there was an
# unhandled error and the server wants to disconnect with an error
# code.) We await a few times to let the server app settle across
# multiple of its own await's.
for __ in range(3):
await asyncio.sleep(0)
if self.closed:
return
assert self._close_code is None
if code is None:
code = WSCloseCode.NORMAL
self._state = _WebSocketState.CLOSED
self._close_code = code
async def send_text(self, payload: str):
"""Send a message to the app with a Unicode string payload.
Arguments:
payload (str): The string to send.
"""
# NOTE(kgriffs): We have to check ourselves because some ASGI
# servers are not very strict which can lead to hard-to-debug
# errors.
if not isinstance(payload, str):
raise TypeError('payload must be a string')
# NOTE(kgriffs): From the client's perspective, it was a send,
# but the server will be expecting websocket.receive
await self._send(text=payload)
async def send_data(self, payload: Union[bytes, bytearray, memoryview]):
"""Send a message to the app with a binary data payload.
Arguments:
payload (Union[bytes, bytearray, memoryview]): The binary data to send.
"""
# NOTE(kgriffs): We have to check ourselves because some ASGI
# servers are not very strict which can lead to hard-to-debug
# errors.
if not isinstance(payload, (bytes, bytearray, memoryview)):
raise TypeError('payload must be a byte string')
# NOTE(kgriffs): From the client's perspective, it was a send,
# but the server will be expecting websocket.receive
await self._send(data=bytes(payload))
async def send_json(self, media: object):
"""Send a message to the app with a JSON-encoded payload.
Arguments:
media: A JSON-encodable object to send as a TEXT (0x01) payload.
"""
text = json.dumps(media)
await self.send_text(text)
async def send_msgpack(self, media: object):
"""Send a message to the app with a MessagePack-encoded payload.
Arguments:
media: A MessagePack-encodable object to send as a BINARY (0x02) payload.
"""
data = self._msgpack.packb(media, use_bin_type=True)
await self.send_data(data)
async def receive_text(self) -> str:
"""Receive a message from the app with a Unicode string payload.
Awaiting this coroutine will block until a message is available or
the WebSocket is disconnected.
"""
event = await self._receive()
# PERF(kgriffs): When we normally expect the key to be
# present, this pattern is faster than get()
try:
text = event['text']
except KeyError:
text = None
# NOTE(kgriffs): Even if the key is present, it may be None
if text is None:
raise falcon_errors.PayloadTypeError('Expected TEXT payload but got BINARY instead')
return text
async def receive_data(self) -> bytes:
"""Receive a message from the app with a binary data payload.
Awaiting this coroutine will block until a message is available or
the WebSocket is disconnected.
"""
event = await self._receive()
# PERF(kgriffs): When we normally expect the key to be
# present, EAFP is faster than get()
try:
data = event['bytes']
except KeyError:
data = None
# NOTE(kgriffs): Even if the key is present, it may be None
if data is None:
raise falcon_errors.PayloadTypeError('Expected BINARY payload but got TEXT instead')
return data
async def receive_json(self) -> object:
"""Receive a message from the app with a JSON-encoded TEXT payload.
Awaiting this coroutine will block until a message is available or
the WebSocket is disconnected.
"""
text = await self.receive_text()
return json.loads(text)
async def receive_msgpack(self) -> object:
"""Receive a message from the app with a MessagePack-encoded BINARY payload.
Awaiting this coroutine will block until a message is available or
the WebSocket is disconnected.
"""
data = await self.receive_data()
return self._msgpack.unpackb(data, use_list=True, raw=False)
@property
def _msgpack(self):
# NOTE(kgriffs): A property is used in lieu of referencing
# the msgpack module directly, in order to bubble up the
# import error in an obvious way, when the package has
# not been installed.
if not self.__msgpack:
import msgpack
self.__msgpack = msgpack
return self.__msgpack
def _require_accepted(self):
if self._state == _WebSocketState.ACCEPTED:
return
if self._state in {_WebSocketState.CONNECT, _WebSocketState.HANDSHAKE}:
raise falcon_errors.OperationNotAllowed(
'WebSocket connection has not yet been accepted'
)
elif self._state == _WebSocketState.CLOSED:
raise falcon_errors.WebSocketDisconnected(self._close_code)
assert self._state == _WebSocketState.DENIED
if self._close_code == WSCloseCode.PATH_NOT_FOUND:
raise falcon_errors.WebSocketPathNotFound(WSCloseCode.PATH_NOT_FOUND)
if self._close_code == WSCloseCode.SERVER_ERROR:
raise falcon_errors.WebSocketServerError(WSCloseCode.SERVER_ERROR)
if self._close_code == WSCloseCode.HANDLER_NOT_FOUND:
raise falcon_errors.WebSocketHandlerNotFound(WSCloseCode.HANDLER_NOT_FOUND)
raise falcon_errors.WebSocketDisconnected(self._close_code)
# NOTE(kgriffs): This is a coroutine just in case we need it to be
# in a future code revision. It also makes it more consistent
# with the other methods.
async def _send(self, data: bytes = None, text: str = None):
self._require_accepted()
# NOTE(kgriffs): From the client's perspective, it was a send,
# but the server will be expecting websocket.receive
event = {'type': EventType.WS_RECEIVE} # type: Dict[str, Union[bytes, str]]
if data is not None:
event['bytes'] = data
if text is not None:
event['text'] = text
self._collected_client_events.append(event)
# NOTE(kgriffs): If something is waiting to read this data on the
# other side, give it a chance to progress (because we like to party
# like it's 1992.)
await asyncio.sleep(0)
async def _receive(self) -> Dict[str, Any]:
while not self._collected_server_events:
self._require_accepted()
await asyncio.sleep(0)
self._require_accepted()
return self._collected_server_events.popleft()
async def _emit(self) -> Dict[str, Any]:
if self._state == _WebSocketState.CONNECT:
self._state = _WebSocketState.HANDSHAKE
return {'type': EventType.WS_CONNECT}
if self._state == _WebSocketState.HANDSHAKE:
# NOTE(kgriffs): We need to wait for the handshake to
# complete, before proceeding.
await self._event_handshake_complete.wait()
while not self._collected_client_events:
await asyncio.sleep(0)
if self.closed:
return self._create_checked_disconnect()
return self._collected_client_events.popleft()
async def _collect(self, event: Dict[str, Any]):
assert event
if self._state == _WebSocketState.CONNECT:
raise falcon_errors.OperationNotAllowed(
'An ASGI application must receive the first websocket.connect '
'event before attempting to send any events.'
)
event_type = event['type']
if self._state == _WebSocketState.HANDSHAKE:
if event_type == EventType.WS_ACCEPT:
self._state = _WebSocketState.ACCEPTED
self._accepted_subprotocol = event.get('subprotocol')
self._accepted_headers = event.get('headers')
self._event_handshake_complete.set()
# NOTE(kgriffs): Yield to other pending tasks that may be
# waiting on the completion of the handshake. This ensures
# that the simulated client connection can enter its context
# before the app logic continues and potentially closes the
# connection from that side.
await asyncio.sleep(0)
elif event_type == EventType.WS_CLOSE:
self._state = _WebSocketState.DENIED
desired_code = event.get('code', WSCloseCode.NORMAL)
if desired_code == WSCloseCode.SERVER_ERROR or (3000 <= desired_code < 4000):
# NOTE(kgriffs): Pass this code through since it is a
# special code we have set in the framework to trigger
# different raised error types or to pass through a
# raised HTTPError status code.
self._close_code = desired_code
else:
# NOTE(kgriffs): Force the close code to this since it is
# similar to what happens with a real web server (the HTTP
# connection is closed with a 403 and there is no websocket
# close code).
self._close_code = WSCloseCode.FORBIDDEN
self._event_handshake_complete.set()
else:
raise falcon_errors.OperationNotAllowed(
'An ASGI application must send either websocket.accept or '
'websocket.close before sending any other event types (got '
'{0})'.format(event_type)
)
elif self._state == _WebSocketState.ACCEPTED:
if event_type == EventType.WS_CLOSE:
self._state = _WebSocketState.CLOSED
self._close_code = event.get('code', WSCloseCode.NORMAL)
else:
assert event_type == EventType.WS_SEND
self._collected_server_events.append(event)
else:
assert self.closed
# NOTE(kgriffs): According to the ASGI spec, we are
# supposed to just silently eat events once the
# socket is disconnected.
pass
# NOTE(kgriffs): Give whatever is waiting on the handshake or a
# collected data/text event a chance to progress.
await asyncio.sleep(0)
def _create_checked_disconnect(self) -> Dict[str, Any]:
if self._disconnect_emitted:
raise falcon_errors.OperationNotAllowed(
'The websocket.disconnect event has already been emitted, '
'and so the app should not attempt to receive any more '
'events, since ASGI servers will likely block indefinitely '
'rather than re-emitting websocket.disconnect events.'
)
self._disconnect_emitted = True
return {'type': EventType.WS_DISCONNECT, 'code': self._close_code}
# get_encoding_from_headers() is Copyright 2016 Kenneth Reitz, and is
# used here under the terms of the Apache License, Version 2.0.
def get_encoding_from_headers(headers):
"""Return encoding from given HTTP Header Dict.
Args:
headers(dict): Dictionary from which to extract encoding. Header
names must either be lowercase or the dict must support
case-insensitive lookups.
"""
content_type = headers.get('content-type')
if not content_type:
return None
content_type, params = cgi.parse_header(content_type)
if 'charset' in params:
return params['charset'].strip("'\"")
# NOTE(kgriffs): Added checks for text/event-stream and application/json
if content_type in ('text/event-stream', 'application/json'):
return 'UTF-8'
if 'text' in content_type:
return 'ISO-8859-1'
return None
def get_unused_port() -> int:
"""Get an unused localhost port for use by a test server.
Warning:
It is possible for a third party to bind to the returned port
before the caller is able to do so. The caller will need to
retry with a different port in that case.
Warning:
This method has only be tested on POSIX systems and may not
work elsewhere.
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('localhost', 0))
return s.getsockname()[1]
def rand_string(min, max) -> str:
"""Return a randomly-generated string, of a random length.
Args:
min (int): Minimum string length to return, inclusive
max (int): Maximum string length to return, inclusive
"""
int_gen = random.randint
string_length = int_gen(min, max)
return ''.join([chr(int_gen(ord(' '), ord('~')))
for __ in range(string_length)])
def create_scope(path='/', query_string='', method='GET', headers=None,
host=DEFAULT_HOST, scheme=None, port=None, http_version='1.1',
remote_addr=None, root_path=None, content_length=None,
include_server=True, cookies=None) -> Dict[str, Any]:
"""Create a mock ASGI scope ``dict`` for simulating HTTP requests.
Keyword Args:
path (str): The path for the request (default ``'/'``)
query_string (str): The query string to simulate, without a
leading ``'?'`` (default ``''``). The query string is passed as-is
(it will not be percent-encoded).
method (str): The HTTP method to use (default ``'GET'``)
headers (dict): Headers as a dict-like (Mapping) object, or an
iterable yielding a series of two-member (*name*, *value*)
iterables. Each pair of strings provides the name and value
for an HTTP header. If desired, multiple header values may be
combined into a single (*name*, *value*) pair by joining the values
with a comma when the header in question supports the list
format (see also RFC 7230 and RFC 7231). When the
request will include a body, the Content-Length header should be
included in this list. Header names are not case-sensitive.
Note:
If a User-Agent header is not provided, it will default to::
f'falcon-client/{falcon.__version__}'
host(str): Hostname for the request (default ``'falconframework.org'``).
This also determines the the value of the Host header in the
request.
scheme (str): URL scheme, either ``'http'`` or ``'https'``
(default ``'http'``)
port (int): The TCP port to simulate. Defaults to
the standard port used by the given scheme (i.e., 80 for ``'http'``
and 443 for ``'https'``). A string may also be passed, as long as
it can be parsed as an int.
http_version (str): The HTTP version to simulate. Must be either
``'2'``, ``'2.0'``, ``'1.1'``, ``'1.0'``, or ``'1'``
(default ``'1.1'``). If set to ``'1.0'``, the Host header will not
be added to the scope.
remote_addr (str): Remote address for the request to use for
the 'client' field in the connection scope (default None)
root_path (str): The root path this application is mounted at; same as
SCRIPT_NAME in WSGI (default ``''``).
content_length (int): The expected content length of the request
body (default ``None``). If specified, this value will be
used to set the Content-Length header in the request.
include_server (bool): Set to ``False`` to not set the 'server' key
in the scope ``dict`` (default ``True``).
cookies (dict): Cookies as a dict-like (Mapping) object, or an
iterable yielding a series of two-member (*name*, *value*)
iterables. Each pair of items provides the name and value
for the 'Set-Cookie' header.
"""
http_version = _fixup_http_version(http_version)
path = uri.decode(path, unquote_plus=False)
# NOTE(kgriffs): Handles both None and ''
query_string = query_string.encode() if query_string else b''
if query_string and query_string.startswith(b'?'):
raise ValueError("query_string should not start with '?'")
scope = {
'type': ScopeType.HTTP,
'asgi': {
'version': '3.0',
'spec_version': '2.1',
},
'http_version': http_version,
'method': method.upper(),
'path': path,
'query_string': query_string,
}
# NOTE(kgriffs): Explicitly test against None so that the caller
# is able to simulate setting app to an empty string if they
# need to cover that branch in their code.
if root_path is not None:
# NOTE(kgriffs): Judging by the algorithm given in PEP-3333 for
# reconstructing the URL, SCRIPT_NAME is expected to contain a
# preceding slash character. Since ASGI states that this value is
# the same as WSGI's SCRIPT_NAME, we will follow suit here.
if root_path and not root_path.startswith('/'):
scope['root_path'] = '/' + root_path
else:
scope['root_path'] = root_path
if scheme:
if scheme not in {'http', 'https', 'ws', 'wss'}:
raise ValueError("scheme must be either 'http', 'https', 'ws', or 'wss'")
scope['scheme'] = scheme
if port is None:
if (scheme or 'http') in {'http', 'ws'}:
port = 80
else:
port = 443
else:
port = int(port)
if remote_addr:
# NOTE(kgriffs): Choose from the standard IANA dynamic range
remote_port = random.randint(49152, 65535)
# NOTE(kgriffs): Expose as an iterable to ensure the framework/app
# isn't hard-coded to only work with a list or tuple.
scope['client'] = iter([remote_addr, remote_port])
if include_server:
scope['server'] = iter([host, port])
# NOTE(myusko): Clients discard Set-Cookie header
# in the response to the OPTIONS method.
if method == 'OPTIONS' and cookies is not None:
cookies = None
_add_headers_to_scope(scope, headers, content_length, host,
port, scheme, http_version, cookies)
return scope
def create_scope_ws(path='/', query_string='', headers=None,
host=DEFAULT_HOST, scheme=None, port=None, http_version='1.1',
remote_addr=None, root_path=None, include_server=True,
subprotocols=None, spec_version='2.1') -> Dict[str, Any]:
"""Create a mock ASGI scope ``dict`` for simulating WebSocket requests.
Keyword Args:
path (str): The path for the request (default ``'/'``)
query_string (str): The query string to simulate, without a
leading ``'?'`` (default ``''``). The query string is passed as-is
(it will not be percent-encoded).
headers (dict): Headers as a dict-like (Mapping) object, or an
iterable yielding a series of two-member (*name*, *value*)
iterables. Each pair of strings provides the name and value
for an HTTP header. If desired, multiple header values may be
combined into a single (*name*, *value*) pair by joining the values
with a comma when the header in question supports the list
format (see also RFC 7230 and RFC 7231). When the
request will include a body, the Content-Length header should be
included in this list. Header names are not case-sensitive.
Note:
If a User-Agent header is not provided, it will default to::
f'falcon-client/{falcon.__version__}'
host(str): Hostname for the request (default ``'falconframework.org'``).
This also determines the the value of the Host header in the
request.
scheme (str): URL scheme, either ``'ws'`` or ``'wss'``
(default ``'ws'``)
port (int): The TCP port to simulate. Defaults to