コード例 #1
0
    def test_request_headers(self, cors_config, expected):
        api_interface = ApiInterfaceBase(mock_endpoint)
        cors.CORS(api_interface, **cors_config)
        target = api_interface.middleware[0]
        http_request = MockRequest(headers={'Origin': 'http://my-domain.org'},
                                   current_operation=mock_endpoint)

        actual = target.request_headers(http_request)

        assert expected == actual
コード例 #2
0
    def test_pre_flight_headers(self, cors_config, expected):
        api_interface = ApiInterfaceBase(mock_endpoint)
        cors.CORS(api_interface, **cors_config)
        target = api_interface.middleware[0]
        http_request = MockRequest(headers={'Origin': 'http://my-domain.org'},
                                   current_operation=mock_endpoint)

        actual = target.pre_flight_headers(http_request, mock_endpoint.methods)

        assert 'GET, HEAD' == actual.pop('Allow')
        assert 'no-cache, no-store' == actual.pop('Cache-Control')
        assert expected == actual
コード例 #3
0
    def test_post_request(self, origins, method, expected):
        api_interface = ApiInterfaceBase(mock_endpoint)
        cors.CORS(api_interface, origins=origins)
        target = api_interface.middleware[0]

        http_request = MockRequest(headers={'Origin': 'http://my-domain.org'},
                                   method=method,
                                   current_operation=mock_endpoint)
        http_response = HttpResponse('')

        actual = target.post_request(http_request, http_response)

        assert actual is http_response
        assert expected == actual.headers.get('Access-Control-Allow-Origin')
コード例 #4
0
    def test_cors_options(self, origins, expected):
        api_interface = ApiInterfaceBase(mock_endpoint)
        cors.CORS(api_interface, origins=origins)
        target = api_interface.middleware[0]

        http_request = MockRequest(headers={'Origin': 'http://my-domain.org'},
                                   method=Method.OPTIONS)
        cors._MethodsMiddleware(
            (Method.GET, Method.HEAD)).pre_dispatch(http_request, None)

        actual = target.cors_options(http_request)

        assert isinstance(actual, HttpResponse)
        assert 'GET, HEAD' == actual.headers.pop('Allow')
        assert 'no-cache, no-store' == actual.headers.pop('Cache-Control')
        assert expected == actual.headers.get('Access-Control-Allow-Origin')
コード例 #5
0
    def test_new(self):
        api_interface = ApiInterfaceBase(mock_endpoint)

        actual = cors.CORS(api_interface, cors.AnyOrigin, 20, True,
                           ('X-Custom-A', ), ('X-Custom-B', ))

        assert actual is api_interface

        target = actual.middleware[0]

        assert isinstance(target, cors.CORS)
        assert target.origins is cors.AnyOrigin
        assert target.max_age == 20
        assert target.allow_credentials is True
        assert target.expose_headers == ('X-Custom-A', )
        assert target.allow_headers == ('X-Custom-B', )