Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@mocketize
def setUp(self):
self.rclient = redis.StrictRedis()
self.rclient.flushdb()
@mocketize
def test_set(self):
self.assertTrue(self.rclient.set('mocket', 'is awesome!'))
@mocketize
def test_incr(self):
Entry.register_responses('INCRBY counter 1', (1, 2, 3))
self.assertEqual(self.rclient.incr('counter'), 1)
self.assertEqual(self.rclient.incr('counter'), 2)
self.assertEqual(self.rclient.incr('counter'), 3)
self.assertEqual(len(Mocket._requests), 3)
self.assertEqual(Mocket._requests[0].data, b'*3\r\n$6\r\nINCRBY\r\n$7\r\ncounter\r\n$1\r\n1\r\n')
self.assertEqual(Mocket._requests[1].data, b'*3\r\n$6\r\nINCRBY\r\n$7\r\ncounter\r\n$1\r\n1\r\n')
self.assertEqual(Mocket._requests[2].data, b'*3\r\n$6\r\nINCRBY\r\n$7\r\ncounter\r\n$1\r\n1\r\n')
@mocketize
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)",
@mocketize
def test_request_bodies(self):
url = "http://bit.ly/fakeurl/{0}"
for e in range(5):
u = url.format(e)
Entry.single_register(Entry.POST, u, body=str(e))
request_body = urlencode({"key-{0}".format(e): "value={0}".format(e)})
urlopen(u, request_body.encode("utf-8"))
last_request = Mocket.last_request()
assert last_request.body == request_body
@mocketize
def test_mocketize_outside_a_test_class():
assert 2 == 2
@mocketize(truesocket_recording_dir=recording_directory)
def test_truesendall_with_recording_https():
url = 'https://mockbin.com/ip'
requests.get(url, headers={"Accept": "application/json"})
resp = requests.get(url, headers={"Accept": "application/json"})
print(resp.content)
assert resp.status_code == 200
dump_filename = os.path.join(
Mocket.get_truesocket_recording_dir(),
Mocket.get_namespace() + '.json',
)
with io.open(dump_filename) as f:
responses = json.load(f)
assert len(responses['mockbin.com']['443'].keys()) == 1
@mocketize
def test_multipart(self):
url = "http://httpbin.org/post"
data = '--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
headers = {
"Content-Length": "495",
"Content-Type": "multipart/form-data; boundary=xXXxXXyYYzzz",
"Accept": "text/plain",
"User-Agent": "Mocket",
"Accept-encoding": "identity",
}
Entry.register(Entry.POST, url)
response = requests.post(url, data=data, headers=headers)
self.assertEqual(response.status_code, 200)
last_request = Mocket.last_request()
self.assertEqual(last_request.method, "POST")
self.assertEqual(last_request.path, "/post")
@mocketize
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",
@mocketize
def test_get_utf8(self):
Entry.register_response('GET snowman', '☃')
self.assertEqual(self.rclient.get('snowman'), b'\xe2\x98\x83')
self.assertEqual(len(Mocket._requests), 1)
self.assertEqual(Mocket._requests[0].data, b'*2\r\n$3\r\nGET\r\n$7\r\nsnowman\r\n')