Esempio n. 1
0
 def test_content(self):
     response = Response(status=200, message='OK',
             headers={'Content-Type':'text/plain'})
     self.assertTrue(response.content_is_text)
     
     response.content = 'foo'
     self.assertTrue(response.content, 'foo')
Esempio n. 2
0
    def test_content(self):
        response = Response(status=200,
                            message='OK',
                            headers={'Content-Type': 'text/plain'})
        self.assertTrue(response.content_is_text)

        response.content = 'foo'
        self.assertTrue(response.content, 'foo')
Esempio n. 3
0
    def test_base(self):
        response = Response(status=200, message='OK')
        self.assertTrue(response)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.message, 'OK')
        self.assertEqual(response.status_line, '200 OK')

        response = Response(status='200', message='OK')
        self.assertEqual(response.status, 200)
Esempio n. 4
0
def generateResponse(status, entity):
    http_response = Response()
    http_response.set_status(status)
    http_response.set_entity(entity)
    entityHeaderDict['Content_Length'] = str(len(entity))
    http_response.generalHeader.set_value({'Datetime': datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
    http_response.responseHeader.set_value(responseHeaderDict)
    http_response.entityHeader.set_value(entityHeaderDict)
    return http_response
Esempio n. 5
0
    def _finalize_response(self, response):
        """ Convert the ``Response`` object into diablo ``Response``

        :return: diablo's ``Response``
        """
        headers = {'Content-Type': self._get_content_type(),
                   'Content-Length': len(response.content)}

        res = Response(content=response.content, headers=headers)
        # status_code is set separately to allow zero
        res.code = response.code
        return res
Esempio n. 6
0
 def test_date(self):
     response = Response(
         status=200,
         message='OK',
         headers={'Last-Modified': 'Sat, 02 Jul 2011 07:53:00 GMT'})
     self.assertTrue(response.last_modified)
     self.assertIsInstance(response.last_modified, datetime)
Esempio n. 7
0
 def test_done(self):
     response = ResponseAsync()
     self.assertFalse(response.done(), 'response does not start done')
     std_response = Response(200)
     response.fulfill(std_response)
     self.assertTrue(response.done(),
                     'response is done after call to fulfill')
Esempio n. 8
0
 def request(url, session, retry_count):
     """
 Post get request to given url with exponential backoff retry scheme
 returns response object on success, else None
 """
     print 'Requesting', url
     response = session.get(url)
     if Response(response.status_code).is_success:
         return response
     else:
         print 'HTTP {0} for {1}'.format(str(response.status_code), url)
         if retry_count < JinstaScrape.MAX_RETRIES:
             # Determine suitable timeout
             timeout = 2**(6 + retry_count
                           )  # use exponential backoff (start at 64s)
             if 'Retry-After' in response.headers:
                 timeout = int(response.headers['Retry-After'])
             print 'Retrying in {0}s'.format(timeout)
             # Timeout
             time.sleep(timeout)
             # Retry
             return JinstaScrape.request(url, session, retry_count + 1)
         else:
             print 'Max retries exceeded. Aborting current query.'
             return None
Esempio n. 9
0
    def _make_request(self, request):

        try:
            request = self._handlers.dispatch('request_prepare', request)
        except Exception, e:
            response = Response(status=400, message='Bad request',
                request=request)
            return response
Esempio n. 10
0
    def test_callback(self):
        def callback(response):
            response.called = True
            self.assertEqual(response.status, 200)

        response = ResponseAsync(callback=callback)
        response.fulfill(Response(200))
        self.assertEqual(response.status, 200)
        self.assertTrue(response.called)
Esempio n. 11
0
    def _prepare_response(self, response):
        """ Coerce response to drest's Response

        @param response either the response data or a `Response` object.
        @return Response object
        """

        if not isinstance(response, Response):
            return Response(0, response)
        return response
Esempio n. 12
0
    def test_base_url(self):
        response = Response(status=200,
                            message='OK',
                            headers={'Content-Base': 'http://foo'})
        self.assertEqual(response.base, 'http://foo/')
        self.assertIsInstance(response.base, Url)

        response = Response(status=200,
                            message='OK',
                            headers={'Content-Location': 'http://bar'})
        self.assertEqual(response.base, 'http://bar/')
        self.assertIsInstance(response.base, Url)

        request = Request('GET', 'http://baz')
        response = Response(status=200, message='OK', request=request)
        self.assertEqual(response.base, 'http://baz/')
        self.assertIsInstance(response.base, Url)

        response = Response(status=200)
        self.assertEqual(response.base, None)
Esempio n. 13
0
    def _prepare_response(self, response):
        """ Coerce response to devil's Response

        :param response: either the response data or a ``Response`` object.
        :returns: ``Response`` object

        todo: we don't need this anymore as resource already does the coercion
        """

        if not isinstance(response, Response):
            return Response(0, response)
        return response
Esempio n. 14
0
 def test_set_and_flag(self):
     response = ResponseAsync()
     self.assertFalse(response.done(), 'response does not start done')
     delay = 0.1
     WaitAndSet(response, Response(200), delay).start()
     start = datetime.now()
     self.assertFalse(response.done(), 'response still not done')
     self.assertEqual(response.status, 200, 'expected status code')
     duration = datetime.now() - start
     min = timedelta(seconds=delay - 0.1)
     max = timedelta(seconds=delay + 0.1)
     self.assertTrue(min < duration and duration < max,
                     'took around 1s to get response and content')
     self.assertTrue(response.done(), 'response now done')
Esempio n. 15
0
def _test_cb(request):
    if isinstance(request, Request) is False:
        print "not a Request object"
        raise Exception

    if request.method is False:
        print "no HTTP method"
        raise Exception

    method = request.method

    if str(request.url) != tests[method]['url']:
        print "%s is different from %s" % (request.url, tests[method]['url'])
        raise Exception

    if method in ['PUT', 'POST']:
        if request.content != tests[method]['content']:
            print "%s is different from %s" % (request.content,
                                               tests[method]['content'])
            raise Exception

    return Response(status=204)
Esempio n. 16
0
    def _build_response(self, r, request):
        status = r.status
        headers = Headers(r.headers)
        content = r.data

        resp = Response(
            status=status,
            headers=headers,
            content=content,
            message=r.reason,
            request=request
        )

        new_resp = self._handlers.dispatch('response_done', resp)
        if new_resp is not None:
            resp = new_resp

        req = self._handlers.dispatch('response_redirect', resp)

        if req is not None and isinstance(req, Request):
            return self.request(req)

        return resp
Esempio n. 17
0
def _cb(request):
    response = Response(200, content=json.dumps({'name': 'batman'}))
    return response
Esempio n. 18
0
def _exception_cb(request):
    return Response(status=404, message='Not Found')
Esempio n. 19
0
def _cb(request):
    if request.method == 'GET':
        res = Response(200, content='everything is ok !')
    elif request.method == 'HEAD':
        res = Response(200, headers=[('Content-Length', 10)])
    return res
Esempio n. 20
0
def _cb_request_send(request):
    return Response(status=204)
Esempio n. 21
0
 def test_request(self):
     request = Request('GET', 'http://foobar')
     response = Response(status=200,
                         message='OK',
                         headers={},
                         request=request)
Esempio n. 22
0
import mimetypes
from json import dumps
from urlparse import urljoin
from traceback import format_exc
from types import StringType

from config import HTTP_CODES, DEBUG
from http import Request, Response
from exception import HTTPError, HTTPResponse
from router import Router
from util import depr, yieldroutes, toa, parse_date, _reloader_child, _reloader_observer
from util import WSGIFileWrapper
from server import ServerAdapter, WSGIRefServer

request = Request()
response = Response()


class Brick(object):
    def __init__(self, catchall=True, autojson=True, config=None):
        self.routes = Router()
        self._logger = None
        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and dumps:
            self.add_filter(dict, dumps)
Esempio n. 23
0
def _cb_response_done(response):
    return Response(status=202)
Esempio n. 24
0
def handleResponse(raw_response):
    http_response = Response()
    http_response.unpack(raw_response)
    return http_response
Esempio n. 25
0
 def coerce_response():
     """ Coerce the response object into devil structure. """
     if not isinstance(response, Response):
         return Response(0, response)
     return response
Esempio n. 26
0
        def _wsgi(environ, start_response):
            """A application for WSGI server."""
            global g
            g.request = Request(environ)
            g.response = Response()
            path_info = g.request.path_info

            # deal with normal page
            try:
                return self._handle_response(path_info, start_response)
            # deal with redirection
            except RedirectError as e:
                if e.location == 'login_url':
                    if hasattr(self, '_login_url'):
                        url = self._login_url
                    else:
                        logging.warning('-- You should set `login_url` in \
                                settings dict.')
                        raise HttpError(404)
                else:
                    url = e.location
                g.response.status = e.status
                #g.response.location = 'http://' + g.request.host + url
                return self._handle_response(url, start_response)
            # deal with bad request
            except HttpError as e:
                if hasattr(self, '_not_found_page'):
                    url = self._not_found_page
                    g.response.status = e.status
                    return self._handle_response(url, start_response)
                else:
                    # add all urls later.
                    data = """<html>
                    <head>
                        <title>404</title>
                        <style type="text/css">
                            body {
                                margin: 0 auto;
                                width: 50%;
                                font-family: "Hiragino Sans GB",
                                "Microsoft YaHei","WenQuanYi Micro Hei",
                                sans-serif;
                            }
                            .bd {
                                margin: 10% auto;
                                border: 1px solid #696969;
                                background: #696969;
                                border-radius: 5px;
                            }
                            p {
                                color: #fff;
                                font-size: 43px;
                                font-weight: bold;
                                text-align: center;
                                text-shadow: 0 0 5px #fefcc9, 3px -3px 5px
                                #feec85, -5px -5px 10px #ffae34, 5px -10px
                                13px #ec760c, -5px -15px 15px #cd4606, 0
                                -20px 18px #973716, 3px -23px 20px #451b0e;
                            }
                        </style>
                    </head>
                    <body>
                        <div class="bd">
                            <p>Oh, God! The page Not Found!</p>
                        </div>
                    </body>
                    </html>
                    """
                start_response(e.status, g.response.headers)
                return [data]
            finally:
                del g.request
                del g.response