示例#1
0
文件: main.py 项目: p4l1ly/aresponses
 async def _find_response(self, request):
     host, path, path_qs, method = request.host, request.path, request.path_qs, request.method
     logger.info(f'Looking for match for {host} {path} {method}')
     i = 0
     host_matched = False
     path_matched = False
     for host_pattern, path_pattern, method_pattern, response, match_querystring in self._responses:
         if _text_matches_pattern(host_pattern, host):
             host_matched = True
             if (not match_querystring and _text_matches_pattern(path_pattern, path)) or\
                (match_querystring and _text_matches_pattern(path_pattern, path_qs)):
                 path_matched = True
                 if _text_matches_pattern(method_pattern, method.lower()):
                     del self._responses[i]
                     if callable(response):
                         if asyncio.iscoroutinefunction(response):
                             return await response(request)
                         return response(request)
                     elif isinstance(response, str):
                         return self.Response(body=response)
                     return response
         i += 1
     self._exception = Exception(
         f"No Match found for {host} {path} {method}.  Host Match: {host_matched}  Path Match: {path_matched}"
     )
     self._loop.stop()
     raise self._exception  # noqa
示例#2
0
文件: main.py 项目: p4l1ly/aresponses
    def _host_matches(self, match_host):
        match_host = match_host.lower()
        for host_pattern in self._host_patterns:
            if _text_matches_pattern(host_pattern, match_host):
                return True

        return False
示例#3
0
文件: main.py 项目: nolar/aresponses
    async def matches(self, request):
        path_to_match = request.path_qs if self.match_querystring else request.path

        if not _text_matches_pattern(self.host_pattern, request.host):
            return False

        if not _text_matches_pattern(self.path_pattern, path_to_match):
            return False

        if not _text_matches_pattern(self.method_pattern,
                                     request.method.lower()):
            return False

        if self.body_pattern != ANY:
            if not _text_matches_pattern(self.body_pattern, await
                                         request.text()):
                return False

        return True
示例#4
0
    async def _find_response(self, request):
        host, path, path_qs, method = request.host, request.path, request.path_qs, request.method
        logger.info(f"Looking for match for {host} {path} {method}")
        for i, (host_pattern, path_pattern, method_pattern, response,
                match_querystring, body) in enumerate(self._responses):
            if _text_matches_pattern(host_pattern, host):
                if (not match_querystring
                        and _text_matches_pattern(path_pattern, path)) or (
                            match_querystring
                            and _text_matches_pattern(path_pattern, path_qs)):
                    if _text_matches_pattern(method_pattern, method.lower()):
                        if (isinstance(body, NoBody)
                                or (isinstance(body, dict)
                                    and body == (await request.json()))
                                or (isinstance(body, str)
                                    and body == (await request.text()))):
                            self._responses.pop(i)

                            if callable(response):
                                if asyncio.iscoroutinefunction(response):
                                    return await response(request)
                                return response(request)

                            if isinstance(response, str):
                                return self.Response(body=response)

                            return response
        if request.body_exists:
            try:
                request_body = await request.json()
            except JSONDecodeError:
                request_body = await request.read()
        else:
            request_body = None
        pytest.fail(
            f"No Match found for \nhost: {host} \npath: {path} \nmethod: {method} \nbody: {request_body}\n"
        )