From 863a3107ec46f56a2cea1a122dc3c78f34b7f132 Mon Sep 17 00:00:00 2001 From: starlet-dx <15929766099@163.com> Date: Fri, 30 Dec 2022 14:44:57 +0800 Subject: [PATCH 1/1] Mock time to make date based tests reliable --- tests/functional/test_httplib2.py | 19 ++++++++++--------- tests/functional/test_requests.py | 19 ++++++++++--------- tests/functional/test_urllib2.py | 19 ++++++++++--------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/tests/functional/test_httplib2.py b/tests/functional/test_httplib2.py index 3b78043..519d430 100644 --- a/tests/functional/test_httplib2.py +++ b/tests/functional/test_httplib2.py @@ -28,6 +28,7 @@ from __future__ import unicode_literals import re import httplib2 +from freezegun import freeze_time from sure import expect, within, microseconds from httpretty import HTTPretty, httprettified from httpretty.core import decode_utf8 @@ -64,8 +65,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now): @httprettified -@within(two=microseconds) -def test_httpretty_should_mock_headers_httplib2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_mock_headers_httplib2(): "HTTPretty should mock basic headers with httplib2" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", @@ -80,13 +81,13 @@ def test_httpretty_should_mock_headers_httplib2(now): 'content-length': '35', 'status': '201', 'server': 'Python/HTTPretty', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_httplib2(): "HTTPretty should allow adding and overwritting headers with httplib2" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", @@ -106,7 +107,7 @@ def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): 'content-length': '27', 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @@ -135,8 +136,8 @@ def test_httpretty_should_allow_forcing_headers_httplib2(now): @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(): "HTTPretty should allow adding and overwritting headers by keyword args " \ "with httplib2" @@ -159,7 +160,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): 'content-length': '27', 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) diff --git a/tests/functional/test_requests.py b/tests/functional/test_requests.py index e98b23f..4428039 100644 --- a/tests/functional/test_requests.py +++ b/tests/functional/test_requests.py @@ -31,6 +31,7 @@ import os import re import json import requests +from freezegun import freeze_time from sure import within, microseconds, expect from tornado import version as tornado_version from httpretty import HTTPretty, httprettified @@ -93,8 +94,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now): @httprettified -@within(two=microseconds) -def test_httpretty_should_mock_headers_requests(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_mock_headers_requests(): "HTTPretty should mock basic headers with requests" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", @@ -110,13 +111,13 @@ def test_httpretty_should_mock_headers_requests(now): 'content-length': '35', 'status': '201', 'server': 'Python/HTTPretty', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_requests(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_requests(): "HTTPretty should allow adding and overwritting headers with requests" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", @@ -135,7 +136,7 @@ def test_httpretty_should_allow_adding_and_overwritting_requests(now): 'content-length': '27', 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @@ -160,8 +161,8 @@ def test_httpretty_should_allow_forcing_headers_requests(now): @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(): "HTTPretty should allow adding and overwritting headers by keyword args " \ "with requests" @@ -179,7 +180,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): 'content-length': '27', 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) diff --git a/tests/functional/test_urllib2.py b/tests/functional/test_urllib2.py index d1b92ee..90e4c46 100644 --- a/tests/functional/test_urllib2.py +++ b/tests/functional/test_urllib2.py @@ -34,6 +34,7 @@ except ImportError: import urllib2 urlopen = urllib2.urlopen +from freezegun import freeze_time from sure import within, microseconds from httpretty import HTTPretty, httprettified from httpretty.core import decode_utf8 @@ -73,8 +74,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now): @httprettified -@within(two=microseconds) -def test_httpretty_should_mock_headers_urllib2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_mock_headers_urllib2(): "HTTPretty should mock basic headers with urllib2" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", @@ -93,13 +94,13 @@ def test_httpretty_should_mock_headers_urllib2(now): 'content-length': '35', 'status': '201', 'server': 'Python/HTTPretty', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_urllib2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_urllib2(): "HTTPretty should allow adding and overwritting headers with urllib2" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", @@ -121,7 +122,7 @@ def test_httpretty_should_allow_adding_and_overwritting_urllib2(now): 'content-length': '27', 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) @@ -148,8 +149,8 @@ def test_httpretty_should_allow_forcing_headers_urllib2(): @httprettified -@within(two=microseconds) -def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): +@freeze_time("2013-10-04 04:20:00") +def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(): ("HTTPretty should allow adding and overwritting headers by " "keyword args with urllib2") @@ -171,7 +172,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): 'content-length': str(len(body)), 'status': '200', 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), + 'date': 'Fri, 04 Oct 2013 04:20:00 GMT', }) -- 2.30.0