Esempio n. 1
0
 def url_for(self, *, filename, append_version=None):
     if append_version is None:
         append_version = self._append_version
     if isinstance(filename, Path):
         filename = str(filename)
     while filename.startswith('/'):
         filename = filename[1:]
     filename = '/' + filename
     url = self._prefix + URL(filename).raw_path
     url = URL(url)
     if append_version is True:
         try:
             if filename.startswith('/'):
                 filename = filename[1:]
             filepath = self._directory.joinpath(filename).resolve()
             if not self._follow_symlinks:
                 filepath.relative_to(self._directory)
         except (ValueError, FileNotFoundError):
             # ValueError for case when path point to symlink
             # with follow_symlinks is False
             return url  # relatively safe
         if filepath.is_file():
             # TODO cache file content
             # with file watcher for cache invalidation
             with open(str(filepath), mode='rb') as f:
                 file_bytes = f.read()
             h = self._get_file_hash(file_bytes)
             url = url.with_query({self.VERSION_KEY: h})
             return url
     return url
Esempio n. 2
0
 def make_url(self, path):
     url = URL(path)
     if not self.skip_url_asserts:
         assert not url.is_absolute()
         return self._root.join(url)
     else:
         return URL(str(self._root) + path)
Esempio n. 3
0
    def request(self, method, url, *,
                auth=None,
                status=200,
                text=None,
                data=None,
                content=None,
                json=None,
                params=None,
                headers={},
                exc=None,
                cookies=None):
        """Mock a request."""
        if json is not None:
            text = _json.dumps(json)
        if text is not None:
            content = text.encode('utf-8')
        if content is None:
            content = b''

        if not isinstance(url, retype):
            url = URL(url)
        if params:
            url = url.with_query(params)

        self._mocks.append(AiohttpClientMockResponse(
            method, url, status, content, cookies, exc, headers))
Esempio n. 4
0
def test_with_query_only_single_arg_is_supported():
    url = URL("http://example.com")
    u1 = url.with_query(b=3)
    u2 = URL("http://example.com/?b=3")
    assert u1 == u2
    with pytest.raises(ValueError):
        url.with_query("a=1", "a=b")
Esempio n. 5
0
    def maker(method, path, query_params={}, headers=None, match_info=None, loop=None):
        path = URL(path)
        if query_params:
            path = path.with_query(query_params)

        if headers is None:
            headers = CIMultiDict(
                {
                    "HOST": "server.example.com",
                    "UPGRADE": "websocket",
                    "CONNECTION": "Upgrade",
                    "SEC-WEBSOCKET-KEY": "dGhlIHNhbXBsZSBub25jZQ==",
                    "ORIGIN": "http://example.com",
                    "SEC-WEBSOCKET-PROTOCOL": "chat, superchat",
                    "SEC-WEBSOCKET-VERSION": "13",
                }
            )

        writer = mock.Mock()
        writer.write_headers = make_mocked_coro(None)
        writer.write = make_mocked_coro(None)
        writer.drain = make_mocked_coro(None)
        transport = mock.Mock()
        transport._drain_helper = make_mocked_coro()
        ret = make_mocked_request(method, str(path), headers, writer=writer, loop=loop)

        if match_info is None:
            match_info = UrlMappingMatchInfo({}, mock.Mock())
            match_info.add_app(app)
        ret._match_info = match_info
        return ret
Esempio n. 6
0
def test_relative_abs_parts_are_removed():
    url = URL("http://*****:*****@example.com:8080/path?a=b#frag")
    rel = url.relative()
    assert not rel.scheme
    assert not rel.user
    assert not rel.password
    assert not rel.host
    assert not rel.port
Esempio n. 7
0
    def extract_facebook():
        facebook, = (extract_other_item('FACEBOOK:', link=True) or
                     [extract_other_item('FACEBOOK:')])
        if not facebook:
            return

        facebook = URL('https://' + facebook.lstrip('/')
                       if not facebook.startswith('http') else facebook)
        return str(URL('https://www.facebook.com/').with_path(facebook.path))
