def __init__(self, app, resource_class, protocol=Resource.Protocol.http): def mix( r ): # mix the resource class with a DummyResource mixin to fake the HTTP library return type(r.__name__, (DummyResource, r), {}) self._app = app self.protocol = protocol self._router = Router(name='api') self._router.register(mix(resource_class), resource_class.__name__)
class ResourceTestClient(object): ''' A test client used to perform basic http requests in a test environment on a Resource. Initialize with an app-like object and the Resource class being tested ''' def __init__(self, app, resource_class): self._app = app self._router = Router(name='api') self._router.register(resource_class, resource_class.__name__) def __del__(self): for endpoint in self._router.endpoints(): self._router.unregister(endpoint) def parse_response_data(self, response): return json.loads(response.data) async def process_request(self, method, url, headers, args, body): handler = None # match the given url to urls in the router then activate the relevant resource for route in self._router.urls_regex(): match = re.match(route.path, url) if match: handler = route.handler args.update(match.groupdict()) break if handler: # create dummy request object request = Request(app=self._app, method=method, url=url, args=args, headers=headers, body=body) response = await handler(request) return response return Response(headers={}, data=None, status=404) async def get(self, url, headers={}, args={}, body={}): return await self.process_request('GET', url, headers, args, body) async def post(self, url, headers={}, args={}, body={}): return await self.process_request('POST', url, headers, args, body) async def put(self, url, headers={}, args={}, body={}): return await self.process_request('PUT', url, headers, args, body) async def delete(self, url, headers={}, args={}, body={}): return await self.process_request('DELETE', url, headers, args, body) async def patch(self, url, headers={}, args={}, body={}): return await self.process_request('PATCH', url, headers, args, body) async def options(self, url, headers={}, args={}, body={}): return await self.process_request('OPTIONS', url, headers, args, body) async def head(self, url, headers={}, args={}, body={}): return await self.process_request('HEAD', url, headers, args, body)
#!/usr/bin/env python # encoding: utf-8 from tbone.resources.routers import Router from resources import * # create a resource router for this app _blog_router = Router(name='api/blog') _blog_router.register(EntryResource, 'entry') routes = [] routes += _blog_router.urls()
#!/usr/bin/env python # encoding: utf-8 from tbone.resources.routers import Router from resources import * # create a resource router for this app chatrooms_router = Router(name='api/team') chatrooms_router.register(UserResource, 'user') chatrooms_router.register(RoomResource, 'channel') chatrooms_router.register(RoomResource, 'entry')
class ResourceTestClient(object): ''' A test client used to perform basic http requests in a test environment on a Resource. Initialize with an app-like object and the Resource class being tested. Defaults to HTTP communication. ''' def __init__(self, app, resource_class, protocol=Resource.Protocol.http): def mix( r ): # mix the resource class with a DummyResource mixin to fake the HTTP library return type(r.__name__, (DummyResource, r), {}) self._app = app self.protocol = protocol self._router = Router(name='api') self._router.register(mix(resource_class), resource_class.__name__) def __del__(self): for endpoint in self._router.endpoints(): self._router.unregister(endpoint) def parse_response_data(self, response): if isinstance(response.payload, dict): return response.payload return json.loads(response.payload) async def process_request(self, method, url, headers, args, body): handler = None # match the given url to urls in the router then activate the relevant resource for route in self._router.urls_regex(self.protocol): match = re.match(route.path, url) if match: handler = route.handler args.update(match.groupdict()) break if handler: # create dummy request object request = Request(app=self._app, method=method, url=url, args=args, headers=headers, body=body) response = await handler(request) # if the protocol is websockets we convert the HTTP response object so the tests don't have to be written twice if self.protocol == Resource.Protocol.websocket: response = json.loads(response) return Response(headers={}, payload=response['payload'], status=response['status']) return response return Response(headers={}, data=None, status=404) async def get(self, url, headers={}, args={}, body={}): return await self.process_request('GET', url, headers, args, body) async def post(self, url, headers={}, args={}, body={}): return await self.process_request('POST', url, headers, args, body) async def put(self, url, headers={}, args={}, body={}): return await self.process_request('PUT', url, headers, args, body) async def delete(self, url, headers={}, args={}, body={}): return await self.process_request('DELETE', url, headers, args, body) async def patch(self, url, headers={}, args={}, body={}): return await self.process_request('PATCH', url, headers, args, body) async def options(self, url, headers={}, args={}, body={}): return await self.process_request('OPTIONS', url, headers, args, body) async def head(self, url, headers={}, args={}, body={}): return await self.process_request('HEAD', url, headers, args, body)
def __init__(self, app, resource_class): self._app = app self._router = Router(name='api') self._router.register(resource_class, resource_class.__name__)
#!/usr/bin/env python # encoding: utf-8 from tbone.resources.routers import Router from resources import * # create a resource router for this app _movies_router = Router(name='api/movies') _movies_router.register(MovieResource, 'movie') routes = [] routes += _movies_router.urls()
#!/usr/bin/env python # encoding: utf-8 from tbone.resources.routers import Router from resources import * # create a resource router for this app _chatrooms_router = Router(name='api/chatrooms') _chatrooms_router.register(UserResource, 'user') _chatrooms_router.register(RoomResource, 'room') routes = [] routes += _chatrooms_router.urls()