コード例 #1
0
def test_cache_cleanup(empty_proxy: PaywalledProxy, usession: uSession,
                       api_endpoint_address: str):
    now = time.time()
    es_mock = ElasticsearchBackend(None)

    api_path = 'http://' + api_endpoint_address
    resource_url = '/some_index/some_type/_search'
    url = api_path + resource_url

    bodies = [
        {
            'query': 'query something'
        },
        {
            'query': 'query some other thing'
        },
        {
            'query': 'query some third thing'
        },
    ]
    requests = [
        Request.from_values(method='GET',
                            base_url=api_path,
                            path=resource_url,
                            content_type='application/json',
                            data=json.dumps(bodies[0])),
        Request.from_values(method='GET',
                            base_url=api_path,
                            path=resource_url,
                            content_type='application/json',
                            data=json.dumps(bodies[1])),
        Request.from_values(method='GET',
                            base_url=api_path,
                            path=resource_url,
                            content_type='application/json',
                            data=json.dumps(bodies[2]))
    ]
    resources = [
        Resource(content='success1', price=4, expires_at=now - 100),
        Resource(content='success2', price=4, expires_at=now + 100),
        Resource(content='success3', price=4, expires_at=now - 100),
    ]
    es_mock.search = mock.Mock(return_value=resources[1])

    server = APIServer(empty_proxy, es=es_mock)
    server.resource_cache.update({
        ExpensiveElasticsearch.get_request_key(requests[0]):
        resources[0],
        ExpensiveElasticsearch.get_request_key(requests[1]):
        resources[1],
        ExpensiveElasticsearch.get_request_key(requests[2]):
        resources[2]
    })
    assert len(server.resource_cache) == 3

    response = usession.get(url, json=bodies[1])
    assert response.json() == 'success2'
    assert len(server.resource_cache) == 1
コード例 #2
0
        def gordo_ml_server_callback(request):
            """
            Redirect calls to a gordo server to reflect what the local testing app gives
            will call the correct path (assuminng only single level paths) on the
            gordo app.
            """
            if request.method in ("GET", "POST"):

                kwargs = dict()
                if request.body:
                    flask_request = Request.from_values(
                        content_length=len(request.body),
                        input_stream=io.BytesIO(request.body),
                        content_type=request.headers["Content-Type"],
                        method=request.method,
                    )
                    if flask_request.json:
                        kwargs["json"] = flask_request.json
                    else:
                        kwargs["data"] = {
                            k: (io.BytesIO(f.read()), f.filename)
                            for k, f in flask_request.files.items()
                        }

                with TEST_SERVER_MUTEXT:
                    resp = getattr(gordo_server_app, request.method.lower())(
                        request.path_url,
                        headers=dict(request.headers),
                        **kwargs)
                return (
                    resp.status_code,
                    resp.headers,
                    json.dumps(resp.json)
                    if resp.json is not None else resp.data,
                )
コード例 #3
0
def test_request_hashing():
    get = Request.from_values(method='GET', path='/somepath')
    get_key = ExpensiveElasticsearch.get_request_key(get)
    assert get_key
    get2 = Request.from_values(method='get', path='/somepath')
    get2_key = ExpensiveElasticsearch.get_request_key(get2)
    assert get_key == get2_key

    post = Request.from_values(method='POST',
                               path='/somepath',
                               content_type='application/json',
                               data=json.dumps(
                                   dict(a=1, b='value', c=dict(c=3, x=4))))
    post_key = ExpensiveElasticsearch.get_request_key(post)
    assert post_key
    post2 = Request.from_values(method='POST',
                                path='/somepath',
                                content_type='application/json',
                                data=json.dumps(
                                    dict(a=1, b='value', c=dict(c=3, x=4))))
    post2_key = ExpensiveElasticsearch.get_request_key(post2)
    assert post_key == post2_key
コード例 #4
0
def _request_from_url(url, current_request):
    """Build a Request instance based on the URL and the current request.

    The current request is the originating request from were we extract
    information such as body, content type and method.
    """
    parts = urlparse(url)

    return Request.from_values(
        base_url="%s://%s" % (parts.scheme, parts.netloc),
        path=parts.path,
        query_string=parts.query,
        content_length=len(current_request.data),
        input_stream=StringIO(current_request.data),
        content_type=current_request.content_type,
        method=current_request.method)
コード例 #5
0
    def test_echo_signature(self):
        response = self.client.open("/datetime/v1/echo", method="GET")
        request = Request.from_values(
            base_url="http://localhost",
            path="/datetime/v1/echo",
            method="GET",
        )
        self.assert200(response,
                       "Response body is : " + response.data.decode("utf-8"))
        s_header = response.headers.get("Signature")
        assert s_header, response.headers

        p = parse_signature_header(s_header)
        ss = Signature(**p)
        sstring = ss.signature_string(request.method, request.path,
                                      response.headers)
        verify_string(load_pubkey(Path("rsa.pub").read_bytes()), sstring,
                      p["signature"])
コード例 #6
0
def callback(name):
    endpoint, view_args, req_args = recall_own_flow()
    if request.args.get('error'):
        return auth_server.create_authorization_response() # access_denied error response

    fed_client = federation.get(name)
    try:
        fed_client.authorize_access_token()
    except OAuthError:
        return auth_server.create_authorization_response()

    user_info = fed_client.fetch_user_info()
    user = User(user_info.sub, user_info)

    auth_url = url_for(endpoint,**view_args,**req_args,_external=True)
    base_url, query_string = auth_url.split('?',1)
    auth_req =  Request.from_values(base_url=base_url, query_string=query_string.encode())
#    g.redirect_uri = req_args.get('redirect_uri')
    return auth_server.create_authorization_response(auth_req, grant_user=user)
コード例 #7
0
    def test_extract_next_url_safe(self):
        req = Request.from_values('/?next=/help')

        self.assertEqual('/help', extract_next_url(req))
コード例 #8
0
 def test_extract_next_url_unsafe(self):
     req = Request.from_values('/?next={}'.format(self.fake.url()))
     self.assertEqual('http://localhost:5000/', extract_next_url(req))
コード例 #9
0
 def test_extract_next_url_blank(self):
     req = Request.from_values('')
     self.assertEqual('http://localhost:5000/', extract_next_url(req))
コード例 #10
0
ファイル: psi.py プロジェクト: hhy5277/python-script
def test_run_http():
    from flask import Request
    _request = Request.from_values(json = { "url": URL })
    run_http(_request)
コード例 #11
0
    def test_extract_next_url_blank(self):
        req = Request.from_values('')

        self.assertEqual('/', extract_next_url(req))