Ejemplo n.º 1
0
 def _request(self, request, validate=True):
     if request['endpoint'].endswith('/'):
         request['endpoint'] = request['endpoint'][:-1]
     if request['endpoint'].startswith(('http://', 'https://')):
         full_url = request['endpoint']
     else:
         if not request['endpoint'].startswith('/'):
             request['endpoint'] = '/' + request['endpoint']
         full_url = self.url + request['endpoint']
     try:
         response = client(request['method'],
                           full_url,
                           params=request['query'],
                           json=request['body'],
                           headers=request['headers'],
                           proxies=request['proxies'],
                           cert=request['cert'],
                           timeout=tuple(request['timeout']),
                           allow_redirects=request['allowRedirects'],
                           verify=request['sslVerify'])
     except Timeout as e:
         raise AssertionError("{} request to {} timed out:\n{}".format(
             request['method'], full_url, e))
     utc_datetime = datetime.now(timezone.utc)
     request['timestamp'] = {
         'utc': utc_datetime.isoformat(),
         'local': utc_datetime.astimezone().isoformat()
     }
     if validate and self.spec:
         self._assert_spec(self.spec, response)
     instance = self._instantiate(request, response, validate)
     self.instances.append(instance)
     return instance
Ejemplo n.º 2
0
 def _request(self, **fields):
     request = deepcopy(self.request)
     request.update(fields)
     if request['endpoint'].endswith('/'):
         request['endpoint'] = request['endpoint'][:-1]
     if request['endpoint'].startswith(('http://', 'https://')):
         full_url = request['endpoint']
     else:
         if not request['endpoint'].startswith('/'):
             request['endpoint'] = '/' + request['endpoint']
         full_url = self.url + request['endpoint']
     if not request['sslVerify']:
         disable_warnings()
     auth = tuple(request['auth']) if request['auth'] else None
     timeout = tuple(request['timeout']) if request['timeout'] else None
     try:
         response = client(request['method'], full_url,
                           params=request['query'],
                           json=request['body'],
                           headers=request['headers'],
                           proxies=request['proxies'],
                           auth=auth,
                           cert=request['cert'],
                           timeout=timeout,
                           allow_redirects=request['allowRedirects'],
                           verify=request['sslVerify'])
     except Timeout as e:
         raise AssertionError("{} request to {} timed out:\n{}".format(
             request['method'], full_url, e))
     utc_datetime = datetime.now(timezone.utc)
     request['timestamp'] = {
         'utc': utc_datetime.isoformat(),
         'local': utc_datetime.astimezone().isoformat()
     }
     return self._instantiate(request, response)
Ejemplo n.º 3
0
def _request_multipart_file(rest_instance,
                            endpoint,
                            request,
                            files,
                            validate=True):
    if not endpoint.startswith(('http://', 'https://')):
        base_url = rest_instance.request[
            'scheme'] + "://" + rest_instance.request['netloc']
        if not endpoint.startswith('/'):
            endpoint = "/" + endpoint
        endpoint = urljoin(base_url, rest_instance.request['path']) + endpoint
    request['url'] = endpoint
    url_parts = urlparse(request['url'])
    request['scheme'] = url_parts.scheme
    request['netloc'] = url_parts.netloc
    request['path'] = url_parts.path
    request['headers'].pop("Content-Type", None)
    try:
        response = client(request['method'],
                          request['url'],
                          data=request['data'],
                          files=files,
                          headers=request['headers'],
                          proxies=request['proxies'],
                          cert=request['cert'],
                          timeout=tuple(request['timeout']),
                          allow_redirects=request['allowRedirects'],
                          verify=request['sslVerify'])
    except SSLError as e:
        raise AssertionError("%s to %s SSL certificate verify failed:\n%s" %
                             (request['method'], request['url'], e))
    except Timeout as e:
        raise AssertionError("%s to %s timed out:\n%s" %
                             (request['method'], request['url'], e))
    utc_datetime = datetime.now(tz=utc)
    request['timestamp'] = {
        'utc': utc_datetime.isoformat(),
        'local': utc_datetime.astimezone(get_localzone()).isoformat()
    }
    if validate and rest_instance.spec:
        rest_instance._assert_spec(rest_instance.spec, response)
    instance = rest_instance._instantiate(request, response, validate)
    rest_instance.instances.append(instance)
    return instance
Ejemplo n.º 4
0
 def _request(self, endpoint, request, validate=True):
     if endpoint.endswith('/'):
         endpoint = endpoint[:-1]
     if not endpoint.startswith(('http://', 'https://')):
         base_url = self.request['scheme'] + "://" + self.request['netloc']
         if not endpoint.startswith('/'):
             endpoint = "/" + endpoint
         endpoint = urljoin(base_url, self.request['path']) + endpoint
     request['url'] = endpoint
     url_parts = urlparse(request['url'])
     request['scheme'] = url_parts.scheme
     request['netloc'] = url_parts.netloc
     request['path'] = url_parts.path
     try:
         response = client(request['method'],
                           request['url'],
                           params=request['query'],
                           json=request['body'],
                           headers=request['headers'],
                           proxies=request['proxies'],
                           cert=request['cert'],
                           timeout=tuple(request['timeout']),
                           allow_redirects=request['allowRedirects'],
                           verify=request['sslVerify'])
     except SSLError as e:
         raise AssertionError(
             "%s to %s SSL certificate verify failed:\n%s" %
             (request['method'], request['url'], e))
     except Timeout as e:
         raise AssertionError("%s to %s timed out:\n%s" %
                              (request['method'], request['url'], e))
     utc_datetime = datetime.now(tz=utc)
     request['timestamp'] = {
         'utc': utc_datetime.isoformat(),
         'local': utc_datetime.astimezone(get_localzone()).isoformat()
     }
     if validate and self.spec:
         self._assert_spec(self.spec, response)
     instance = self._instantiate(request, response, validate)
     self.instances.append(instance)
     return instance