Example #1
0
 def test_fetch_post_file(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'hosts': open('/etc/hosts', 'r')}, 'POST')
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'hosts': open('/etc/hosts', 'r'),
                                   'unicode': u'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
Example #2
0
 def test_fetch_get_basic(self):
     status, header, body = fetch('http://www.google.com')
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2', {'q': 'hudora'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2', {'keyg': 'value', 'just a test': 212})
     assert status == 200
Example #3
0
 def test_fetch_post_file(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'hosts': open('/etc/hosts', 'r')},
                                  'POST')
     status, header, body = fetch('http://www.postbin.org/186ndf2', {
         'hosts': open('/etc/hosts', 'r'),
         'unicode': u'Iñtërnâtiônàlizætiøn'
     }, 'POST')
     assert status == 201
Example #4
0
 def test_fetch_get_unicode(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühg1': 'Iñtërnâtiônàlizætiøn'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühg2': u'Iñtërnâtiônàlizætiøn'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {u'kühg3': 'Iñtërnâtiônàlizætiøn'})
     assert status == 200
Example #5
0
 def test_fetch_get_unicode(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühg1': 'Iñtërnâtiônàlizætiøn'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühg2': u'Iñtërnâtiônàlizætiøn'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {u'kühg3': 'Iñtërnâtiônàlizætiøn'})
     assert status == 200
Example #6
0
 def test_fetch_get_basic(self):
     status, header, body = fetch('http://www.google.com')
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'q': 'hudora'})
     assert status == 200
     status, header, body = fetch('http://www.postbin.org/186ndf2', {
         'keyg': 'value',
         'just a test': 212
     })
     assert status == 200
