def test_http_response_parser_bad_status_line_eof(self): p = protocol.http_response_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(errors.BadStatusLine, p.throw, tulip.EofStream())
def test_http_response_parser_bad_status_line(self): p = protocol.http_response_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(errors.BadStatusLine, p.send, b'\r\n\r\n')
def test_parse_frame_header_payload_size(self): buf = tulip.ParserBuffer() p = websocket.parse_frame(buf) next(p) self.assertRaises( websocket.WebSocketError, p.send, struct.pack('!BB', 0b10001000, 0b01111110))
def test_parse_chunked_payload_incomplete(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = protocol.parse_chunked_payload(out, buf) next(p) p.send(b'4\r\ndata\r\n') self.assertRaises(errors.IncompleteRead, p.throw, tulip.EofStream)
def test_parser(self, m_parse_message): cur = 0 def parse_message(buf): nonlocal cur yield if cur == 0: cur = 1 return websocket.Message(websocket.OPCODE_TEXT, b'line1', b'') else: return websocket.Message(websocket.OPCODE_CLOSE, b'', b'') m_parse_message.side_effect = parse_message out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = websocket.WebSocketParser() next(p) p.send((out, buf)) p.send(b'') self.assertRaises(StopIteration, p.send, b'') self.assertEqual( (websocket.OPCODE_TEXT, b'line1', b''), out._buffer[0]) self.assertEqual( (websocket.OPCODE_CLOSE, b'', b''), out._buffer[1]) self.assertTrue(out._eof)
def test_parse_length_payload_eof(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = protocol.parse_length_payload(out, buf, 4) next(p) p.send(b'da') self.assertRaises(errors.IncompleteRead, p.throw, tulip.EofStream)
def test_parse_frame_header_continuation(self): buf = tulip.ParserBuffer() p = websocket.parse_frame(buf) next(p) self.assertRaises( websocket.WebSocketError, p.send, struct.pack('!BB', 0b00000000, 0b00000000))
def test_http_request_parser_bad_method(self): p = protocol.http_request_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(errors.BadStatusLine, p.send, b'!12%()+=~$ /get HTTP/1.1\r\n\r\n')
def test_parser_eof(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = websocket.WebSocketParser() next(p) p.send((out, buf)) self.assertRaises(tulip.EofStream, p.throw, tulip.EofStream) self.assertEqual([], list(out._buffer))
def test_http_request_parser_bad_version(self): p = protocol.http_request_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(errors.BadStatusLine, p.send, b'GET //get HT/11\r\n\r\n')
def test_http_response_parser_code_not_int(self): p = protocol.http_response_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) with self.assertRaises(errors.BadStatusLine) as cm: p.send(b'HTTP/1.1 ttt test\r\n\r\n') self.assertIn('HTTP/1.1 ttt test', str(cm.exception))
def test_close_frame_invalid(self, m_parse_frame): def parse_frame(buf): yield return (1, websocket.OPCODE_CLOSE, b'1') m_parse_frame.side_effect = parse_frame buf = tulip.ParserBuffer() p = websocket.parse_message(buf) next(p) self.assertRaises(websocket.WebSocketError, p.send, b'')
def test_unknown_frame(self, m_parse_frame): def parse_frame(buf): yield return (1, websocket.OPCODE_CONTINUATION, b'') m_parse_frame.side_effect = parse_frame buf = tulip.ParserBuffer() p = websocket.parse_message(buf) next(p) self.assertRaises(websocket.WebSocketError, p.send, b'')
def test_http_payload_parser_length_zero(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('CONTENT-LENGTH', '0')], None, None) p = protocol.http_payload_parser(msg) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() self.assertRaises(StopIteration, p.send, (out, buf)) self.assertEqual(b'', b''.join(out._buffer))
def test_http_response_parser_bad_version(self): p = protocol.http_response_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) with self.assertRaises(errors.BadStatusLine) as cm: p.send(b'HT/11 200 Ok\r\n\r\n') self.assertEqual('HT/11 200 Ok\r\n', cm.exception.args[0])
def test_http_payload_parser_length_wrong(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('CONTENT-LENGTH', '-1')], None, None) p = protocol.http_payload_parser(msg) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() self.assertRaises(errors.InvalidHeader, p.send, (out, buf))
def test_http_payload_parser_no_length(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None) p = protocol.http_payload_parser(msg, readall=False) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() self.assertRaises(StopIteration, p.send, (out, buf)) self.assertEqual(b'', b''.join(out._buffer)) self.assertTrue(out._eof)
def test_parse_frame_length0(self): buf = tulip.ParserBuffer() p = websocket.parse_frame(buf) next(p) try: p.send(struct.pack('!BB', 0b00000001, 0b00000000)) except StopIteration as exc: fin, opcode, payload = exc.value self.assertEqual((0, 1, b''), (fin, opcode, payload))
def test_http_request_parser_max_headers(self): p = protocol.http_request_parser(8190, 20, 8190) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises( errors.LineTooLong, p.send, b'get /path HTTP/1.1\r\ntest: line\r\ntest2: data\r\n\r\n')
def test_parse_chunked_payload_extension(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = protocol.parse_chunked_payload(out, buf) next(p) try: p.send(b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') except StopIteration: pass self.assertEqual(b'dataline', b''.join(out._buffer))
def test_http_payload_parser_eof(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None) p = protocol.http_payload_parser(msg, readall=True) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) p.send(b'data') p.send(b'line') self.assertRaises(tulip.EofStream, p.throw, tulip.EofStream()) self.assertEqual(b'dataline', b''.join(out._buffer))
def test_http_payload_parser_websocket(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('SEC-WEBSOCKET-KEY1', '13')], None, None) p = protocol.http_payload_parser(msg) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(StopIteration, p.send, b'1234567890') self.assertEqual(b'12345678', b''.join(out._buffer))
def test_parse_eof_payload(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = protocol.parse_eof_payload(out, buf) next(p) p.send(b'data') try: p.throw(tulip.EofStream()) except tulip.EofStream: pass self.assertEqual([b'data'], list(out._buffer))
def test_parse_chunked_payload_chunks(self): out = tulip.DataBuffer() buf = tulip.ParserBuffer() p = protocol.parse_chunked_payload(out, buf) next(p) p.send(b'4\r\ndata\r') p.send(b'\n4') p.send(b'\r') p.send(b'\n') p.send(b'line\r\n0\r\n') self.assertRaises(StopIteration, p.send, b'test\r\n') self.assertEqual(b'dataline', b''.join(out._buffer))
def test_http_request_parser_two_slashes(self): p = protocol.http_request_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) try: p.send(b'get //path HTTP/1.1\r\n\r\n') except StopIteration: pass self.assertEqual(('GET', '//path', (1, 1), deque(), False, None), out._buffer[0])
def test_http_payload_parser_deflate_disabled(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), [('CONTENT-LENGTH', len(self._COMPRESSED))], None, 'deflate') p = protocol.http_payload_parser(msg, compression=False) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(StopIteration, p.send, self._COMPRESSED) self.assertEqual(self._COMPRESSED, b''.join(out._buffer))
def test_http_payload_parser_chunked(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('TRANSFER-ENCODING', 'chunked')], None, None) p = protocol.http_payload_parser(msg) next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) self.assertRaises(StopIteration, p.send, b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') self.assertEqual(b'dataline', b''.join(out._buffer))
def test_http_request_parser_eof(self): # http_request_parser does not fail on EofStream() p = protocol.http_request_parser() next(p) out = tulip.DataBuffer() buf = tulip.ParserBuffer() p.send((out, buf)) p.send(b'get /path HTTP/1.1\r\n') try: p.throw(tulip.EofStream()) except StopIteration: pass self.assertFalse(out._buffer)
def test_simple_binary(self, m_parse_frame): def parse_frame(buf): yield return (1, websocket.OPCODE_BINARY, b'binary') m_parse_frame.side_effect = parse_frame buf = tulip.ParserBuffer() p = websocket.parse_message(buf) next(p) try: p.send(b'') except StopIteration as exc: res = exc.value self.assertEqual(res, (websocket.OPCODE_BINARY, b'binary', ''))
def test_close_frame_info(self, m_parse_frame): def parse_frame(buf): yield return (1, websocket.OPCODE_CLOSE, b'0112345') m_parse_frame.side_effect = parse_frame buf = tulip.ParserBuffer() p = websocket.parse_message(buf) next(p) try: p.send(b'') except StopIteration as exc: res = exc.value self.assertEqual(res, (websocket.OPCODE_CLOSE, 12337, b'12345'))