Esempio n. 8
0
def test_proxies_from_env_http_with_auth(mocker):
    url = URL('http://*****:*****@aiohttp.io/path')
    mocker.patch.dict(os.environ, {'http_proxy': str(url)})
    ret = helpers.proxies_from_env()
    assert ret.keys() == {'http'}
    assert ret['http'].proxy == url.with_user(None)
    proxy_auth = ret['http'].proxy_auth
    assert proxy_auth.login == 'user'
    assert proxy_auth.password == 'pass'
    assert proxy_auth.encoding == 'latin1'
Esempio n. 9
0
def test_update_query():
    url = URL("http://example.com/")
    assert str(url.update_query({"a": "1"})) == "http://example.com/?a=1"
    assert str(URL("test").update_query(a=1)) == "test?a=1"

    url = URL("http://example.com/?foo=bar")
    expected_url = URL("http://example.com/?foo=bar&baz=foo")

    assert url.update_query({"baz": "foo"}) == expected_url
    assert url.update_query(baz="foo") == expected_url
    assert url.update_query("baz=foo") == expected_url
Esempio n. 10
0
def test_response_real_url(loop, session) -> None:
    url = URL('http://def-cl-resp.org/#urlfragment')
    response = ClientResponse('get', url,
                              request_info=mock.Mock(),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=loop,
                              session=session)
    assert response.url == url.with_fragment(None)
    assert response.real_url == url
Esempio n. 11
0
def test_build_with_query_and_query_string():
    with pytest.raises(ValueError):
        URL.build(
            scheme="http",
            host="127.0.0.1",
            user="******",
            password="******",
            port=8000,
            path="/index.html",
            query=dict(arg="value1"),
            query_string="arg=value1",
            fragment="top",
        )
Esempio n. 12
0
def test_build_with_query_and_query_string():
    with pytest.raises(ValueError):
        URL.build(
            scheme='http',
            host='127.0.0.1',
            user='******',
            password='******',
            port=8000,
            path='/index.html',
            query=dict(arg="value1"),
            query_string="arg=value1",
            fragment="top"
        )
Esempio n. 13
0
def test_build_drop_dots():
    u = URL.build(
        scheme='http',
        host='example.com',
        path='/path/../to',
    )
    assert str(u) == 'http://example.com/to'
Esempio n. 14
0
 async def start_server(self,
                        loop: Optional[asyncio.AbstractEventLoop]=None,
                        **kwargs: Any) -> None:
     if self.runner:
         return
     self._loop = loop
     self._ssl = kwargs.pop('ssl', None)
     self.runner = await self._make_runner(**kwargs)
     await self.runner.setup()
     if not self.port:
         self.port = 0
     _sock = get_port_socket(self.host, self.port)
     self.host, self.port = _sock.getsockname()[:2]
     site = SockSite(self.runner, sock=_sock, ssl_context=self._ssl)
     await site.start()
     server = site._server
     assert server is not None
     sockets = server.sockets
     assert sockets is not None
     self.port = sockets[0].getsockname()[1]
     if self.scheme is sentinel:
         if self._ssl:
             scheme = 'https'
         else:
             scheme = 'http'
         self.scheme = scheme
     self._root = URL('{}://{}:{}'.format(self.scheme,
                                          self.host,
                                          self.port))
Esempio n. 15
0
 def apost(self, headers={}, data=None, **kw):
     session = aiohttp.ClientSession()
     url = URL(self.url)
     if kw:
         url = url.with_query(**kw)
     logger.debug("POST %s", url)
     try:
         response = yield from session.post(
             url, headers=headers, data=data, timeout=10,
         )
         payload = yield from response.read()
     finally:
         yield from session.close()
     response.raise_for_status()
     payload = payload.decode('utf-8')
     return Payload.factory(response.status, response.headers, payload)
Esempio n. 16
0
 def aget(self, **kw):
     session = aiohttp.ClientSession()
     url = URL(self.url)
     if kw:
         url = url.with_query(**kw)
     logger.debug("GET %s", url)
     try:
         response = yield from session.get(url, timeout=10)
         payload = yield from response.read()
     finally:
         yield from session.close()
     response.raise_for_status()
     payload = payload.decode('utf-8')
     if response.content_type == 'text/x-python':
         payload = ast.literal_eval(payload)
     return Payload.factory(response.status, response.headers, payload)
