1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
import time
from unittest import mock
import pytest
from PyQt6.Qt import QCoreApplication, QNetworkReply
from UM.TaskManagement.HttpRequestManager import HttpRequestManager
#
# Test a GET request and it should succeed.
#
@pytest.mark.skip(reason = "This runs and works perfectly fine if you run this without junitxml.")
def test_getSuccessful() -> None:
app = QCoreApplication([])
http_request_manager = HttpRequestManager.getInstance()
cbo = mock.Mock()
def callback(*args, **kwargs):
cbo.callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
request_data = http_request_manager.get(url = "http://localhost:8080/success", callback = callback)
# Make sure that if something goes wrong, we quit after 10 seconds
http_request_manager.callLater(10.0, app.quit)
app.exec()
http_request_manager.cleanup() # Remove all unscheduled events
cbo.callback.assert_called_once_with(request_data.reply)
#
# Test a GET request and it should fail with a 404 Page Not Found.
#
@pytest.mark.skip(reason = "This runs and works perfectly fine if you run this without junitxml.")
def test_getFail404() -> None:
app = QCoreApplication([])
http_request_manager = HttpRequestManager.getInstance()
cbo = mock.Mock()
def callback(*args, **kwargs):
cbo.callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
def error_callback(*args, **kwargs):
cbo.error_callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
request_data = http_request_manager.get(url = "http://localhost:8080/do_not_exist",
callback = callback,
error_callback = error_callback)
# Make sure that if something goes wrong, we quit after 10 seconds
http_request_manager.callLater(10.0, app.quit)
app.exec()
http_request_manager.cleanup() # Remove all unscheduled events
cbo.error_callback.assert_called_once_with(request_data.reply, QNetworkReply.NetworkError.ContentNotFoundError)
cbo.callback.assert_not_called()
#
# Test a GET request and it should time out.
#
@pytest.mark.skip(reason = "This runs and works perfectly fine if you run this without junitxml.")
def test_getTimeout() -> None:
app = QCoreApplication([])
http_request_manager = HttpRequestManager.getInstance()
cbo = mock.Mock()
def error_callback(*args, **kwargs):
cbo.error_callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
request_data = http_request_manager.get(url = "http://localhost:8080/timeout",
error_callback = error_callback, timeout = 1)
# Make sure that if something goes wrong, we quit after 10 seconds
http_request_manager.callLater(10.0, app.quit)
app.exec()
http_request_manager.cleanup() # Remove all unscheduled events
cbo.error_callback.assert_called_once_with(request_data.reply, QNetworkReply.NetworkError.OperationCanceledError)
assert request_data.is_aborted_due_to_timeout
#
# Test a GET request that requires Basic Auth to access. This test provides correct auth data and expects an HTTP
# 200 as a result.
#
@pytest.mark.skip(reason = "This runs and works perfectly fine if you run this without junitxml.")
def test_getBasicAuthSuccess() -> None:
app = QCoreApplication([])
http_request_manager = HttpRequestManager.getInstance()
cbo = mock.Mock()
def callback(*args, **kwargs):
cbo.callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
def error_callback(*args, **kwargs):
cbo.callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
request_data = http_request_manager.get(url="http://localhost:8080/auth",
headers_dict = {"Authorization": "Basic dXNlcjp1c2Vy"},
callback = callback,
error_callback=error_callback)
# Make sure that if something goes wrong, we quit after 10 seconds
http_request_manager.callLater(10.0, app.quit)
app.exec()
http_request_manager.cleanup() # Remove all unscheduled events
cbo.callback.assert_called_once_with(request_data.reply)
cbo.error_callback.assert_not_called()
#
# Test a GET request that requires Basic Auth to access. This test doesn't provide auth data and expects an HTTP
# 401 as a result.
#
@pytest.mark.skip(reason = "This runs and works perfectly fine if you run this without junitxml.")
def test_getBasicAuthFail() -> None:
app = QCoreApplication([])
http_request_manager = HttpRequestManager.getInstance()
cbo = mock.Mock()
def callback(*args, **kwargs):
cbo.callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
def error_callback(*args, **kwargs):
cbo.error_callback(*args, **kwargs)
# quit now so we don't need to wait
http_request_manager.callLater(0, app.quit)
request_data = http_request_manager.get(url="http://localhost:8080/auth",
callback=callback,
error_callback = error_callback)
# Make sure that if something goes wrong, we quit after 10 seconds
http_request_manager.callLater(10.0, app.quit)
app.exec()
http_request_manager.cleanup() # Remove all unscheduled events
cbo.error_callback.assert_called_once_with(request_data.reply, request_data.reply.error())
cbo.callback.assert_not_called()
|