def test_multiuri_handler_query_arg(self): def handler(user): return dict(name=user) sp = ServicePublisher() sp.add_endpoint( Endpoint(name='', method='GET', uris=[ '/(?P<user>.*)/profile', '/profiles/(?P<user>.*)', ], args=([args.String('user')], {}), function=handler)) req = create_req('GET', '/oneuser/profile') resp = MockResponse(req, 200) result = sp(req, resp.start_response) resp.write(result) self.assertEqual(req._buffer, response_buf(200, '{"name": "oneuser"}')) req = create_req('GET', '/profiles/other_user') resp = MockResponse(req, 200) result = sp(req, resp.start_response) resp.write(result) self.assertEqual(req._buffer, response_buf(200, '{"name": "other_user"}'))
def test_arg_handlersuccess_nested_json(self): def handler(*args, **kwargs): return dict(arg1=1) sp = ServicePublisher() sp.add_endpoint( Endpoint(name='', method='POST', uri='/location', args=([args.String('test')], {}), function=handler)) req = create_req('POST', '/location', arguments=dict(test="blah"), headers={"Content-Type": "application/json"}, body='{"test":"foo", "spam":{"wonderful":true}}') resp = MockResponse(req, 200) result = sp(req, resp.start_response) resp.write(result) self.assertEqual(req._buffer, response_buf(200, '{"arg1": 1}'))
def test_arg_handlersuccess_part_tre(self): def handler(*args, **kwargs): return dict(arg1=1) sp = ServicePublisher() sp.add_endpoint( Endpoint(name='', method='POST', uri='/location', args=([args.String('test')], {}), function=handler)) req = create_req('POST', '/location', arguments=dict(test="blah"), headers={"Content-Type": "application/json"}, body='{"test"="foo"}') resp = MockResponse(req, 500) result = sp(req, resp.start_response) resp.write(result) self.assertEqual( req._buffer, response_buf(400, '{"message": "body is not JSON", "code": 400}'))
def test_string_optional_true(self): req = create_req({"headers":{"test":"something"}, "arguments":{},"body":'{"test":1}'}) i = args.String("test1", optional=True) self.assertEqual(None, i.argspec(req, None))
def test_string_optional_false(self): req = create_req({"headers":{"test":"something"}, "arguments":{},"body":'{"test":1}'}) i = args.String("test1", optional=False) i.argspec(req, None)
def test_string_in_inargs_but_none(self): req = create_req({"headers":{"test":"something"}, "arguments":{},"body":'{}'}) i = args.String("test") self.assertEqual(1, i.argspec(req, {"test":None}))
def test_amp_in_arg_value(self): req = create_json_post_req({"QUERY_STRING":"test=some%26thing"}) i = args.String("test", optional=True) v = i.argspec(req, None) self.assertEqual('some&thing', v, '"some&thing" != "%s"' % v)
def test_amp_in_arg_key(self): req = create_json_post_req({"QUERY_STRING":"test%26123=some"}) i = args.String("test&123", optional=True) v = i.argspec(req, None) self.assertEqual('some', v, '"some" != "%s"' % v)
def test_string_in_args(self): req = create_json_post_req({"QUERY_STRING":"test=1"}) i = args.String("test") self.assertEqual("1", i.argspec(req, None))
def test_string_in_body(self): req = create_json_post_req({"headers":{"test":"something"}, "arguments":{},"body":'{"test":1}'}) i = args.String("test") self.assertEqual(1, i.argspec(req, None))
renderer=HTML(), ), Endpoint(name='Post Hello', method='POST', uri='/hello/?$', function=hws.say_hello, args=Args( args.JsonBodyField('name'), ), ), Endpoint(name='Put Hello', method='PUT', uri='/hello/(?P<name>[^/]+)?$', function=hws.say_hello, args=Args( args.String('name'), ), ), Endpoint(name='Get Hello', method='GET', uri='/hello/?$', function=hws.say_hello, args=Args( args.String('name'), args.Integer('number', optional=True), ), ), Endpoint(name='I just break', method='GET', uri='/break/?$', function=hws.just_throw_an_exception,
# # Ideally you should separate this code from your pure python service code to # increase portability. # # Since the service_description is just a list, you can combine many # descriptions if you wish to run them in the same process. This will allow # for quick reorganizing when trying to scale. # ps = PageService() service_description = [ Endpoint(name='Get a Page', method='GET', uri='/(?P<name>.+)?$', function=ps.get_page, args=Args( name=args.String('name', optional=True), ), # Our default exception handler will take care of any exceptions # this endpoint might throw. exceptions={}, renderer=PageRenderer(), ), Endpoint(name='Save a Page', method='POST', uri='/(?P<name>.+)?$', function=ps.save_page, args=Args( admin_user=GAEAdminUser('admin_user', ps.ADMIN_USERS), name=args.String('name'), title=args.String('title'), content=args.String('content'),