Skip to content

Commit 9c2f297

Browse files
committed
Reduce mocking in test_reject_open_redirect for compat
This is a followup for change Ie36401c782f023d1d5f2623732619105dc2cfa24 to reduce mocking in the unit test coverage for it. While backporting the bug fix, it was found to be incompatible with earlier versions of Python < 3.6 due to a difference in internal implementation [1]. This reduces the mocking in the unit test to be more agnostic to the internals of the StreamRequestHandler (ancestor of SimpleHTTPRequestHandler) and work across Python versions >= 2.7. Related-Bug: #1927677 [1] python/cpython@34eeed4 Change-Id: I546d376869a992601b443fb95acf1034da2a8f36 (cherry picked from commit 214cabe)
1 parent b0099aa commit 9c2f297

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

nova/tests/unit/console/test_websocketproxy.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Tests for nova websocketproxy."""
1616

1717
import copy
18+
import io
1819
import socket
1920

2021
import mock
@@ -625,30 +626,28 @@ def test_reject_open_redirect(self):
625626
b''
626627
]
627628

628-
# Collect the response data to verify at the end. The
629-
# SimpleHTTPRequestHandler writes the response data by calling the
630-
# request socket sendall() method.
631-
self.data = b''
632-
633-
def fake_sendall(data):
634-
self.data += data
635-
636-
mock_req.sendall.side_effect = fake_sendall
637-
638629
client_addr = ('8.8.8.8', 54321)
639630
mock_server = mock.MagicMock()
640631
# This specifies that the server will be able to handle requests other
641632
# than only websockets.
642633
mock_server.only_upgrade = False
643634

644635
# Constructing a handler will process the mock_req request passed in.
645-
websocketproxy.NovaProxyRequestHandler(
636+
handler = websocketproxy.NovaProxyRequestHandler(
646637
mock_req, client_addr, mock_server)
647638

639+
# Collect the response data to verify at the end. The
640+
# SimpleHTTPRequestHandler writes the response data to a 'wfile'
641+
# attribute.
642+
output = io.BytesIO()
643+
handler.wfile = output
644+
# Process the mock_req again to do the capture.
645+
handler.do_GET()
646+
output.seek(0)
647+
result = output.readlines()
648+
648649
# Verify no redirect happens and instead a 400 Bad Request is returned.
649-
self.data = self.data.decode()
650-
self.assertIn('Error code: 400', self.data)
651-
self.assertIn('Message: URI must not start with //', self.data)
650+
self.assertIn('400 URI must not start with //', result[0].decode())
652651

653652
@mock.patch('nova.objects.ConsoleAuthToken.validate')
654653
def test_no_compute_rpcapi_with_invalid_token(self, mock_validate):

0 commit comments

Comments
 (0)