Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sendall(self):
with mock.patch("time.gmtime") as tt:
tt.return_value = time.struct_time((2013, 4, 30, 10, 39, 21, 1, 120, 0))
Entry.single_register(
Entry.GET, "http://testme.org/get/p/?a=1&b=2", body="test_body"
)
resp = urlopen("http://testme.org/get/p/?b=2&a=1", timeout=10)
self.assertEqual(resp.code, 200)
self.assertEqual(resp.read(), b"test_body")
self.assertEqualHeaders(
dict(resp.headers),
{
"Status": "200",
"Content-length": "9",
"Server": "Python/Mocket",
"Connection": "close",
"Date": "Tue, 30 Apr 2013 10:39:21 GMT",
"Content-type": "text/plain; charset=utf-8",
},
)
def test_user_get_userid(self):
# Handler for `user2 = User.get(...)`
Entry.single_register(
Entry.POST,
self.lastuser.endpoint_url(
current_app.lastuser_config['getuser_userid_endpoint']
),
body=json.dumps(
{
"status": "ok",
"type": "user",
"buid": "1234567890123456789012",
"userid": "1234567890123456789012",
"name": "user1",
"title": "User 1",
"label": "User 1 (@user1)",
"oldids": ['0987654321098765432109'],
"timezone": "Asia/Kolkata",
}
def test_http_session(self):
url = 'http://httpbin.org/ip'
body = "asd" * 100
Entry.single_register(Entry.GET, url, body=body, status=404)
Entry.single_register(Entry.POST, url, body=body*2, status=201)
async def main(l):
async with aiohttp.ClientSession(loop=l) as session:
with async_timeout.timeout(3):
async with session.get(url) as get_response:
assert get_response.status == 404
assert await get_response.text() == body
with async_timeout.timeout(3):
async with session.post(url, data=body * 6) as post_response:
assert post_response.status == 201
assert await post_response.text() == body * 2
assert Mocket.last_request().method == 'POST'
assert Mocket.last_request().body == body * 6
loop = asyncio.get_event_loop()
def test_sendall_json(self):
with mock.patch("time.gmtime") as tt:
tt.return_value = time.struct_time((2013, 4, 30, 10, 39, 21, 1, 120, 0))
Entry.single_register(
Entry.GET,
"http://testme.org/get?a=1&b=2#test",
body='{"a": "€"}',
headers={"content-type": "application/json"},
)
response = urlopen("http://testme.org/get?b=2&a=1#test", timeout=10)
self.assertEqual(response.code, 200)
self.assertEqual(response.read(), b'{"a": "\xe2\x82\xac"}')
self.assertEqualHeaders(
dict(response.headers),
{
"status": "200",
"content-length": "12",
"server": "Python/Mocket",
"connection": "close",
def test_post_file_object(self):
url = "http://github.com/fluidicon.png"
Entry.single_register(Entry.POST, url, status=201)
file_obj = open("tests/fluidicon.png", "rb")
files = {"content": file_obj}
r = requests.post(url, files=files, data={}, verify=False)
self.assertEqual(r.status_code, 201)
def test_same_url_different_methods(self):
url = "http://bit.ly/fakeurl"
response_to_mock = {"content": 0, "method": None}
responses = []
methods = [Entry.PUT, Entry.GET, Entry.POST]
for m in methods:
response_to_mock["method"] = m
Entry.single_register(m, url, body=json.dumps(response_to_mock))
response_to_mock["content"] += 1
for m in methods:
responses.append(requests.request(m, url).json())
methods_from_responses = [r["method"] for r in responses]
contents_from_responses = [r["content"] for r in responses]
self.assertEqual(methods, methods_from_responses)
self.assertEqual(list(range(len(methods))), contents_from_responses)
def test_file_object(self):
url = "http://github.com/fluidicon.png"
filename = "tests/fluidicon.png"
file_obj = open(filename, "rb")
Entry.single_register(Entry.GET, url, body=file_obj)
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))
self.assertEqual(r.headers["Content-Type"], "image/png")
def test_json(response):
url_to_mock = 'https://testme.org/json'
Entry.single_register(
Entry.GET,
url_to_mock,
body=json.dumps(response),
headers={'content-type': 'application/json'})
mocked_response = requests.get(url_to_mock).json()
assert response == mocked_response
mocked_response = json.loads(urlopen(url_to_mock).read().decode('utf-8'))
assert response == mocked_response