Skip to content

Commit

Permalink
Implementation of requested changes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ftsalamp authored and danielballan committed May 11, 2018
1 parent 3f92aee commit 6441547
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
12 changes: 1 addition & 11 deletions web_monitoring/diffing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,7 @@ def get(self, differ):

# Pass the bytes and any remaining args to the diffing function.
executor = concurrent.futures.ProcessPoolExecutor()
try:
res = yield executor.submit(caller, func, res_a, res_b, **query_params)
except KeyError:
exceptionMessage = str(sys.exc_info()[1])
exceptionMessage = exceptionMessage[1:-1]
self.send_error(400, reason = 'Malformed request. {}'.format(exceptionMessage))
return
except UndecodableContentError:
exceptionMessage = str(sys.exc_info()[1])
self.send_error(422, reason = exceptionMessage)
return
res = yield executor.submit(caller, func, res_a, res_b, **query_params)
res['version'] = web_monitoring.__version__
# Echo the client's request unless the differ func has specified
# somethine else.
Expand Down
20 changes: 19 additions & 1 deletion web_monitoring/tests/test_diffing_server_exc_handling.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from tornado.testing import AsyncHTTPTestCase
import web_monitoring.diffing_server as df
from web_monitoring.diff_errors import UndecodableContentError
Expand All @@ -12,42 +13,49 @@ def get_app(self):
def test_invalid_url_a_format(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=example.org&b=https://example.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_invalid_url_b_format(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=https://example.org&b=example.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_invalid_diffing_method(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/non_existing?format=json&include=all&a=example.org&b=https://example.org')
self.json_check(response)
self.assertEqual(response.code, 404)

def test_missing_url_a(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&b=https://example.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_missing_url_b(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=https://example.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_not_reachable_url_a(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=https://eeexample.org&b=https://example.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_not_reachable_url_b(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=https://example.org&b=https://eeexample.org')
self.json_check(response)
self.assertEqual(response.code, 400)

def test_missing_params_caller_func(self):
response = self.fetch('http://example.org/')
with self.assertRaises(KeyError):
df.caller(mockDiffingMethod, response, response)
df.caller(mock_diffing_method, response, response)

def test_undecodable_content(self):
response = self.fetch('https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt')
Expand All @@ -57,7 +65,17 @@ def test_undecodable_content(self):
def test_fetch_undecodable_content(self):
port = self.get_http_port()
response = self.fetch(f'http://localhost:{port}/html_token?format=json&include=all&a=https://example.org&b=https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt')
self.json_check(response)
self.assertEqual(response.code, 422)

def json_check(self, response):
jsonHeader = response.headers.get('Content-Type').split(';')
self.assertEqual(jsonHeader[0], 'application/json')

jsonResponse = json.loads(response.body)
self.assertTrue(isinstance(jsonResponse['code'],int))
self.assertTrue(isinstance(jsonResponse['error'],str))


def mock_diffing_method(c_body):
return
Expand Down

0 comments on commit 6441547

Please sign in to comment.