Exemple #1
0
 def test_get_url_params_no_request_id(self):
     req = FakeRequest(args={'foo': ['hello', 'bye'], 'bar': ['world']})
     assert Service.get_url_params(req, ['foo'], ['bar', 'baz']) == {
         'foo': 'hello',
         'bar': 'world',
     }
     assert Service.get_request_id(req) is None
Exemple #2
0
 def test_format_response_with_request_id(self):
     req = FakeRequest()
     Service.set_request_id(req, 'req0')
     response = Service.format_response({'foo': 'bar'}, req)
     assert req.headers == {'content-type': ['application/json']}
     assert json.loads(response) == {
         'request_id': 'req0',
         'foo': 'bar',
     }
Exemple #3
0
 def test_format_error_with_status_code(self):
     req = FakeRequest()
     Service.set_request_id(req, 'req0')
     response = Service.format_error(APIError('teapot', 418), req)
     assert req.code == 418
     assert req.headers == {'content-type': ['application/json']}
     assert json.loads(response) == {
         'request_id': 'req0',
         'error': 'teapot',
     }
Exemple #4
0
 def test_format_error_with_request_id(self):
     req = FakeRequest()
     Service.set_request_id(req, 'req0')
     response = Service.format_error(APIError('bad thing'), req)
     assert req.code == 500
     assert req.headers == {'content-type': ['application/json']}
     assert json.loads(response) == {
         'request_id': 'req0',
         'error': 'bad thing',
     }
Exemple #5
0
 def test_get_url_params_with_request_id(self):
     req = FakeRequest(args={
         'request_id': ['req0'],
         'foo': ['hello', 'bye'],
         'bar': ['world'],
     })
     assert Service.get_url_params(req, ['request_id', 'foo', 'bar']) == {
         'request_id': 'req0',
         'foo': 'hello',
         'bar': 'world',
     }
     assert Service.get_request_id(req) == 'req0'
Exemple #6
0
 def test_get_params_optional(self):
     params = {
         'foo': 'hello',
         'bar': 'world',
     }
     assert Service.get_params(params, ['foo'], ['bar']) == {
         'foo': 'hello',
         'bar': 'world',
     }
     assert Service.get_params(params, ['foo'], ['bar', 'baz']) == {
         'foo': 'hello',
         'bar': 'world',
     }
Exemple #7
0
 def test_get_params_mandatory(self):
     params = {
         'foo': 'hello',
         'bar': 'world',
     }
     assert Service.get_params(params, ['foo', 'bar']) == {
         'foo': 'hello',
         'bar': 'world',
     }
Exemple #8
0
 def test_request_id(self):
     req = FakeRequest()
     assert Service.get_request_id(req) is None
     Service.set_request_id(req, 'foo')
     assert Service.get_request_id(req) == 'foo'
Exemple #9
0
 def hello(slf, request, request_id):
     Service.set_request_id(request, request_id)
     params = Service.get_json_params(request, ['who'])
     return {'hello': params['who']}
Exemple #10
0
 def hello(slf, request):
     params = Service.get_url_params(request, ['request_id', 'who'])
     return {'hello': params['who']}
Exemple #11
0
 def test_get_json_params(self):
     req = FakeRequest(content=json.dumps({'foo': 'hello', 'bar': 'world'}))
     assert Service.get_json_params(req, ['foo'], ['bar', 'baz']) == {
         'foo': 'hello',
         'bar': 'world',
     }