Esempio n. 17
0
    def __init__(self, path: str, *, name: Optional[str]=None) -> None:
        super().__init__(name=name)
        pattern = ''
        formatter = ''
        for part in ROUTE_RE.split(path):
            match = self.DYN.fullmatch(part)
            if match:
                pattern += '(?P<{}>{})'.format(match.group('var'), self.GOOD)
                formatter += '{' + match.group('var') + '}'
                continue

            match = self.DYN_WITH_RE.fullmatch(part)
            if match:
                pattern += '(?P<{var}>{re})'.format(**match.groupdict())
                formatter += '{' + match.group('var') + '}'
                continue

            if '{' in part or '}' in part:
                raise ValueError("Invalid path '{}'['{}']".format(path, part))

            path = URL.build(path=part).raw_path
            formatter += path
            pattern += re.escape(path)

        try:
            compiled = re.compile(pattern)
        except re.error as exc:
            raise ValueError(
                "Bad pattern '{}': {}".format(pattern, exc)) from None
        assert compiled.pattern.startswith(PATH_SEP)
        assert formatter.startswith('/')
        self._pattern = compiled
        self._formatter = formatter
Esempio n. 18
0
    def __init__(self, method: str, url: URL, *,
                 writer: 'asyncio.Task[None]',
                 continue100: Optional['asyncio.Future[bool]'],
                 timer: BaseTimerContext,
                 request_info: RequestInfo,
                 traces: List['Trace'],
                 loop: asyncio.AbstractEventLoop,
                 session: 'ClientSession') -> None:
        assert isinstance(url, URL)

        self.method = method
        self.cookies = SimpleCookie()

        self._real_url = url
        self._url = url.with_fragment(None)
        self._body = None  # type: Any
        self._writer = writer  # type: Optional[asyncio.Task[None]]
        self._continue = continue100  # None by default
        self._closed = True
        self._history = ()  # type: Tuple[ClientResponse, ...]
        self._request_info = request_info
        self._timer = timer if timer is not None else TimerNoop()
        self._cache = {}  # type: Dict[str, Any]
        self._traces = traces
        self._loop = loop
        # store a reference to session #1985
        self._session = session  # type: Optional[ClientSession]
        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Esempio n. 19
0
 def _match(self, path: str) -> Optional[Dict[str, str]]:
     match = self._pattern.fullmatch(path)
     if match is None:
         return None
     else:
         return {key: URL.build(path=value, encoded=True).path
                 for key, value in match.groupdict().items()}
Esempio n. 20
0
def test_build_query_quoting():
    u = URL.build(scheme="http", host="127.0.0.1", path="/файл.jpg", query="arg=Привет")

    assert u == URL("http://127.0.0.1/файл.jpg?arg=Привет")
    assert str(u) == (
        "http://127.0.0.1/%D1%84%D0%B0%D0%B9%D0%BB.jpg?"
        "arg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82"
    )
Esempio n. 21
0
def test_query_str():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        path='/',
        query_string="arg=value1"
    )
    assert str(u) == 'http://127.0.0.1/?arg=value1'
Esempio n. 22
0
    async def match_request(self, method, url, *, data=None, auth=None,
                            params=None, headers=None, allow_redirects=None,
                            timeout=None, json=None):
        """Match a request against pre-registered requests."""
        data = data or json
        url = URL(url)
        if params:
            url = url.with_query(params)

        for response in self._mocks:
            if response.match_request(method, url, params):
                self.mock_calls.append((method, url, data, headers))

                if response.exc:
                    raise response.exc
                return response

        assert False, "No mock registered for {} {} {}".format(method.upper(),
                                                               url, params)
Esempio n. 23
0
def test_build_already_encoded():
    # resulting URL is invalid but not encoded
    u = URL.build(
        scheme="http",
        host="историк.рф",
        path="/путь/файл",
        query_string="ключ=знач",
        fragment="фраг",
        encoded=True,
    )
    assert str(u) == "http://историк.рф/путь/файл?ключ=знач#фраг"
