From 81acba47003a3c0c1182ad15498685f398c21a8e Mon Sep 17 00:00:00 2001 From: Jonathan Berglind Date: Tue, 13 Feb 2018 15:01:45 +0100 Subject: [PATCH 1/3] Use HTTPStatus for existing tests, add more ones Add tests for endpoints being refactored --- tests/end2end/fixtures/test_webserver.py | 36 +++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/end2end/fixtures/test_webserver.py b/tests/end2end/fixtures/test_webserver.py index a59d425e2..8187e32cd 100644 --- a/tests/end2end/fixtures/test_webserver.py +++ b/tests/end2end/fixtures/test_webserver.py @@ -22,6 +22,7 @@ import json import urllib.request import urllib.error +from http import HTTPStatus import pytest @@ -52,11 +53,38 @@ def test_server(server, qtbot, path, content, expected): @pytest.mark.parametrize('line, verb, path, equal', [ - ({'verb': 'GET', 'path': '/', 'status': 200}, 'GET', '/', True), - ({'verb': 'GET', 'path': '/foo/', 'status': 200}, 'GET', '/foo', True), + ({'verb': 'GET', 'path': '/', 'status': HTTPStatus.OK}, 'GET', '/', True), + ({'verb': 'GET', 'path': '/foo/', 'status': HTTPStatus.OK}, + 'GET', '/foo', True), + ({'verb': 'GET', 'path': '/relative-redirect', 'status': HTTPStatus.FOUND}, + 'GET', '/relative-redirect', True), + ({'verb': 'GET', 'path': '/absolute-redirect', 'status': HTTPStatus.FOUND}, + 'GET', '/absolute-redirect', True), + ({'verb': 'GET', 'path': '/redirect-to', 'status': HTTPStatus.FOUND}, + 'GET', '/redirect-to', True), + ({'verb': 'GET', 'path': '/redirect-self', 'status': HTTPStatus.FOUND}, + 'GET', '/redirect-self', True), + ({'verb': 'GET', 'path': '/content-size', 'status': HTTPStatus.OK}, + 'GET', '/content-size', True), + ({'verb': 'GET', 'path': '/twenty-mb', 'status': HTTPStatus.OK}, + 'GET', '/twenty-mb', True), + ({'verb': 'GET', 'path': '/500-inline', + 'status': HTTPStatus.INTERNAL_SERVER_ERROR}, 'GET', '/500-inline', True), + ({'verb': 'GET', 'path': '/basic-auth/user1/password1', + 'status': HTTPStatus.UNAUTHORIZED}, + 'GET', '/basic-auth/user1/password1', True), + ({'verb': 'GET', 'path': '/drip', 'status': HTTPStatus.OK}, + 'GET', '/drip', True), + ({'verb': 'GET', 'path': '/404', 'status': HTTPStatus.NOT_FOUND}, + 'GET', '/404', True), - ({'verb': 'GET', 'path': '/', 'status': 200}, 'GET', '/foo', False), - ({'verb': 'POST', 'path': '/', 'status': 200}, 'GET', '/', False), + ({'verb': 'GET', 'path': '/', 'status': HTTPStatus.OK}, + 'GET', '/foo', False), + ({'verb': 'POST', 'path': '/', 'status': HTTPStatus.OK}, + 'GET', '/', False), + ({'verb': 'GET', 'path': '/basic-auth/user/password', + 'status': HTTPStatus.UNAUTHORIZED}, + 'GET', '/basic-auth/user/passwd', False), ]) def test_expected_request(server, line, verb, path, equal): expected = server.ExpectedRequest(verb, path) From 3d5bba9cff7d60f3803f595ad709e3881c85b830 Mon Sep 17 00:00:00 2001 From: Jonathan Berglind Date: Tue, 13 Feb 2018 15:05:15 +0100 Subject: [PATCH 2/3] Use HTTPStatus in flask test server --- tests/end2end/fixtures/webserver_sub.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/end2end/fixtures/webserver_sub.py b/tests/end2end/fixtures/webserver_sub.py index 4ec4619f7..f0720bab6 100644 --- a/tests/end2end/fixtures/webserver_sub.py +++ b/tests/end2end/fixtures/webserver_sub.py @@ -32,6 +32,7 @@ import time import signal import os import threading +from http import HTTPStatus import cheroot.wsgi import flask @@ -112,7 +113,7 @@ def redirect_n_times(n): def relative_redirect(): """302 Redirect once.""" response = app.make_response('') - response.status_code = 302 + response.status_code = HTTPStatus.FOUND response.headers['Location'] = flask.url_for('root') return response @@ -121,7 +122,7 @@ def relative_redirect(): def absolute_redirect(): """302 Redirect once.""" response = app.make_response('') - response.status_code = 302 + response.status_code = HTTPStatus.FOUND response.headers['Location'] = flask.url_for('root', _external=True) return response @@ -133,7 +134,7 @@ def redirect_to(): # werkzeug from "fixing" the URL. This endpoint should set the Location # header to the exact string supplied. response = app.make_response('') - response.status_code = 302 + response.status_code = HTTPStatus.FOUND response.headers['Location'] = flask.request.args['url'].encode('utf-8') return response @@ -149,7 +150,7 @@ def content_size(): response = flask.Response(generate_bytes(), headers={ "Content-Type": "application/octet-stream", }) - response.status_code = 200 + response.status_code = HTTPStatus.OK return response @@ -163,7 +164,7 @@ def twenty_mb(): "Content-Type": "application/octet-stream", "Content-Length": str(20 * 1024 * 1024), }) - response.status_code = 200 + response.status_code = HTTPStatus.OK return response @@ -174,7 +175,7 @@ def internal_error_attachment(): "Content-Type": "application/octet-stream", "Content-Disposition": 'inline; filename="attachment.jpg"', }) - response.status_code = 500 + response.status_code = HTTPStatus.INTERNAL_SERVER_ERROR return response @@ -199,7 +200,7 @@ def basic_auth(user='user', passwd='passwd'): auth = flask.request.authorization if not auth or auth.username != user or auth.password != passwd: r = flask.make_response() - r.status_code = 401 + r.status_code = HTTPStatus.UNAUTHORIZED r.headers = {'WWW-Authenticate': 'Basic realm="Fake Realm"'} return r @@ -222,14 +223,14 @@ def drip(): "Content-Type": "application/octet-stream", "Content-Length": str(numbytes), }) - response.status_code = 200 + response.status_code = HTTPStatus.OK return response @app.route('/404') def status_404(): r = flask.make_response() - r.status_code = 404 + r.status_code = HTTPStatus.NOT_FOUND return r From 681bb058fa3ca862a0ba4184a23dfcef1990f9eb Mon Sep 17 00:00:00 2001 From: Jonathan Berglind Date: Tue, 13 Feb 2018 17:58:11 +0100 Subject: [PATCH 3/3] Use HTTPStatus enum instead of http.client in webserver fixture --- tests/end2end/fixtures/webserver.py | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/end2end/fixtures/webserver.py b/tests/end2end/fixtures/webserver.py index 2beb6fb95..9ba665d0c 100644 --- a/tests/end2end/fixtures/webserver.py +++ b/tests/end2end/fixtures/webserver.py @@ -23,7 +23,7 @@ import re import sys import json import os.path -import http.client +from http import HTTPStatus import attr import pytest @@ -65,28 +65,28 @@ class Request(testprocess.Line): # WORKAROUND for https://github.com/PyCQA/pylint/issues/399 (?) # pylint: disable=no-member path_to_statuses = { - '/favicon.ico': [http.client.NOT_FOUND], - '/does-not-exist': [http.client.NOT_FOUND], - '/does-not-exist-2': [http.client.NOT_FOUND], - '/404': [http.client.NOT_FOUND], + '/favicon.ico': [HTTPStatus.NOT_FOUND], + '/does-not-exist': [HTTPStatus.NOT_FOUND], + '/does-not-exist-2': [HTTPStatus.NOT_FOUND], + '/404': [HTTPStatus.NOT_FOUND], - '/redirect-later': [http.client.FOUND], - '/redirect-self': [http.client.FOUND], - '/redirect-to': [http.client.FOUND], - '/relative-redirect': [http.client.FOUND], - '/absolute-redirect': [http.client.FOUND], + '/redirect-later': [HTTPStatus.FOUND], + '/redirect-self': [HTTPStatus.FOUND], + '/redirect-to': [HTTPStatus.FOUND], + '/relative-redirect': [HTTPStatus.FOUND], + '/absolute-redirect': [HTTPStatus.FOUND], - '/cookies/set': [http.client.FOUND], + '/cookies/set': [HTTPStatus.FOUND], - '/500-inline': [http.client.INTERNAL_SERVER_ERROR], + '/500-inline': [HTTPStatus.INTERNAL_SERVER_ERROR], } for i in range(15): - path_to_statuses['/redirect/{}'.format(i)] = [http.client.FOUND] + path_to_statuses['/redirect/{}'.format(i)] = [HTTPStatus.FOUND] for suffix in ['', '1', '2', '3', '4', '5', '6']: key = '/basic-auth/user{}/password{}'.format(suffix, suffix) - path_to_statuses[key] = [http.client.UNAUTHORIZED, http.client.OK] + path_to_statuses[key] = [HTTPStatus.UNAUTHORIZED, HTTPStatus.OK] - default_statuses = [http.client.OK, http.client.NOT_MODIFIED] + default_statuses = [HTTPStatus.OK, HTTPStatus.NOT_MODIFIED] # pylint: enable=no-member sanitized = QUrl('http://localhost' + self.path).path() # Remove ?foo