Example #7
0
 def test_fetch_post_unicode(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kuehp1': 'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühp2': 'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühp3': u'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {u'kühp4': u'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
    def generate_pdf_server(self, design, xpath, xmldata, multi=False):
        """Generate report via pyJasperServer."""
        if multi:
            content = dict(designs=design, xpath=xpath, xmldata=xmldata)
        else:
            content = dict(design=design, xpath=xpath, xmldata=xmldata)
        status, _headers, content = fetch(self.serverurl, content, 'POST')

        content = dict(design=design, xpath=xpath, xmldata=xmldata)
        status, headers, content = fetch(self.serverurl, content=content, method='POST', multipart=multi)
        if not status == 200:
            raise JasperException("%s -- %r" % (content, status))
        return content
Example #9
0
    def responds_with_valid_links(self):
        if NO_LINK_VALIDATION:
            return self
        links = extract_links(self.content, self.url)
        for link in links:
            if link in brokenlinks:
                # no need to check again
                brokenlinks.setdefault(link, set()).add(self.url)
            elif link not in oklinks:
                try:
                    status, _responseheaders, _content = fetch(
                        link,
                        headers=dict(
                            referer=self.url, Cookie=self.headers.get('set-cookie', '')
                        ),
                        content='', method='GET', multipart=False, ua='', timeout=30)
                except (IOError, huTools.http._httplib2.ServerNotFoundError):
                    status = 600
                except (huTools.http._httplib2.RedirectLimit):
                    status = 700

                if status == 200:
                    oklinks.add(link)
                else:
                    brokenlinks.setdefault(link, set()).add(self.url)
                if status == 700:
                    print 'too many redirects on %s' % link
                self.expect_condition(
                    status in (200, 401, 405, 700), 'invalid (%r) link to %r' % (status, link))
Example #10
0
def generate_report(reportdesign, xpath, xmldata, url=None, sign_keyname='', sign_reason='',
                    callback='', metadata=None):
    """Generates a report, returns the PDF.

    reportdesign, xpath and xmldata - necessary data to generate a JasperReport.
    url - points to a jasper server
    sign_keyname - key for the signature lying on the server
    sign_reason - reason to be sent within the signed document
    callback - url to where the generated report will be sent
    metadata - PDF metadata

    Return value is pdf data stripped of timestamps for modification and creaton dates.
    If a callback is given, the server will sent the pdf data to the given URL instead.
    """
    url = _find_server(url)
    content = dict(design=reportdesign, xpath=xpath, xmldata=xmldata)
    if callback:
        content['callback'] = callback
    if sign_keyname:
        content['sign_keyname'] = sign_keyname
        if not sign_reason:
            raise ValueError('reason is needed when signing documents!')
        content['sign_reason'] = sign_reason
    if metadata:
        content['metadata'] = urllib.urlencode(metadata)

    status, _headers, content = fetch(url, content, 'POST')
    if not status == 200:
        raise JasperException("%s -- %r" % (content, status))
    # remove Timestamps so the same reports hash the same.
    return re.sub("/(ModDate|CreationDate)\(D:\d{14}[+-]\d{2}'\d{2}'\)", '', content)
Example #11
0
    def generate_pdf_server(self, design, xpath, xmldata, multi=False):
        """Generate report via pyJasperServer."""
        if multi:
            content = dict(designs=design, xpath=xpath, xmldata=xmldata)
        else:
            content = dict(design=design, xpath=xpath, xmldata=xmldata)
        status, _headers, content = fetch(self.serverurl, content, 'POST')

        content = dict(design=design, xpath=xpath, xmldata=xmldata)
        status, headers, content = fetch(self.serverurl,
                                         content=content,
                                         method='POST',
                                         multipart=multi)
        if not status == 200:
            raise JasperException("%s -- %r" % (content, status))
        return content
Example #12
0
    def post_message(self, guid, content_type, content, ignore_duplication_errors=False):
        """Veröffentlicht eine Nachricht auf der FMTP-Queue.

        guid: Eindeutiger Bezeichner für die Nachricht
        content_type: Content-Type der Nachricht, beispielsweise text/plain
        content: Inhalt der Nachricht
        ignore_duplication_errors: Wenn True, werden die exceptions FmtpMessageExists und FmtpMessageDeleted
            nicht geworfen.
        """
        url = self.queue_url + guid + '/'
        headers = {'Content-Type': content_type}

        status, headers, body = http.fetch(url, method='POST', credentials=self.credentials, headers=headers,
                                                                                              content=content)
        if status == 409:
            if not ignore_duplication_errors:
                raise FmtpMessageExists('Message %s already exists' % url)
            else:
                logger.info('Message %s already in queue %s, ignored', guid, self.queue_url)
        elif status == 410:
            if not ignore_duplication_errors:
                raise FmtpMessageDeleted('Message %s is deleted' % url)
            else:
                logger.info('Message %s already in queue %s, was also deleted, ignored', guid, self.queue_url)
        elif status != 201:
            raise FmtpHttpError('expected 201 when posting to %s, got %s' % (url, status))
Example #13
0
 def test_fetch_post_unicode(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kuehp1': 'Iñtërnâtiônàlizætiøn'},
                                  'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühp2': 'Iñtërnâtiônàlizætiøn'}, 'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {'kühp3': u'Iñtërnâtiônàlizætiøn'},
                                  'POST')
     assert status == 201
     status, header, body = fetch('http://www.postbin.org/186ndf2',
                                  {u'kühp4': u'Iñtërnâtiônàlizætiøn'},
                                  'POST')
     assert status == 201
Example #14
0
    def __call__(self, fnc, *args, **kwargs):
        """Do a remote procedure call via HTTP"""

        warnings.warn("hutools.rest is deprecated", DeprecationWarning, stacklevel=2)
        headers = {"content-type": "application/json"}
        path = os.path.join(self.endpoint, build_url(fnc, *args), "")
        if "params" in kwargs:
            path = "%s?%s" % (path, urllib.urlencode(kwargs.pop("params")))

        if kwargs:
            content = json.dumps(kwargs)
            method = "POST"
        else:
            content = None
            method = "GET"

        url = urlparse.urljoin(self.endpoint, path)
        credentials = "%s:%s" % (self.username, self.password)
        status, headers, content = fetch(url, method=method, content=content, headers=headers, credentials=credentials)
        if status == 201:
            return {"status": 201, "success": "created"}
        if status == 401:
            raise ClientUnauthorizedExecption()
        elif status == 403:
            raise ClientForbiddenException()
        elif status == 404:
            raise ClientNotFoundException()
        elif status == 500:
            raise ClientServerErrorExecption()

        try:
            return json.loads(content)
        except Exception:
            return {"status": status, "error": "unparseable response", "data": content}
    def GET(self, path, auth=None, accept=None):  # pylint: disable=C0103
        """Führt einen HTTP-GET auf den gegebenen [path] aus.
        Nutzt dabei ggf. die credentials zu [auth] und [accept]."""
        if auth and auth not in self.authdict:
            raise ValueError("Unknown auth '%s'" % auth)

        headers = {}
        if accept:
            headers['Accept'] = accept

        url = urlparse.urlunparse(('http', self.host, path, '', '', ''))

        # try request several times if it is slow to get rid of network jitter
        counter = 0
        duration = 100001
        while counter < 5 and duration >= DEFAULTFAST:
            if counter > 1:
                if DEBUG:
                    print "retry request because of %d ms duration" % duration
                else:
                    sys.stdout.write('.')
                    sys.stdout.flush()
                time.sleep(1.5)
            start = time.time()
            status, responseheaders, content = fetch(
                url, content='', method='GET',
                credentials=self.authdict.get(auth),
                headers=headers, multipart=False, ua='', timeout=30)
            duration = int((time.time() - start) * 1000)
            slowstats[url] = duration
            counter += 1
        response = Response('GET', url, status, responseheaders, content, duration)
        self.responses.append(response)
        return response
Example #16
0
    def _fetch_message(self, url):
        """Erfragt eine Nachricht von der Queue, und gibt sie als Message zurück.

        Mögliche Exceptions: FmtpHttpError, FmtpFormatError
        """
        status, headers, body = http.fetch(url, method='GET', credentials=self.credentials)
        if status != 200:
            raise FmtpHttpError('expected 200 when fetching %s, got %s' % (url, status))

        return Message(self, url, headers['content-type'], body)
Example #17
0
    def _acknowledge_message(self, url):
        """Bestätigt den Empfang einer Nachricht, und löscht sie von der Queue.

        url: die Url der Nachricht

        Mögliche Exceptions: FmtpMessageDeleted, FmtpHttpError

        Diese Funktion sollte nicht direkt aufgerufen werden.
        """
        status, headers, body = http.fetch(url, method='DELETE', credentials=self.credentials,
                                                                       headers={'Accept': 'application/json'})
        if status == 410:
            raise FmtpMessageDeleted('Message %s already deleted' % url)
        if status != 204:
            raise FmtpHttpError('expected 204 when deleting %s, got %s' % (url, status))
Example #18
0
    def POST(self, path, content, auth=None, headers={}):
        """Führt einen HTTP-GET auf den gegebenen [path] aus.
        Nutzt dabei ggf. die credentials zu [auth] und [accept]."""
        if auth and auth not in self.authdict:
            raise ValueError("Unknown auth '%s'" % auth)
        url = urlparse.urlunparse((self.protocol, self.host, path, '', '', ''))

        start = time.time()
        status, responseheaders, content = fetch(
            url, content=content, method='GET',
            credentials=self.authdict.get(auth),
            headers=headers, multipart=False, ua='resttest', timeout=30)
        duration = int((time.time() - start) * 1000)

        response = Response(self, 'GET', url, status, responseheaders, content, duration)
        self.responses.append(response)
        return response
Example #19
0
    def __call__(self, fnc, *args, **kwargs):
        """Do a remote procedure call via HTTP"""

        warnings.warn("hutools.rest is deprecated",
                      DeprecationWarning,
                      stacklevel=2)
        headers = {'content-type': 'application/json'}
        path = os.path.join(self.endpoint, build_url(fnc, *args), '')
        if 'params' in kwargs:
            path = "%s?%s" % (path, urllib.urlencode(kwargs.pop('params')))

        if kwargs:
            content = json.dumps(kwargs)
            method = 'POST'
        else:
            content = None
            method = 'GET'

        url = urlparse.urljoin(self.endpoint, path)
        credentials = '%s:%s' % (self.username, self.password)
        status, headers, content = fetch(url,
                                         method=method,
                                         content=content,
                                         headers=headers,
                                         credentials=credentials)
        if status == 201:
            return {'status': 201, 'success': 'created'}
        if status == 401:
            raise ClientUnauthorizedExecption()
        elif status == 403:
            raise ClientForbiddenException()
        elif status == 404:
            raise ClientNotFoundException()
        elif status == 500:
            raise ClientServerErrorExecption()

        try:
            return json.loads(content)
        except Exception:
            return {
                'status': status,
                'error': 'unparseable response',
                'data': content
            }
Example #20
0
    def _fetch_message_urls(self):
        """Erfragt die URLs der Nachrichten auf der Queue vom Server, und gibt sie als Liste zurück.

        Mögliche Exceptions: FmtpHttpError, FmtpFormatError
        """
        status, headers, body = http.fetch(self.queue_url, method='GET', credentials=self.credentials,
                                                                       headers={'Accept': 'application/json'})
        # Nach HTTP-Fehlern schauen.
        if status != 200:
            raise FmtpHttpError('requested %s as Messagelist, got %s' % (self.queue_url, status))

        # Antwort parsen, wenn nicht parsebar, exception
        try:
            data = hujson.loads(body)
        except:
            raise FmtpFormatError('Expeceted to get a json messagelist at %s.' % self.queue_url)

        # Urls auflisten, wenn Format nicht stimmt: exception
        try:
            return [msg['url'] for msg in data['messages']]
        except KeyError:
            raise FmtpFormatError('Expected the message at %s list to have /messages[*]/url', self.queue_url)
Example #21
0
def generate_report(reportdesign,
                    xpath,
                    xmldata,
                    url=None,
                    sign_keyname='',
                    sign_reason='',
                    callback='',
                    metadata=None):
    """Generates a report, returns the PDF.

    reportdesign, xpath and xmldata - necessary data to generate a JasperReport.
    url - points to a jasper server
    sign_keyname - key for the signature lying on the server
    sign_reason - reason to be sent within the signed document
    callback - url to where the generated report will be sent
    metadata - PDF metadata

    Return value is pdf data stripped of timestamps for modification and creaton dates.
    If a callback is given, the server will sent the pdf data to the given URL instead.
    """
    url = _find_server(url)
    content = dict(design=reportdesign, xpath=xpath, xmldata=xmldata)
    if callback:
        content['callback'] = callback
    if sign_keyname:
        content['sign_keyname'] = sign_keyname
        if not sign_reason:
            raise ValueError('reason is needed when signing documents!')
        content['sign_reason'] = sign_reason
    if metadata:
        content['metadata'] = urllib.urlencode(metadata)

    status, _headers, content = fetch(url, content, 'POST')
    if not status == 200:
        raise JasperException("%s -- %r" % (content, status))
    # remove Timestamps so the same reports hash the same.
    return re.sub("/(ModDate|CreationDate)\(D:\d{14}[+-]\d{2}'\d{2}'\)", '',
                  content)
Example #22
0
    def __call__(self, fnc, *args, **kwargs):
        """Do a remote procedure call via HTTP"""

        warnings.warn("hutools.rest is deprecated", DeprecationWarning, stacklevel=2)
        headers = {'content-type': 'application/json'}
        path = os.path.join(self.endpoint, build_url(fnc, *args), '')
        if 'params' in kwargs:
            path = "%s?%s" % (path, urllib.urlencode(kwargs.pop('params')))

        if kwargs:
            content = json.dumps(kwargs)
            method = 'POST'
        else:
            content = None
            method = 'GET'

        url = urlparse.urljoin(self.endpoint, path)
        credentials = '%s:%s' % (self.username, self.password)
        status, headers, content = fetch(url, method=method, content=content, headers=headers,
                                         credentials=credentials)
        if status == 201:
            return {'status': 201, 'success': 'created'}
        if status == 401:
            raise ClientUnauthorizedExecption()
        elif status == 403:
            raise ClientForbiddenException()
        elif status == 404:
            raise ClientNotFoundException()
        elif status == 500:
            raise ClientServerErrorExecption()

        try:
            return json.loads(content)
        except Exception:
            return {'status': status,
                    'error': 'unparseable response',
                    'data': content}
    def GET(self, path, auth=None, accept=None, headers={}):
        """Führt einen HTTP-GET auf den gegebenen [path] aus.
        Nutzt dabei ggf. die credentials zu [auth] und [accept]."""
        if auth and auth not in self.authdict:
            raise ValueError("Unknown auth '%s'" % auth)

        myheaders = {}
        if accept:
            myheaders['Accept'] = accept
        myheaders.update(headers)

        url = urlparse.urlunparse((self.protocol, self.host, path, '', '', ''))

        # try request several times if it is slow to get rid of network jitter
        counter = 0
        duration = 100001
        while counter < 5 and duration >= DEFAULTFAST:
            if counter > 1:
                if duration > 10:
                    break  # solw API pages etc we test only once
                if self.debug:
                    print "retry request because of %d ms duration" % duration
                else:
                    sys.stdout.write('.')
                    sys.stdout.flush()
                time.sleep(1.5)
            start = time.time()
            status, responseheaders, content = fetch(
                url, content='', method='GET',
                credentials=self.authdict.get(auth),
                headers=myheaders, multipart=False, ua='resttest', timeout=TIMEOUT)
            duration = int((time.time() - start) * 1000)
            self.slowstats[url] = duration
            counter += 1
        response = Response(self, 'GET', url, status, responseheaders, content, duration)
        self.responses.append(response)
        return response
Example #24
0
 def test_fetch_post_basic(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2',
     {'keyp1': 'value', 'just a test': 212}, 'POST')
     assert status == 201
Example #25
0
 def test_fetch_post_basic(self):
     status, header, body = fetch('http://www.postbin.org/186ndf2', {
         'keyp1': 'value',
         'just a test': 212
     }, 'POST')
     assert status == 201