|
15 | 15 | """Tests for nova websocketproxy."""
|
16 | 16 |
|
17 | 17 | import copy
|
| 18 | +import io |
18 | 19 | import socket
|
19 | 20 |
|
20 | 21 | import mock
|
@@ -625,30 +626,28 @@ def test_reject_open_redirect(self):
|
625 | 626 | b''
|
626 | 627 | ]
|
627 | 628 |
|
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 |
| - |
638 | 629 | client_addr = ('8.8.8.8', 54321)
|
639 | 630 | mock_server = mock.MagicMock()
|
640 | 631 | # This specifies that the server will be able to handle requests other
|
641 | 632 | # than only websockets.
|
642 | 633 | mock_server.only_upgrade = False
|
643 | 634 |
|
644 | 635 | # Constructing a handler will process the mock_req request passed in.
|
645 |
| - websocketproxy.NovaProxyRequestHandler( |
| 636 | + handler = websocketproxy.NovaProxyRequestHandler( |
646 | 637 | mock_req, client_addr, mock_server)
|
647 | 638 |
|
| 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 | + |
648 | 649 | # 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()) |
652 | 651 |
|
653 | 652 | @mock.patch('nova.objects.ConsoleAuthToken.validate')
|
654 | 653 | def test_no_compute_rpcapi_with_invalid_token(self, mock_validate):
|
|
0 commit comments