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] 34eeed4290

Change-Id: I546d376869a992601b443fb95acf1034da2a8f36
(cherry picked from commit 214cabe684)
melanie witt 2021-07-31 00:30:16 +00:00
parent b0099aa8a2
commit 9c2f297837
1 changed files with 13 additions and 14 deletions

View File

@ -15,6 +15,7 @@
"""Tests for nova websocketproxy."""
import copy
import io
import socket
import mock
@ -625,16 +626,6 @@ class NovaProxyRequestHandlerTestCase(test.NoDBTestCase):
# Collect the response data to verify at the end. The
# SimpleHTTPRequestHandler writes the response data by calling the
# request socket sendall() method. = b''
def fake_sendall(data): += data
mock_req.sendall.side_effect = fake_sendall
client_addr = ('', 54321)
mock_server = mock.MagicMock()
# This specifies that the server will be able to handle requests other
@ -642,13 +633,21 @@ class NovaProxyRequestHandlerTestCase(test.NoDBTestCase):
mock_server.only_upgrade = False
# Constructing a handler will process the mock_req request passed in.
handler = websocketproxy.NovaProxyRequestHandler(
mock_req, client_addr, mock_server)
# Collect the response data to verify at the end. The
# SimpleHTTPRequestHandler writes the response data to a 'wfile'
# attribute.
output = io.BytesIO()
handler.wfile = output
# Process the mock_req again to do the capture.
result = output.readlines()
# Verify no redirect happens and instead a 400 Bad Request is returned. =
self.assertIn('Error code: 400',
self.assertIn('Message: URI must not start with //',
self.assertIn('400 URI must not start with //', result[0].decode())
def test_no_compute_rpcapi_with_invalid_token(self, mock_validate):