Пример #1
0
 def test_parsing_clears_cache(self):
     request = make_json_request({
         'string': 'value',
         'integer': [1, 2]
     })
     string_result = parser.parse_json(request, 'string', Arg(str))
     assert string_result == 'value'
     assert 'json' in parser._cache
     assert 'string' in parser._cache['json']
     assert 'integer' in parser._cache['json']
     attrs = {'string': Arg(str), 'integer': Arg(int, multiple=True)}
     parser.parse(attrs, request)
     assert parser._cache == {}
Пример #2
0
    async def get(self):
        args = parser.parse(DirectionsQuerySchema, self.request,
                            locations=('query',))

        error_msg = 'Google Maps API error response: %s'
        try:
            routes = await self.googlemaps.directions(**args)
        except googlemaps.exceptions.ApiError as e:
            self.send_error(
                ApiErrorCode[e.status].value,
                message=error_msg % (e.message if e.message else e.status)
            )
            return
        except googlemaps.exceptions.HTTPError as e:
            self.send_error(e.status_code,
                            message=error_msg % 'HTTP error')
            return
        except googlemaps.exceptions.Timeout as e:
            self.send_error(599, message=error_msg % 'timeout')
            return
        except googlemaps.exceptions.TransportError as e:
            self.send_error(500, message=error_msg % 'transport error')
            return

        result = [{'id': i, 'route': r} for i, r in enumerate(routes)]
        schema = DirectionsSchema(many=True)
        output = schema.dumps(result)
        self.finish(output.data)
Пример #3
0
    def test_it_should_parse_files_arguments(self):
        attrs = {
            'string': Arg(str),
            'integer': Arg(int, multiple=True)
        }

        request = make_files_request([
            ('string', 'value'),('integer', '1'), ('integer', '2')
        ])

        parsed = parser.parse(attrs, request, targets=['files'])

        assert parsed['string'] == value
        assert parsed['integer'] == [1, 2]
Пример #4
0
    def test_it_should_parse_form_arguments(self):
        attrs = {
            'string': Arg(),
            'integer': Arg(int, multiple=True)
        }

        request = make_form_request([
            ('string', 'value'),('integer', '1'), ('integer', '2')
        ])

        parsed = parser.parse(attrs, request)

        assert parsed['integer'] == [1, 2]
        assert parsed['string'] == bvalue
Пример #5
0
    def test_it_should_parse_header_arguments(self):
        attrs = {
            'string': Arg(str),
            'integer': Arg(int, multiple=True)
        }

        request = make_request(headers={
            'string': 'value',
            'integer': ['1', '2']
        })

        parsed = parser.parse(attrs, request, targets=['headers'])

        assert parsed['string'] == value
        assert parsed['integer'] == [1, 2]
Пример #6
0
    def test_it_should_parse_json_arguments(self):
        attrs = {
            'string': Arg(str),
            'integer': Arg(int, multiple=True)
        }

        request = make_json_request({
            'string': 'value',
            'integer': [1, 2]
        })

        parsed = parser.parse(attrs, request)

        assert parsed['integer'] == [1, 2]
        assert parsed['string'] == value
Пример #7
0
    def parse_request(self, args):
        """HTTP Request Parser

        Parse request parameters from any HTTP methods
        """
        return parser.parse(args, self.request)
Пример #8
0
 def test_it_should_fail_with_bad_request_on_error(self):
     with pytest.raises(tornado.web.HTTPError) as error:
         parser.parse(None, make_request())
Пример #9
0
 def test_it_should_raise_httperror_on_failed_validation(self):
     args = {'foo': Arg(validate=lambda x: False)}
     with pytest.raises(tornado.web.HTTPError):
         parser.parse(args, make_json_request({'foo': 42}))