|
2 | 2 | from hyper.packages.hyperframe.frame import (
|
3 | 3 | Frame, DataFrame, RstStreamFrame, SettingsFrame,
|
4 | 4 | PushPromiseFrame, PingFrame, WindowUpdateFrame, HeadersFrame,
|
5 |
| - ContinuationFrame, BlockedFrame, |
| 5 | + ContinuationFrame, BlockedFrame, GoAwayFrame, |
6 | 6 | )
|
7 | 7 | from hyper.packages.hpack.hpack_compat import Encoder, Decoder
|
8 | 8 | from hyper.http20.connection import HTTP20Connection
|
|
11 | 11 | )
|
12 | 12 | from hyper.http20.response import HTTP20Response, HTTP20Push
|
13 | 13 | from hyper.http20.exceptions import (
|
14 |
| - HPACKDecodingError, HPACKEncodingError, ProtocolError |
| 14 | + HPACKDecodingError, HPACKEncodingError, ProtocolError, ConnectionError, |
15 | 15 | )
|
16 | 16 | from hyper.http20.window import FlowControlManager
|
17 | 17 | from hyper.http20.util import (
|
|
20 | 20 | from hyper.common.headers import HTTPHeaderMap
|
21 | 21 | from hyper.compat import zlib_compressobj
|
22 | 22 | from hyper.contrib import HTTP20Adapter
|
| 23 | +import hyper.http20.errors as errors |
23 | 24 | import errno
|
24 | 25 | import os
|
25 | 26 | import pytest
|
@@ -1183,6 +1184,82 @@ def test_stripping_multiple_connection_headers(self):
|
1183 | 1184 |
|
1184 | 1185 | assert h2_safe_headers(headers) == stripped
|
1185 | 1186 |
|
| 1187 | + def test_goaway_frame_PROTOCOL_ERROR(self): |
| 1188 | + f = GoAwayFrame(0) |
| 1189 | + # Set error code to PROTOCOL_ERROR |
| 1190 | + f.error_code = 1; |
| 1191 | + |
| 1192 | + c = HTTP20Connection('www.google.com') |
| 1193 | + c._sock = DummySocket() |
| 1194 | + |
| 1195 | + # 'Receive' the GOAWAY frame. |
| 1196 | + # Validate that the spec error name and description are used to throw |
| 1197 | + # the connection exception. |
| 1198 | + with pytest.raises(ConnectionError) as conn_err: |
| 1199 | + c.receive_frame(f) |
| 1200 | + |
| 1201 | + err_msg = str(conn_err) |
| 1202 | + name, number, description = errors.get_data(1) |
| 1203 | + |
| 1204 | + assert name in err_msg |
| 1205 | + assert number in err_msg |
| 1206 | + assert description in err_msg |
| 1207 | + |
| 1208 | + def test_goaway_frame_HTTP_1_1_REQUIRED(self): |
| 1209 | + f = GoAwayFrame(0) |
| 1210 | + # Set error code to HTTP_1_1_REQUIRED |
| 1211 | + f.error_code = 13; |
| 1212 | + |
| 1213 | + c = HTTP20Connection('www.google.com') |
| 1214 | + c._sock = DummySocket() |
| 1215 | + |
| 1216 | + # 'Receive' the GOAWAY frame. |
| 1217 | + # Validate that the spec error name and description are used to throw |
| 1218 | + # the connection exception. |
| 1219 | + with pytest.raises(ConnectionError) as conn_err: |
| 1220 | + c.receive_frame(f) |
| 1221 | + |
| 1222 | + err_msg = str(conn_err) |
| 1223 | + name, number, description = errors.get_data(13) |
| 1224 | + |
| 1225 | + assert name in err_msg |
| 1226 | + assert number in err_msg |
| 1227 | + assert description in err_msg |
| 1228 | + |
| 1229 | + def test_goaway_frame_NO_ERROR(self): |
| 1230 | + f = GoAwayFrame(0) |
| 1231 | + # Set error code to NO_ERROR |
| 1232 | + f.error_code = 0; |
| 1233 | + |
| 1234 | + c = HTTP20Connection('www.google.com') |
| 1235 | + c._sock = DummySocket() |
| 1236 | + |
| 1237 | + # 'Receive' the GOAWAY frame. |
| 1238 | + # Test makes sure no exception is raised; error code 0 means we are |
| 1239 | + # dealing with a standard and graceful shutdown. |
| 1240 | + c.receive_frame(f) |
| 1241 | + |
| 1242 | + def test_goaway_frame_invalid_error_code(self): |
| 1243 | + f = GoAwayFrame(0) |
| 1244 | + # Set error code to non existing error |
| 1245 | + f.error_code = 100; |
| 1246 | + f.additional_data = 'data about non existing error code'; |
| 1247 | + |
| 1248 | + c = HTTP20Connection('www.google.com') |
| 1249 | + c._sock = DummySocket() |
| 1250 | + |
| 1251 | + # 'Receive' the GOAWAY frame. |
| 1252 | + # If the error code does not exist in the spec then the additional |
| 1253 | + # data is used instead. |
| 1254 | + with pytest.raises(ConnectionError) as conn_err: |
| 1255 | + c.receive_frame(f) |
| 1256 | + |
| 1257 | + err_msg = str(conn_err) |
| 1258 | + with pytest.raises(ValueError): |
| 1259 | + name, number, description = errors.get_data(100) |
| 1260 | + |
| 1261 | + assert 'data about non existing error code' in err_msg |
| 1262 | + assert str(f.error_code) in err_msg |
1186 | 1263 |
|
1187 | 1264 | # Some utility classes for the tests.
|
1188 | 1265 | class NullEncoder(object):
|
|
0 commit comments