Пример #1
0
 def test_make_location_header(self):
     from restart.testing import RequestFactory
     factory = RequestFactory()
     request = factory.post('/tests', data={})
     assert 'http://localhost/tests/1' == make_location_header(request, 1)
     assert ('http://localhost/tests/id' == make_location_header(
         request, 'id'))
Пример #2
0
 def test_make_location_header(self):
     from restart.testing import RequestFactory
     factory = RequestFactory()
     request = factory.post('/tests', data={})
     assert 'http://localhost/tests/1' == make_location_header(request, 1)
     assert ('http://localhost/tests/id' ==
             make_location_header(request, 'id'))
Пример #3
0
class TestParsers(object):

    factory = RequestFactory(keep_initial_request=True)

    def parse(self, parser, data, content_type=None):
        request = self.factory.get('/', data=data, content_type=content_type)
        return parser.parse(request.stream, request.content_type,
                            request.content_length)

    def test_parser(self):
        parser = Parser()

        with pytest.raises(NotImplementedError):
            data = {'hello': 'world'}
            self.parse(parser, data)

    def test_json_parser(self):
        parser = JSONParser()
        data = '{"hello": "world"}'
        parsed = self.parse(parser, data, 'application/json')

        assert parsed == {'hello': 'world'}

    def test_json_parser_with_invalid_data(self):
        parser = JSONParser()
        data = {'hello': 'world'}

        with pytest.raises(BadRequest):
            self.parse(parser, data, 'application/json')

    def test_multi_part_parser(self):
        parser = MultiPartParser()
        data = {
            'text': 'this is some text',
            'file': (BytesIO(b'this is the file'), 'test.txt')
        }
        parsed_data, parsed_files = self.parse(parser, data)

        assert parsed_data['text'] == 'this is some text'
        assert isinstance(parsed_files['file'], FileStorage)
        assert parsed_files['file'].filename == 'test.txt'
        assert parsed_files['file'].stream.read() == b'this is the file'

    def test_urlencoded_parser(self):
        parser = URLEncodedParser()
        data = {'hello': 'world'}
        parsed = self.parse(parser, data)

        assert parsed == {'hello': 'world'}
Пример #4
0
class TestRequestFactory(object):

    initial_factory = RequestFactory(keep_initial_request=True)
    factory = RequestFactory()

    def assert_request(self,
                       request,
                       request_class,
                       method,
                       path,
                       data,
                       args=None):
        args = args or {}

        assert isinstance(request, request_class)
        assert request.method == method
        assert request.path == path
        assert request.stream.read() == data
        assert request.args == args

    def test_get_cases(self):
        request = self.initial_factory.get('/cases')
        self.assert_request(request, WerkzeugSpecificRequest, 'GET', '/cases',
                            b'')

        request = self.factory.get('/cases')
        self.assert_request(request, WerkzeugRequest, 'GET', '/cases', b'')

    def test_post_cases(self):
        request = self.initial_factory.post('/cases', data='{"name": "case"}')
        self.assert_request(request, WerkzeugSpecificRequest, 'POST', '/cases',
                            b'{"name": "case"}')

        request = self.factory.post('/cases', data='{"name": "case_2"}')
        self.assert_request(request, WerkzeugRequest, 'POST', '/cases',
                            b'{"name": "case_2"}')

    def test_get_case(self):
        request = self.initial_factory.get('/cases/1')
        self.assert_request(request, WerkzeugSpecificRequest, 'GET',
                            '/cases/1', b'')

        request = self.factory.get('/cases/1')
        self.assert_request(request, WerkzeugRequest, 'GET', '/cases/1', b'')

    def test_put_case(self):
        request = self.initial_factory.put('/cases/1')
        self.assert_request(request, WerkzeugSpecificRequest, 'PUT',
                            '/cases/1', b'')

        request = self.factory.put('/cases/1')
        self.assert_request(request, WerkzeugRequest, 'PUT', '/cases/1', b'')

    def test_patch_case(self):
        request = self.initial_factory.patch('/cases/1')
        self.assert_request(request, WerkzeugSpecificRequest, 'PATCH',
                            '/cases/1', b'')

        request = self.factory.patch('/cases/1')
        self.assert_request(request, WerkzeugRequest, 'PATCH', '/cases/1', b'')

    def test_delete_case(self):
        request = self.initial_factory.delete('/cases/1')
        self.assert_request(request, WerkzeugSpecificRequest, 'DELETE',
                            '/cases/1', b'')

        request = self.factory.delete('/cases/1')
        self.assert_request(request, WerkzeugRequest, 'DELETE', '/cases/1',
                            b'')
Пример #5
0
from __future__ import absolute_import

import pytest
from werkzeug.wrappers import Response as WerkzeugSpecificResponse

from restart.api import RESTArt
from restart.resource import Resource
from restart.response import Response
from restart.adapter import Adapter, WerkzeugAdapter
from restart.testing import RequestFactory

factory = RequestFactory()

api = RESTArt()


@api.route(methods=['GET'])
class Demo(Resource):
    name = 'demo'

    def read(self, request):
        return "I'm a demo"


def dummy_handler(*args, **kwargs):
    return Response('dummy')


class TestAdapter(object):
    def test_adapt_rules(self):
        adapter = Adapter(api)
Пример #6
0
from __future__ import absolute_import

import pytest

from restart.request import Request, WerkzeugRequest
from restart.parsers import JSONParser
from restart.negotiator import Negotiator
from restart.testing import RequestFactory

factory = RequestFactory(keep_initial_request=True)


def assert_environ(environ):
    keys = (
        'CONTENT_LENGTH',
        'CONTENT_TYPE',
        'HTTP_HOST',
        'PATH_INFO',
        'QUERY_STRING',
        'REQUEST_METHOD',
        'SCRIPT_NAME',
        'SERVER_NAME',
        'SERVER_PORT',
        'SERVER_PROTOCOL',
    )
    for key in keys:
        assert key in environ


class TestRequest(object):
    def test_normal_request(self):