def test_to_bytestring(): assert util.to_bytestring('test_str', 'ascii') == b'test_str' assert util.to_bytestring('test_str®') == b'test_str\xc2\xae' assert util.to_bytestring(b'byte_test_str') == b'byte_test_str' with pytest.raises(TypeError) as err: util.to_bytestring(100) msg = '100 is not a string' assert msg in str(err)
def test_http_header_encoding(): """ tests whether http response headers are USASCII encoded """ mocked_socket = mock.MagicMock() mocked_socket.sendall = mock.MagicMock() mocked_request = mock.MagicMock() response = Response(mocked_request, mocked_socket, None) # set umlaut header response.headers.append(('foo', 'häder')) try: response.send_headers() except Exception as e: assert isinstance(e, UnicodeEncodeError) # build our own header_str to compare against tosend = response.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers]) header_str = "%s\r\n" % "".join(tosend) try: mocked_socket.sendall(util.to_bytestring(header_str,"ascii")) except Exception as e: assert isinstance(e, UnicodeEncodeError)
def test_http_header_encoding(): """ tests whether http response headers are USASCII encoded """ mocked_socket = mock.MagicMock() mocked_socket.sendall = mock.MagicMock() mocked_request = mock.MagicMock() response = Response(mocked_request, mocked_socket, None) # set umlaut header value - latin-1 is OK response.headers.append(('foo', 'häder')) response.send_headers() # set a-breve header value - unicode, non-latin-1 fails response = Response(mocked_request, mocked_socket, None) response.headers.append(('apple', 'măr')) with pytest.raises(UnicodeEncodeError): response.send_headers() # build our own header_str to compare against tosend = response.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers]) header_str = "%s\r\n" % "".join(tosend) with pytest.raises(UnicodeEncodeError): mocked_socket.sendall(util.to_bytestring(header_str, "ascii"))
def send_headers(self): if self.headers_sent: return tosend = self.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers]) header_str = "%s\r\n" % "".join(tosend) util.write(self.sock, util.to_bytestring(header_str, "ascii")) self.headers_sent = True
def send_headers(self): if self.headers_sent: return tosend = self.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers]) header_str = "%s\r\n" % "".join(tosend) util.write(self.sock, util.to_bytestring(header_str)) self.headers_sent = True
def address(self): bind = self.settings['bind'].get() return util.parse_address(util.to_bytestring(bind))
def address(self): if not self.conf['bind']: raise RuntimeError("Listener address is not set") return util.parse_address(util.to_bytestring(self.conf['bind']))