Skip to content

Commit 8e0c499

Browse files
authored
Refactor: move MockRequest and MockResponse into mock_http module (#226)
This starts to cover some of the server refactoring tasks in #28 by moving the mock request/response classes out of the main server module and into a separate submodule.
1 parent 2785510 commit 8e0c499

5 files changed

Lines changed: 53 additions & 44 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Thanks to the following people for all their contributions! This project depends
254254
| Contributions | Name |
255255
| ----: | :---- |
256256
| [📖](# "Documentation") | [aaxis-em](https://github.com/aaxis-em) |
257+
| [💻](# "Code") | [Adeel](https://github.com/Adeelp1) |
257258
| [💻](# "Code") [⚠️](# "Tests") [🚇](# "Infrastructure") [📖](# "Documentation") [💬](# "Answering Questions") [👀](# "Reviewer") | [Dan Allan](https://github.com/danielballan) |
258259
| [💻](# "Code") | [Vangelis Banos](https://github.com/vbanos) |
259260
| [💻](# "Code") [📖](# "Documentation") | [Chaitanya Prakash Bapat](https://github.com/ChaiBapchya) |

docs/source/release-history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Release History
33
===============
44

5+
In Development
6+
--------------
7+
8+
- Move mock request/response classes into a separate submodule of `server`. (:issue:`226`)
9+
10+
511
Version 0.2.0 (2026-03-16)
612
--------------------------
713

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import mimetypes
2+
3+
4+
class MockRequest:
5+
"An HTTPRequest-like object for local file:/// requests."
6+
def __init__(self, url):
7+
self.url = url
8+
9+
10+
class MockResponse:
11+
"An HTTPResponse-like object for local file:/// requests."
12+
def __init__(self, url, body, headers=None):
13+
self.request = MockRequest(url)
14+
self.body = body
15+
self.headers = headers
16+
self.error = None
17+
18+
if self.headers is None:
19+
self.headers = {}
20+
21+
if 'Content-Type' not in self.headers:
22+
self.headers.update(self._get_content_type_headers_from_url(url))
23+
24+
@staticmethod
25+
def _get_content_type_headers_from_url(url):
26+
# If the extension is not recognized, assume text/html
27+
headers = {'Content-Type': 'text/html'}
28+
29+
content_type, content_encoding = mimetypes.guess_type(url)
30+
31+
if content_type is not None:
32+
headers['Content-Type'] = content_type
33+
34+
if content_encoding is not None:
35+
headers['Content-Encoding'] = content_encoding
36+
37+
return headers

web_monitoring_diff/server/server.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import inspect
77
import functools
88
import logging
9-
import mimetypes
109
import os
1110
import pycurl
1211
import re
@@ -20,6 +19,7 @@
2019
import tornado.web
2120
import traceback
2221
import web_monitoring_diff
22+
from .mock_http import MockResponse
2323
from .. import basic_diffs, html_render_diff, html_links_diff
2424
from ..exceptions import UndiffableContentError, UndecodableContentError
2525
from ..utils import shutdown_executor_in_loop, Signal
@@ -169,42 +169,6 @@ def __init__(self, status_code=500, public_message=None, log_message=None,
169169
super().__init__(status_code, log_message, **kwargs)
170170

171171

172-
class MockRequest:
173-
"An HTTPRequest-like object for local file:/// requests."
174-
def __init__(self, url):
175-
self.url = url
176-
177-
178-
class MockResponse:
179-
"An HTTPResponse-like object for local file:/// requests."
180-
def __init__(self, url, body, headers=None):
181-
self.request = MockRequest(url)
182-
self.body = body
183-
self.headers = headers
184-
self.error = None
185-
186-
if self.headers is None:
187-
self.headers = {}
188-
189-
if 'Content-Type' not in self.headers:
190-
self.headers.update(self._get_content_type_headers_from_url(url))
191-
192-
@staticmethod
193-
def _get_content_type_headers_from_url(url):
194-
# If the extension is not recognized, assume text/html
195-
headers = {'Content-Type': 'text/html'}
196-
197-
content_type, content_encoding = mimetypes.guess_type(url)
198-
199-
if content_type is not None:
200-
headers['Content-Type'] = content_type
201-
202-
if content_encoding is not None:
203-
headers['Content-Encoding'] = content_encoding
204-
205-
return headers
206-
207-
208172
DEBUG_MODE = os.environ.get('DIFFING_SERVER_DEBUG', 'False').strip().lower() == 'true'
209173

210174
VALIDATE_TARGET_CERTIFICATES = \

web_monitoring_diff/tests/test_server_exc_handling.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from tornado.testing import AsyncHTTPTestCase, bind_unused_port
1111
from unittest.mock import patch
1212
import web_monitoring_diff.server.server as df
13+
from web_monitoring_diff.server.mock_http import MockResponse
1314
from web_monitoring_diff.exceptions import UndecodableContentError
1415
import web_monitoring_diff
1516
from tornado.escape import utf8
@@ -234,7 +235,7 @@ async def responder(handler):
234235
assert response.code == 504
235236

236237
def test_missing_params_caller_func(self):
237-
response = df.MockResponse('http://example.org/', 'Whatever')
238+
response = MockResponse('http://example.org/', 'Whatever')
238239
with self.assertRaises(KeyError):
239240
df.caller(mock_diffing_method, response, response)
240241

@@ -603,7 +604,7 @@ def mock_tornado_request(fixture, headers=None):
603604
path = fixture_path(fixture)
604605
with open(path, 'rb') as f:
605606
body = f.read()
606-
return df.MockResponse(f'file://{path}', body, headers)
607+
return MockResponse(f'file://{path}', body, headers)
607608

608609

609610
# TODO: we may want to extract this to a support module
@@ -680,23 +681,23 @@ def _find_stub(self, request):
680681

681682
class MockResponderHeadersTest(unittest.TestCase):
682683
def test_pdf_extension(self):
683-
response = df.MockResponse(f'file://{fixture_path("simple.pdf")}', '')
684+
response = MockResponse(f'file://{fixture_path("simple.pdf")}', '')
684685
assert response.headers['Content-Type'] == 'application/pdf'
685686

686687
def test_html_extension(self):
687-
response = df.MockResponse(f'file://{fixture_path("unknown_encoding.html")}', '')
688+
response = MockResponse(f'file://{fixture_path("unknown_encoding.html")}', '')
688689
assert response.headers['Content-Type'] == 'text/html'
689690

690691
def test_txt_extension(self):
691-
response = df.MockResponse(f'file://{fixture_path("empty.txt")}', '')
692+
response = MockResponse(f'file://{fixture_path("empty.txt")}', '')
692693
assert response.headers['Content-Type'] == 'text/plain'
693694

694695
def test_no_extension_should_assume_html(self):
695-
response = df.MockResponse(f'file://{fixture_path("unknown_encoding")}', '')
696+
response = MockResponse(f'file://{fixture_path("unknown_encoding")}', '')
696697
assert response.headers['Content-Type'] == 'text/html'
697698

698699
def test_unknown_extension_should_assume_html(self):
699-
response = df.MockResponse(f'file://{fixture_path("unknown_encoding.notarealextension")}', '')
700+
response = MockResponse(f'file://{fixture_path("unknown_encoding.notarealextension")}', '')
700701
assert response.headers['Content-Type'] == 'text/html'
701702

702703

0 commit comments

Comments
 (0)