def test_multi_register(self): url = "http://foobar.com/path" Entry.register( Entry.POST, url, Response(body='{"foo":"bar0"}', status=200), Response(body='{"foo":"bar1"}', status=201), Response(body='{"foo":"bar2"}', status=202), ) response = requests.post(url, json={"test": 0}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {"foo": "bar0"}) response = requests.post(url, json={"test": 1}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {"foo": "bar1"}) response = requests.post(url, json={"test": 2}) self.assertEqual(response.status_code, 202) self.assertEqual(response.json(), {"foo": "bar2"}) response = requests.post(url, json={"test": 22}) self.assertEqual(response.status_code, 202) self.assertEqual(response.json(), {"foo": "bar2"})
def test_sendall_double(self): Entry.register(Entry.GET, 'http://testme.org/', Response(status=404), Response()) self.assertRaises(HTTPError, urlopen, 'http://testme.org/') response = urlopen('http://testme.org/') self.assertEqual(response.code, 200) response = urlopen('http://testme.org/') self.assertEqual(response.code, 200) self.assertEqual(len(Mocket._requests), 3)
def test_register(self): with mock.patch("time.gmtime") as tt: tt.return_value = time.struct_time((2013, 4, 30, 10, 39, 21, 1, 120, 0)) Entry.register( Entry.GET, "http://testme.org/get?a=1&b=2#test", Response('{"a": "€"}', headers={"content-type": "application/json"}), ) entries = Mocket._entries[("testme.org", 80)] self.assertEqual(len(entries), 1) entry = entries[0] self.assertEqual(entry.method, "GET") self.assertEqual(entry.schema, "http") self.assertEqual(entry.path, "/get") self.assertEqual(entry.query, "a=1&b=2") self.assertEqual(len(entry.responses), 1) response = entry.responses[0] self.assertEqual(response.body, b'{"a": "\xe2\x82\xac"}') self.assertEqual(response.status, 200) self.assertEqual( response.headers, { "Status": "200", "Date": "Tue, 30 Apr 2013 10:39:21 GMT", "Connection": "close", "Server": "Python/Mocket", "Content-Length": "12", "Content-Type": "application/json", }, )
def test_register(self): with mock.patch('time.gmtime') as tt: tt.return_value = time.struct_time( (2013, 4, 30, 10, 39, 21, 1, 120, 0)) Entry.register( Entry.GET, 'http://testme.org/get?a=1&b=2#test', Response('{"a": "€"}', headers={'content-type': 'application/json'})) entries = Mocket._entries[('testme.org', 80)] self.assertEqual(len(entries), 1) entry = entries[0] self.assertEqual(entry.method, 'GET') self.assertEqual(entry.schema, 'http') self.assertEqual(entry.path, '/get') self.assertEqual(entry.query, 'a=1&b=2') self.assertEqual(len(entry.responses), 1) response = entry.responses[0] self.assertEqual(response.body, b'{"a": "\xe2\x82\xac"}') self.assertEqual(response.status, 200) self.assertEqual( response.headers, { u'Status': u'200', u'Date': u'Tue, 30 Apr 2013 10:39:21 GMT', u'Connection': u'close', u'Server': u'Python/Mocket', u'Content-Length': u'12', u'Content-Type': u'application/json', })
def single_register(cls, method, uri, body='', status=200, headers=None, match_querystring=True): entry = cls(uri, method, Response(body=body, status=status, headers=headers), match_querystring=match_querystring) Mocket.register(entry) return entry
def test_file_object_with_no_lib_magic(self): url = "http://github.com/fluidicon.png" filename = "tests/fluidicon.png" file_obj = open(filename, "rb") Entry.register(Entry.GET, url, Response(body=file_obj, lib_magic=None)) r = requests.get(url) remote_content = r.content local_file_obj = open(filename, "rb") local_content = local_file_obj.read() self.assertEqual(remote_content, local_content) self.assertEqual(len(remote_content), len(local_content)) self.assertEqual(int(r.headers["Content-Length"]), len(local_content)) with self.assertRaises(KeyError): self.assertEqual(r.headers["Content-Type"], "image/png")
def single_register(cls, method, uri, body='', status=200, headers=None): entry = cls(uri, method, Response(body=body, status=status, headers=headers)) Mocket.register(entry) return entry
def test_wrongpath_truesendall(self): Entry.register(Entry.GET, "http://httpbin.org/user.agent", Response(status=404)) response = urlopen("http://httpbin.org/ip") self.assertEqual(response.code, 200)
def test_wrongpath_truesendall(self): Entry.register(Entry.GET, 'http://httpbin.org/user.agent', Response()) response = urlopen('http://httpbin.org/ip') self.assertEqual(response.code, 200)