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_put_incoming_creates_a_blob_using_filesystem(self):
self.prepare('filesystem')
user_id, doc_id = self.user_id, uuid4().hex
content = 'Hi'
formatter = IncomingFormatter()
incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id)
yield treq.put(incoming_endpoint, BytesIO(content), persistent=False)
db = self.state.open_database(user_id)
consumer = DummyRequest([''])
yield db.read_blob(user_id, doc_id, consumer, namespace='MX')
flags = yield db.get_flags(user_id, doc_id, namespace='MX')
data = consumer.written.pop()
expected_preamble = formatter.preamble(content, doc_id)
expected_preamble = decode_preamble(expected_preamble, True)
written_preamble, written_content = data.split()
written_preamble = decode_preamble(written_preamble, True)
self.assertEquals(expected_preamble, written_preamble)
self.assertEquals(content, written_content)
self.assertIn(Flags.PENDING, flags)
def _get(self, *args, **kwargs):
kwargs['agent'] = Agent(reactor)
return treq.get(*args, **kwargs)
def make_post(host, port, data):
request = post(
"http://{host}:{port}".format(host=host, port=port),
data=data,
persistent=False
)
def failed(failure):
Message.new(message_type=u"acceptance:http_query_failed",
reason=unicode(failure)).write()
return False
request.addCallbacks(content, failed)
return request
d = verify_socket(host, port)
parsed = urlparse.urlparse(url)
claims["aud"] = "{scheme}://{netloc}".format(
scheme=parsed.scheme,
netloc=parsed.netloc
)
log.msg("Setting VAPID 'aud' to {}".format(claims["aud"]))
headers.update(self._vapid.sign(claims, self._crypto_key))
if data:
headers.update({
"Content-Type": "application/octet-stream",
"Content-Encoding": "aesgcm",
"Crypto-key": crypto_key,
"Encryption": self._encryption,
})
d = treq.post(url,
data=data,
headers=headers,
allow_redirects=False,
agent=self._agent)
d.addCallback(self._sent_notification, processor)
d.addErrback(self._error_notif, processor)
def check_and_decode_json(result, response_code):
"""
Given ``treq`` response object, extract JSON and ensure response code
is the expected one.
:param result: ``treq`` response.
:param int response_code: Expected response code.
:return: ``Deferred`` firing with decoded JSON.
"""
def error(body):
raise ResponseError(result.code, body)
if result.code != response_code:
d = content(result)
d.addCallback(error)
return d
return json_content(result)
def query_server():
req = get(
"http://{host}:12345".format(host=node.public_address),
persistent=False
).addCallbacks(content)
return req
def send_request():
"""
Send an HTTP request in a loop until the request is answered.
"""
response = request(
b"GET", b"http://127.0.0.1:%d" % (port,),
persistent=False)
def check_error(failure):
"""
Catch ConnectionRefused errors and response timeouts and return
False so that loop_until repeats the request.
Other error conditions will be passed down the errback chain.
"""
failure.trap(ConnectionRefusedError, ResponseNeverReceived)
return False
response.addErrback(check_error)
return response
def a_case(fname):
env = treq.load_py(os.path.splitext(fname)[0] + ".py")
expect = env['request']
cfg = env['cfg']
req = treq.request(fname, expect)
for case in req.gen_cases(cfg):
case[0](*case[1:])
def test_put_incoming_creates_a_document_using_couch(self):
self.prepare('couch')
user_id, doc_id = self.user_id, uuid4().hex
content, scheme = 'Hi', EncryptionSchemes.PUBKEY
formatter = IncomingFormatter()
incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id)
yield treq.put(incoming_endpoint, BytesIO(content), persistent=False)
db = self.state.open_database(user_id)
doc = db.get_doc(doc_id)
self.assertEquals(doc.content, formatter.format(content, scheme))
def deliver_using_incoming_api(url, user_uuid, token, data):
auth = 'Token %s' % base64.b64encode('%s:%s' % (user_uuid, token))
uri = "%s/incoming/%s/%s?namespace=MX" % (url, user_uuid, uuid.uuid4().hex)
return treq.put(uri, headers={'Authorization': auth}, data=BytesIO(data))