async def test_ps_cookie_sets_cookie(): rs = await PsCookie(CcPlain(), name="foo").exit( RsFake("200 OK"), identity=IdentitySimple(urn="urn:test:99")) assert await rs.headers() == { "Set-Cookie": "foo=urn%3Atest%3A99;Path=/;HttpOnly;Expires=Mon, 13 Feb 2012 03:21:34 GMT;" }
async def test_fk_host_matches(): expected_rs = RsFake("200 OK", CIMultiDict({"foo": "bar"})) assert await FkHost("www.example.com", MgFixed(expected_rs)).route( RqFake()) is None assert (await FkHost("www.example.com", MgFixed(expected_rs)).route( RqFake(headers=CIMultiDict({"host": "www.example.com"}))) is expected_rs)
async def test_ps_token_returns_token(): signature = SiHmac("foo") rs = await PsToken.from_signature(signature).exit( RsFake("200 OK"), IdentitySimple("urn:john-gold")) assert await rs.headers() == {"Content-Type": "application/json"} assert await whole_body_of(rs) == ( b'{"jwt_token": "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJleHAiOiAxMzI2NTk3' b"Njk0LjAsICJpYXQiOiAxMzI2NTExMjk0LjAsICJzdWIiOiAidXJuOmpvaG4tZ29sZCJ9.FKuDBgE" b'lsZhhXR1RWClEpq8plv7tJWPTeQDQSL-cG-o="}')
async def test_rs_with_body_transparent(): rs = RsWithBody( TextBody("New text"), RsFake("200 Whatever", headers=CIMultiDict({"Hi": "Mark"}), body=b"Fake body"), ) assert await rs.status() == "200 Whatever" assert await rs.headers() == CIMultiDict({"Hi": "Mark"})
async def test_rs_with_status_decorating(): rs = RsWithStatus( 404, response=RsFake(status="200 OK", body=b"foo bar", headers=CIMultiDict({"Hello": "there"})), ) assert await rs.body().__anext__() == b"foo bar" assert await rs.headers() == CIMultiDict({"Hello": "there"}) assert await rs.status() == "404 Not Found"
async def test_rs_with_headers_immutability(): headers = await RsWithHeaders( RsFake( "200 OK", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), {"foo": "baz"}, ).headers() with pytest.raises(TypeError): headers["accept"] = "foo"
async def test_rs_with_headers_case_insensitive(): headers = await RsWithHeaders( RsFake( "200 OK", MultiDict({"Foo": "bar", "accept": "some-value"}), ), {"foo": "baz"}, ).headers() assert headers.getall("foo") == headers.getall("Foo") == ["bar", "baz"] assert headers.getall("accept") == headers.getall("ACCEPT") == ["some-value"]
async def test_rs_with_type_headers(): assert await RsWithType( RsFake( "201 CREATED", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), type_="text/html", ).headers() == CIMultiDict( {"foo": "bar", "accept": "some-value", "content-type": "text/html"} )
async def test_rs_without_headers_headers(): assert (sorted((await RsWithoutHeaders( RsFake( status="200 OK", headers=CIMultiDict({ "Accept": "Some-Value", "Hello": "Another-Value", "Foo": "Bar", }), ), ["Hello", "Accept", "Missing"], ).headers()).items()) == [("Foo", "Bar")])
async def test_text(): assert await Text( RsFake( "201 CREATED", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), charset="utf-8", ).headers() == CIMultiDict( { "foo": "bar", "accept": "some-value", "content-type": "text/plain; charset=utf-8", } )
async def test_rs_without_headers_case_insensitive(): headers = await RsWithoutHeaders( RsFake( "200 OK", MultiDict({ "foo": "bar", "Accept": "some-value" }), ), ["accept"], ).headers() assert headers["foo"] == headers["Foo"] == "bar" assert "accept" not in headers assert "Accept" not in headers
async def test_json(): assert await Json( RsFake( "201 CREATED", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), charset="utf-8", ).headers() == CIMultiDict( { "foo": "bar", "accept": "some-value", "content-type": "application/json; charset=utf-8", } )
async def test_rs_with_headers_headers_value(): assert ( sorted( ( await RsWithHeaders( RsFake( "200 OK", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), {"foo": "baz"}, ).headers() ).items() ) == [("accept", "some-value"), ("foo", "bar"), ("foo", "baz")] )
async def test_rs_with_type_headers_charset(): assert await RsWithType( RsFake( "201 CREATED", CIMultiDict({"foo": "bar", "accept": "some-value"}), ), type_="whatever", charset="utf-8", ).headers() == CIMultiDict( { "foo": "bar", "accept": "some-value", "content-type": "whatever; charset=utf-8", } )
async def test_rs_with_type_headers_delete_previous(): assert await RsWithType( RsFake( "201 CREATED", CIMultiDict( { "foo": "bar", "accept": "some-value", "CONTENT-TYPE": "application/json", } ), ), type_="text/html", ).headers() == CIMultiDict( {"foo": "bar", "accept": "some-value", "content-type": "text/html"} )
async def test_fk_fixed_works(): rs = RsFake("200 OK", MultiDict({"foo": "bar"})) assert await FkFixed(MgFixed(rs)).route(RqFake()) is rs
async def test_rs_with_body_body(): assert (await RsWithBody( TextBody("New text"), RsFake("200 Whatever", body=b"Fake body"), ).body().__anext__() == b"New text")