-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a37377
commit 76ab1fa
Showing
7 changed files
with
254 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
try: | ||
import uio as io | ||
except ImportError: | ||
import io | ||
|
||
SOL_SOCKET = 'SOL_SOCKET' | ||
SO_REUSEADDR = 'SO_REUSEADDR' | ||
|
||
_calls = [] | ||
_requests = [] | ||
|
||
|
||
def getaddrinfo(host, port): | ||
_calls.append(('getaddrinfo', host, port)) | ||
return (('family', 'addr'), 'socktype', 'proto', 'canonname', 'sockaddr') | ||
|
||
|
||
class socket: | ||
def __init__(self): | ||
self.request_index = 0 | ||
|
||
def setsockopt(self, level, optname, value): | ||
_calls.append(('setsockopt', level, optname, value)) | ||
|
||
def bind(self, addr): | ||
_calls.append(('bind', addr)) | ||
|
||
def listen(self, backlog): | ||
_calls.append(('listen', backlog)) | ||
|
||
def accept(self): | ||
_calls.append(('accept',)) | ||
self.request_index += 1 | ||
return _requests[self.request_index - 1], 'addr' | ||
|
||
|
||
class FakeStream(io.BytesIO): | ||
def __init__(self, input_data): | ||
super().__init__(input_data) | ||
self.response = b'' | ||
|
||
def write(self, data): | ||
self.response += data | ||
|
||
|
||
def get_request_fd(method, path, headers=None, body=None): | ||
if headers is None: | ||
headers = {} | ||
if body is None: | ||
body = '' | ||
elif 'Content-Length' not in headers: | ||
headers['Content-Length'] = str(len(body)) | ||
request_bytes = '{method} {path} HTTP/1.0\n'.format( | ||
method=method, path=path) | ||
if 'Host' not in headers: | ||
headers['Host'] = 'example.com:1234' | ||
for header, value in headers.items(): | ||
request_bytes += '{header}: {value}\n'.format( | ||
header=header, value=value) | ||
request_bytes += '\n' + body | ||
return FakeStream(request_bytes.encode()) | ||
|
||
|
||
def clear_requests(): | ||
_requests.clear() | ||
|
||
|
||
def add_request(method, path, headers=None, body=None): | ||
fd = get_request_fd(method, path, headers=headers, body=body) | ||
_requests.append(fd) | ||
return fd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,143 @@ | ||
import sys | ||
import unittest | ||
from microdot import Microdot | ||
from tests import mock_socket | ||
|
||
|
||
class TestMicrodot(unittest.TestCase): | ||
def test_foo(self): | ||
pass | ||
def setUp(self): | ||
# mock socket module | ||
self.original_socket = sys.modules['microdot'].socket | ||
sys.modules['microdot'].socket = mock_socket | ||
|
||
def tearDown(self): | ||
# restore original socket module | ||
sys.modules['microdot'].socket = self.original_socket | ||
|
||
def test_get_request(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 'foo' | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 200 OK\r\n' | ||
b'Content-Length: 3\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'foo') | ||
|
||
def test_post_request(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 'foo' | ||
|
||
@app.route('/', methods=['POST']) | ||
def index_post(req): | ||
return 'bar' | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('POST', '/') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 200 OK\r\n' | ||
b'Content-Length: 3\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'bar') | ||
|
||
def test_404(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 'foo' | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/foo') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 404 N/A\r\n' | ||
b'Content-Length: 9\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'Not found') | ||
|
||
def test_404_handler(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 'foo' | ||
|
||
@app.errorhandler(404) | ||
def handle_404(req): | ||
return '404' | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/foo') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 200 OK\r\n' | ||
b'Content-Length: 3\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'404') | ||
|
||
def test_500(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 1 / 0 | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 500 N/A\r\n' | ||
b'Content-Length: 21\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'Internal server error') | ||
|
||
def test_500_handler(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 1 / 0 | ||
|
||
@app.errorhandler(500) | ||
def handle_500(req): | ||
return '501', 501 | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 501 N/A\r\n' | ||
b'Content-Length: 3\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'501') | ||
|
||
def test_exception_handler(self): | ||
app = Microdot() | ||
|
||
@app.route('/') | ||
def index(req): | ||
return 1 / 0 | ||
|
||
@app.errorhandler(ZeroDivisionError) | ||
def handle_div_zero(req, exc): | ||
return '501', 501 | ||
|
||
mock_socket.clear_requests() | ||
fd = mock_socket.add_request('GET', '/') | ||
self.assertRaises(IndexError, app.run) | ||
self.assertEqual(fd.response, b'HTTP/1.0 501 N/A\r\n' | ||
b'Content-Length: 3\r\n' | ||
b'Content-Type: text/plain\r\n' | ||
b'\r\n' | ||
b'501') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters