def test_mock_works_with_multiple_methods(self): double = ServiceMock( service='foo', methods=['GET', 'POST', 'PATCH'], url='/foo', input_schema={'type': 'object'}, output_schema={'type': 'array'}, output=[] ) set_service_locations(dict(foo="http://localhost:1234/")) self.useFixture(double) self.assertEqual( 200, requests.post("http://localhost:1234/foo", json={}).status_code ) self.assertEqual( 200, requests.get("http://localhost:1234/foo", json={}).status_code ) self.assertEqual( 200, requests.patch("http://localhost:1234/foo", json={}).status_code )
def test_mock_records_calls(self): double = ServiceMock( service='foo', methods=['POST'], url='/', input_schema={'type': 'object'}, output_schema={'type': 'array'}, output=[] ) set_service_locations(dict(foo="http://localhost:1234/")) self.useFixture(double) requests.post("http://localhost:1234/", json={'call': 1}) requests.post("http://localhost:1234/", json={'call': 2}) call1, call2 = double.calls self.assertEqual( json.loads(call1.request.body.decode()), {'call': 1} ) self.assertEqual( json.loads(call2.request.body.decode()), {'call': 2} )
def test_can_construct_double_with_input_schema_and_valid_payload(self): double = ServiceMock(service='foo', methods=['POST'], url='/', input_schema={'type': 'object'}, output_schema={'type': 'array'}, output=[]) set_service_locations(dict(foo="http://localhost:1234/")) self.useFixture(double) resp = requests.post("http://localhost:1234/", json={}) self.assertEqual(200, resp.status_code) self.assertEqual([], resp.json())
def test_mock_regards_method(self): double = ServiceMock(service='foo', methods=['GET'], url='/foo', input_schema={'type': 'object'}, output_schema={'type': 'array'}, output=[]) set_service_locations(dict(foo="http://localhost:1234/")) self.useFixture(double) self.assertRaises(requests.exceptions.ConnectionError, requests.post, "http://localhost:1234/bar", json={})
def test_mock_output_status(self): double = ServiceMock( service='foo', methods=['POST'], url='/foo', input_schema={'type': 'object'}, output_schema={'type': 'array'}, output=[], output_status=201 ) set_service_locations(dict(foo="http://localhost:1234/")) self.useFixture(double) self.assertEqual( 201, requests.post("http://localhost:1234/foo", json={}).status_code )
def setUp(self): super().setUp() # service locations are cached between tests. This should eventually # be fixed, but until then it's easier to set them to an empty dict at # the start of every test: set_service_locations({})