Debian Codegraph @

testing/web-platform/tests/tools/third_party/h2/test/test_interacting_stacks.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_interacting_stacks
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 These tests run two entities, a client and a server, in parallel threads. These
7 two entities talk to each other, running what amounts to a number of carefully
8 controlled simulations of real flows.
9
10 This is to ensure that the stack as a whole behaves intelligently in both
11 client and server cases.
12
13 These tests are long, complex, and somewhat brittle, so they aren't in general
14 recommended for writing the majority of test cases. Their purposes is primarily
15 to validate that the top-level API of the library behaves as described.
16
17 We should also consider writing helper functions to reduce the complexity of
18 these tests, so that they can be written more easily, as they are remarkably
19 useful.
20 """
21 from . import coroutine_tests
22
23 import h2.config
24 import h2.connection
25 import h2.events
26 import h2.settings
27
28
29 class TestCommunication(coroutine_tests.CoroutineTestCase):
30 """
31 Test that two communicating state machines can work together.
32 """
33 server_config = h2.config.H2Configuration(client_side=False)
34
35 def test_basic_request_response(self):
36 """
37 A request issued by hyper-h2 can be responded to by hyper-h2.
38 """
39 request_headers = [
40 (b':method', b'GET'),
41 (b':path', b'/'),
42 (b':authority', b'example.com'),
43 (b':scheme', b'https'),
44 (b'user-agent', b'test-client/0.1.0'),
45 ]
46 response_headers = [
47 (b':status', b'204'),
48 (b'server', b'test-server/0.1.0'),
49 (b'content-length', b'0'),
50 ]
51
52 def client():
53 c = h2.connection.H2Connection()
54
55 # Do the handshake. First send the preamble.
56 c.initiate_connection()
57 data = yield c.data_to_send()
58
59 # Next, handle the remote preamble.
60 events = c.receive_data(data)
61 assert len(events) == 2
62 assert isinstance(events[0], h2.events.SettingsAcknowledged)
63 assert isinstance(events[1], h2.events.RemoteSettingsChanged)
64 changed = events[1].changed_settings
65 assert (
66 changed[
67 h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS
68 ].new_value == 100
69 )
70
71 # Send a request.
72 events = c.send_headers(1, request_headers, end_stream=True)
73 assert not events
74 data = yield c.data_to_send()
75
76 # Validate the response.
77 events = c.receive_data(data)
78 assert len(events) == 2
79 assert isinstance(events[0], h2.events.ResponseReceived)
80 assert events[0].stream_id == 1
81 assert events[0].headers == response_headers
82 assert isinstance(events[1], h2.events.StreamEnded)
83 assert events[1].stream_id == 1
84
85 @self.server
86 def server():
87 c = h2.connection.H2Connection(config=self.server_config)
88
89 # First, read for the preamble.
90 data = yield
91 events = c.receive_data(data)
92 assert len(events) == 1
93 assert isinstance(events[0], h2.events.RemoteSettingsChanged)
94 changed = events[0].changed_settings
95 assert (
96 changed[
97 h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS
98 ].new_value == 100
99 )
100
101 # Send our preamble back.
102 c.initiate_connection()
103 data = yield c.data_to_send()
104
105 # Listen for the request.
106 events = c.receive_data(data)
107 assert len(events) == 3
108 assert isinstance(events[0], h2.events.SettingsAcknowledged)
109 assert isinstance(events[1], h2.events.RequestReceived)
110 assert events[1].stream_id == 1
111 assert events[1].headers == request_headers
112 assert isinstance(events[2], h2.events.StreamEnded)
113 assert events[2].stream_id == 1
114
115 # Send our response.
116 events = c.send_headers(1, response_headers, end_stream=True)
117 assert not events
118 yield c.data_to_send()
119
120 self.run_until_complete(client(), server())