Esempio n. 24
0
def test_build_path_quoting():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        path='/файл.jpg',
        query=dict(arg="Привет")
    )

    assert u == URL('http://127.0.0.1/файл.jpg?arg=Привет')
    assert str(u) == ('http://127.0.0.1/%D1%84%D0%B0%D0%B9%D0%BB.jpg?'
                      'arg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
Esempio n. 25
0
 def start_server(self, **kwargs):
     if self.server:
         return
     self.port = unused_port()
     self._root = URL('{}://{}:{}'.format(self.scheme,
                                          self.host,
                                          self.port))
     self.handler = self.app.make_handler(**kwargs)
     self.server = yield from self._loop.create_server(self.handler,
                                                       self.host,
                                                       self.port)
Esempio n. 26
0
def run_app(app, *, host='0.0.0.0', port=None,
            shutdown_timeout=60.0, ssl_context=None,
            print=print, backlog=128, access_log_format=None,
            access_log=access_logger):
    """Run an app locally"""
    if port is None:
        if not ssl_context:
            port = 8080
        else:
            port = 8443

    loop = app.loop

    make_handler_kwargs = dict()
    if access_log_format is not None:
        make_handler_kwargs['access_log_format'] = access_log_format
    handler = app.make_handler(access_log=access_log,
                               **make_handler_kwargs)
    server = loop.create_server(handler, host, port, ssl=ssl_context,
                                backlog=backlog)
    srv, startup_res = loop.run_until_complete(asyncio.gather(server,
                                                              app.startup(),
                                                              loop=loop))

    scheme = 'https' if ssl_context else 'http'
    url = URL('{}://localhost'.format(scheme))
    url = url.with_host(host).with_port(port)
    print("======== Running on {} ========\n"
          "(Press CTRL+C to quit)".format(url))

    try:
        loop.run_forever()
    except KeyboardInterrupt:  # pragma: no cover
        pass
    finally:
        srv.close()
        loop.run_until_complete(srv.wait_closed())
        loop.run_until_complete(app.shutdown())
        loop.run_until_complete(handler.finish_connections(shutdown_timeout))
        loop.run_until_complete(app.cleanup())
    loop.close()
Esempio n. 27
0
def test_build_with_all():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        user='******',
        password='******',
        port=8000,
        path='/index.html',
        query_string="arg=value1",
        fragment="top"
    )
    assert str(u) == 'http://*****:*****@127.0.0.1:8000/index.html?arg=value1#top'
Esempio n. 28
0
def test_build_with_all():
    u = URL.build(
        scheme="http",
        host="127.0.0.1",
        user="******",
        password="******",
        port=8000,
        path="/index.html",
        query_string="arg=value1",
        fragment="top",
    )
    assert str(u) == "http://*****:*****@127.0.0.1:8000/index.html?arg=value1#top"
Esempio n. 29
0
 def __init__(self, runner, sock, *,
              shutdown_timeout=60.0, ssl_context=None,
              backlog=128):
     super().__init__(runner, shutdown_timeout=shutdown_timeout,
                      ssl_context=ssl_context, backlog=backlog)
     self._sock = sock
     scheme = 'https' if self._ssl_context else 'http'
     if hasattr(socket, 'AF_UNIX') and sock.family == socket.AF_UNIX:
         name = '{}://unix:{}:'.format(scheme, sock.getsockname())
     else:
         host, port = sock.getsockname()[:2]
         name = str(URL.build(scheme=scheme, host=host, port=port))
     self._name = name
Esempio n. 30
0
    async def resolve(self, request: Request) -> _Resolve:
        path = request.rel_url.raw_path
        method = request.method
        allowed_methods = set(self._routes)
        if not path.startswith(self._prefix):
            return None, set()

        if method not in allowed_methods:
            return None, allowed_methods

        match_dict = {'filename': URL.build(path=path[len(self._prefix)+1:],
                                            encoded=True).path}
        return (UrlMappingMatchInfo(match_dict, self._routes[method]),
                allowed_methods)
Esempio n. 31
0
 def store(self, store: Union[URL, str]) -> None:
     self._store = URL(store)