Permalink
Browse files

Merge pull request #6 from txels/method_match

Method matching now also a regex, and match headers optional
  • Loading branch information...
2 parents f5eb979 + a89bf0e commit b86e4b15d5d78ce5463f1ef9a11292411e6648eb Alex Couper committed May 18, 2012
Showing with 38 additions and 16 deletions.
  1. +14 −9 pretenders/http/server.py
  2. +24 −7 pretenders/http/tests/integration/test_http.py
View
23 pretenders/http/server.py
@@ -1,8 +1,9 @@
import re
from collections import OrderedDict
-from bottle import request, response, route, run, HTTPResponse
+from bottle import request, response, route, HTTPResponse
from bottle import delete, get, post
+from bottle import run as run_bottle
presets = OrderedDict()
history = []
@@ -30,14 +31,14 @@ def select_preset(path):
"""
path_match = False
for key, preset_list in presets.items():
- if not len(preset_list):
- continue
preset = preset_list[0]
preset_path = preset['match-path']
preset_method = preset['match-method']
if re.match(preset_path, path):
- if request.method == preset_method or preset_method == '*':
+ if re.match(preset_method, request.method):
del preset_list[0]
+ if not preset_list:
+ del presets[key]
return preset
else:
path_match = True
@@ -82,8 +83,8 @@ def add_preset():
headers = to_dict(request.headers,
include=lambda x: not x.startswith('X-Pretend-'))
- method = get_header('X-Pretend-Match-Method')
- path = get_header('X-Pretend-Match-Path')
+ method = get_header('X-Pretend-Match-Method', '')
+ path = get_header('X-Pretend-Match-Path', '')
if (path, method) not in presets:
presets[(path, method)] = []
@@ -108,6 +109,9 @@ def clear_presets():
@get('/history/<ordinal:int>')
def get_history(ordinal):
+ """
+ Access requests issued to the mock server
+ """
try:
saved = history[ordinal]
for header, value in saved['headers'].items():
@@ -127,9 +131,10 @@ def clear_history():
del history[:]
-def run_bottle(port=8000):
- run(host='localhost', port=port, reloader=True)
+def run(port=8000):
+ "Start the mock HTTP server"
+ run_bottle(host='localhost', port=port, reloader=True)
if __name__ == "__main__":
- run_bottle()
+ run()
View
31 pretenders/http/tests/integration/test_http.py
@@ -113,23 +113,31 @@ def test_method_matching():
"Test that server matches methods correctly."
test_client.reset_all()
add_test_preset('/test_get', 'GET', 'You tested a get', 200)
- add_test_preset('/test_get', 'GET', 'You tested a get', 200)
- add_test_preset('/test_post', 'POST', 'You tested a post', 201)
add_test_preset('/test_post', 'POST', 'You tested a post', 201)
- add_test_preset('/test_star', '*', 'You tested a *', 202)
- add_test_preset('/test_star', '*', 'You tested a *', 202)
+ add_test_preset('/test_star', '.*', 'You tested a .*', 202)
+ add_test_preset('/test_star', '.*', 'You tested a .*', 202)
+ add_test_preset('/test_put_or_post', '(PUT|POST)',
+ 'You tested a PUT or a POST', 203)
# Only GET works when GET matched
- assert_equals(200, test_client._mock.get(url="/test_get").status)
assert_equals(405, test_client._mock.post(url="/test_get").status)
+ assert_equals(200, test_client._mock.get(url="/test_get").status)
+ assert_equals(404, test_client._mock.get(url="/test_get").status)
# Only POST works when POST matched
- assert_equals(201, test_client._mock.post(url="/test_post").status)
assert_equals(405, test_client._mock.get(url="/test_post").status)
+ assert_equals(201, test_client._mock.post(url="/test_post").status)
+ assert_equals(404, test_client._mock.post(url="/test_post").status)
- # Any method works with * as the method matched
+ # Any method works with .* as the method matched
assert_equals(202, test_client._mock.get(url="/test_star").status)
assert_equals(202, test_client._mock.post(url="/test_star").status)
+ assert_equals(404, test_client._mock.post(url="/test_star").status)
+
+ # PUT or POST work with (PUT|POST) as the method matched
+ assert_equals(405, test_client._mock.get(url="/test_put_or_post").status)
+ assert_equals(203, test_client._mock.post(url="/test_put_or_post").status)
+ assert_equals(404, test_client._mock.post(url="/test_put_or_post").status)
def test_multiple_responses_for_a_url():
@@ -170,10 +178,19 @@ def test_regular_expression_matching():
def test_blank_path_matches_anything():
+ "A blank path matcher header matches any path"
test_client.reset_all()
add_test_preset("", response_status=200)
response = test_client._mock.post(url='/some/strange/12121/string')
assert_equals(response.status, 200)
response = test_client._mock.post(url='/some/strange/12121/string')
assert_equals(response.status, 404)
+
+def test_missing_method_and_path_matches_anything():
+ "Missing matcher headers match anything"
+ test_client.reset_all()
+ test_client.add_preset(response_status=323,
+ response_body=b'Hello')
+ response = test_client._mock.post(url='/some/strange/12121/string')
+ assert_equals(response.status, 323)

0 comments on commit b86e4b1

Please sign in to comment.