示例#1
0
    def test_route__with_cors_options(self, *, web):
        handler = Mock()
        cors_options = {
            'http://example.com':
            ResourceOptions(
                allow_credentials=True,
                expose_headers='*',
                allow_headers='*',
                max_age=300,
                allow_methods='*',
            ),
        }
        web.web_app = Mock()
        web._cors = Mock()

        web.route(
            pattern='/foo/',
            handler=handler,
            cors_options=cors_options,
        )

        web.web_app.router.add_route.assert_has_calls(
            [call(method, '/foo/', ANY) for method in NON_OPTIONS_METHODS])
        web._cors.add.assert_has_calls([
            call(web.web_app.router.add_route(),
                 _prepare_cors_options(cors_options))
            for _ in NON_OPTIONS_METHODS
        ])
示例#2
0
def test__prepare_cors_options():
    x = ResourceOptions(
        allow_credentials=True,
        expose_headers=['foo', 'bar'],
        allow_headers='*',
        max_age=13124,
        allow_methods='*',
    )
    y = ResourceOptions(
        allow_credentials=False,
        expose_headers='*',
        allow_headers='*',
        max_age=None,
        allow_methods=['POST'],
    )
    z = aiohttp_cors.ResourceOptions(allow_credentials=False)
    opts = {
        'http://bob.example.com': x,
        'http://alice.example.com': y,
        'http://moo.example.com': z,
    }

    aiohttp_cors_opts = _prepare_cors_options(opts)
    x1 = aiohttp_cors_opts['http://bob.example.com']
    assert isinstance(x1, aiohttp_cors.ResourceOptions)
    x2 = aiohttp_cors_opts['http://alice.example.com']
    assert isinstance(x2, aiohttp_cors.ResourceOptions)
    assert aiohttp_cors_opts['http://moo.example.com']

    assert x1.allow_credentials
    assert x1.expose_headers == {'foo', 'bar'}
    assert x1.allow_headers == '*'
    assert x1.max_age == 13124
    assert x1.allow_methods == '*'

    assert not x2.allow_credentials
    assert x2.expose_headers == '*'
    assert x2.allow_headers == '*'
    assert x2.max_age is None
    assert x2.allow_methods == {'POST'}
示例#3
0
def test__prepare_cors_options():
    x = ResourceOptions(
        allow_credentials=True,
        expose_headers=["foo", "bar"],
        allow_headers="*",
        max_age=13124,
        allow_methods="*",
    )
    y = ResourceOptions(
        allow_credentials=False,
        expose_headers="*",
        allow_headers="*",
        max_age=None,
        allow_methods=["POST"],
    )
    z = aiohttp_cors.ResourceOptions(allow_credentials=False)
    opts = {
        "http://bob.example.com": x,
        "http://alice.example.com": y,
        "http://moo.example.com": z,
    }

    aiohttp_cors_opts = _prepare_cors_options(opts)
    x1 = aiohttp_cors_opts["http://bob.example.com"]
    assert isinstance(x1, aiohttp_cors.ResourceOptions)
    x2 = aiohttp_cors_opts["http://alice.example.com"]
    assert isinstance(x2, aiohttp_cors.ResourceOptions)
    assert aiohttp_cors_opts["http://moo.example.com"]

    assert x1.allow_credentials
    assert x1.expose_headers == {"foo", "bar"}
    assert x1.allow_headers == "*"
    assert x1.max_age == 13124
    assert x1.allow_methods == "*"

    assert not x2.allow_credentials
    assert x2.expose_headers == "*"
    assert x2.allow_headers == "*"
    assert x2.max_age is None
    assert x2.allow_methods == {"POST"}
示例#4
0
    def test_route__with_cors_options(self, *, web):
        handler = Mock()
        handler.get_methods = Mock(name="get_methods",
                                   return_value=set(
                                       {"GET", "PUT", "POST", "DELETE"}))
        cors_options = {
            "http://example.com":
            ResourceOptions(
                allow_credentials=True,
                expose_headers="*",
                allow_headers="*",
                max_age=300,
                allow_methods="*",
            ),
        }
        web.web_app = Mock()
        web._cors = Mock()

        web.route(
            pattern="/foo/",
            handler=handler,
            cors_options=cors_options,
        )

        web.web_app.router.add_route.assert_has_calls(
            [call(method, "/foo/", ANY) for method in NON_OPTIONS_METHODS],
            any_order=True,
        )
        web._cors.add.assert_has_calls(
            [
                call(web.web_app.router.add_route(),
                     _prepare_cors_options(cors_options))
                for _ in NON_OPTIONS_METHODS
            ],
            any_order=True,
        )