Ejemplo n.º 1
0
def check_digest_header(content, headers):
    digest_value = headers.get("digest")
    if not digest_value:
        return

    digest_value = digest_value.replace("sha-256=", "")
    assert digest(content).decode("ascii") == digest_value, digest(content)
 def test_echo_after_request(self):
     response = self.client.open("/datetime/v1/echo", method="GET")
     self.assert200(response,
                    "Response body is : " + response.data.decode("utf-8"))
     d = response.headers.get("Digest")
     assert d, response.headers
     assert response.headers["Digest"][8:].encode("ascii") == digest(
         response.data)
 def test_post_data(self):
     request_body = json.dumps({"a": "ciao"})
     response = self.client.open(
         "/datetime/v1/data",
         method="POST",
         data=request_body,
         headers={"Content-Type": "application/json"},
     )
     self.assert_status(
         response,
         200,
         "Response body is : " + response.data.decode("utf-8"),
     )
     assert "Digest" in response.headers
     assert response.headers["Digest"][8:].encode("ascii") == digest(
         response.data)
 def test_post_data_gzip(self):
     request_body = json.dumps({"a": "ciao"})
     response = self.client.open(
         "/datetime/v1/data",
         method="POST",
         data=request_body,
         headers={
             "Content-Type": "application/json",
             "Accept-Encoding": "gzip",
         },
     )
     self.assert_status(response, 200,
                        "Response body is : " + repr(response.data))
     assert "Digest" in response.headers
     assert response.headers["Digest"][8:].encode("ascii") == digest(
         response.data)
     assert "ret" in json.loads(gzip.decompress(response.data))
Ejemplo n.º 5
0
def send_request(
    url,
    method,
    headers=None,
    signed_headers=None,
    payload_body=None,
    expires_window=2,
):
    from openapi.callbacks import digest

    request = Request(
        method,
        url,
        headers=CaseInsensitiveDict(headers or {}),
        data=payload_body,
    )

    if method not in ("GET", "HEAD", "DELETE"):
        request.headers["Digest"] = b"sha-256=" + digest(
            request.data if request.data else b"")
        assert "Digest" in request.headers

    if signed_headers:
        created = time()
        signature = Signature(
            keyId=MOCK_KID,
            algorithm="bar",
            headers=signed_headers,
            created=int(created),
            expires=created + expires_window,
        )
        request.headers["Signature"] = signature.sign_http_message(request)
        request.headers["Signature-String"] = signature.signature_string(
            method,
            urlparse(url).path, request.headers).replace("\n", "%")
    prepared_request = request.prepare()
    response = s.send(prepared_request, verify=0)

    return response