Example #1
0
def test_content_type_version():
    encap = headers.encap_headers({
        "content-type": "text/plain",
        "fn-fdk-version": "1.2.3"
    })

    assert encap == {"content-type": "text/plain", "fn-fdk-version": "1.2.3"}
Example #2
0
def setup_headers(deadline=None, headers=None,
                  request_url="/", method="POST", gateway=False):
    new_headers = {}

    if gateway:
        new_headers = hs.encap_headers(headers)
        new_headers.update({
            constants.FN_INTENT: constants.INTENT_HTTP_REQUEST,
        })
    elif headers is not None:
        for k, v in headers.items():
            new_headers.update({k: v})

    new_headers.update({
        constants.FN_HTTP_REQUEST_URL: request_url,
        constants.FN_HTTP_METHOD: method,
    })

    if deadline is None:
        now = dt.datetime.now(dt.timezone.utc).astimezone()
        now += dt.timedelta(0, float(constants.DEFAULT_DEADLINE))
        deadline = now.isoformat()

    new_headers.update({constants.FN_DEADLINE: deadline})
    return new_headers
Example #3
0
    def SetResponseHeaders(self, headers, status_code):
        log.log("setting headers. gateway: {0}".format(self.__is_gateway()))
        if self.__is_gateway():
            headers = hs.encap_headers(headers, status=status_code)

        for k, v in headers.items():
            self.__response_headers[k.lower()] = v
Example #4
0
def test_content_runtime_version():
    encap = headers.encap_headers({
        "content-type": "text/plain",
        "fn-fdk-runtime": "python:3.8.8 final"
    })

    assert encap == {
        "content-type": "text/plain",
        "fn-fdk-runtime": "python:3.8.8 final",
    }
Example #5
0
def test_encap_simple_headers():
    encap = headers.encap_headers({
        "Test-header": "foo",
        "name-Conflict": "h1",
        "name-conflict": "h2",
        "nAme-conflict": ["h3", "h4"],
        "fn-http-h-name-conflict": "h5",
        "multi-header": ["bar", "baz"]
    })
    assert "fn-http-h-test-header" in encap
    assert "fn-http-h-name-conflict" in encap
    assert "fn-http-h-multi-header" in encap

    assert encap["fn-http-h-test-header"] == "foo"
    assert set(
        encap["fn-http-h-name-conflict"]) == {"h1", "h2", "h3", "h4", "h5"}
    assert encap["fn-http-h-multi-header"] == ["bar", "baz"]
Example #6
0
def set_response_headers(current_headers, new_headers,
                         status_code, content_type=None):
    if isinstance(new_headers, dict):
        new_headers = hs.GoLikeHeaders(new_headers)
    elif isinstance(new_headers, hs.GoLikeHeaders):
        pass
    else:
        raise TypeError(
            "Invalid headers type: {}, only dict allowed."
            .format(type(new_headers))
        )

    new_headers = hs.encap_headers(
        new_headers,
        status=status_code,
        content_type=content_type
    )
    for k, v in new_headers.items():
        current_headers.set(k, v)

    return current_headers
Example #7
0
def test_encap_no_headers():
    encap = headers.encap_headers({})
    assert not encap, "headers should be empty"
Example #8
0
def test_encap_status_override():
    encap = headers.encap_headers({"fn-http-status": 412}, 202)
    assert "fn-http-status" in encap
    assert encap["fn-http-status"] == "202"
Example #9
0
def test_encap_status():
    encap = headers.encap_headers({}, 202)
    assert "fn-http-status" in encap
    assert encap["fn-http-status"] == "202"