Ejemplo n.º 1
0
 async def test_bad_route(self):
     handler = AIOHTTPGridFS(self.db)
     await self.start_app(extra_routes={'/x/{wrongname}': handler})
     response = await self.get('/x/foo')
     self.assertEqual(500, response.status)
     msg = 'Bad AIOHTTPGridFS route "/x/{wrongname}"'
     self.assertIn(msg, (await response.text()))
Ejemplo n.º 2
0
 def test_post(self):
     # Only allow GET and HEAD, even if a POST route is added.
     yield from self.start_app()
     resource = self.app.router.add_resource('/fs/{filename}')
     handler = AIOHTTPGridFS(self.db)
     resource.add_route('POST', handler)
     result = yield from self.request('post', '/fs/foo')
     self.assertEqual(405, result.status)
Ejemplo n.º 3
0
 def test_bad_route(self):
     yield from self.start_app()
     resource = self.app.router.add_resource('/x/{wrongname}')
     handler = AIOHTTPGridFS(self.db)
     resource.add_route('GET', handler)
     response = yield from self.get('/x/foo')
     self.assertEqual(500, response.status)
     msg = 'Bad AIOHTTPGridFS route "/x/{wrongname}"'
     self.assertIn(msg, (yield from response.text()))
Ejemplo n.º 4
0
    async def test_tz_aware(self):
        client = self.asyncio_client(tz_aware=True)
        await self.start_app(AIOHTTPGridFS(client.motor_test))
        now = datetime.datetime.utcnow()
        ago = now - datetime.timedelta(minutes=10)
        hence = now + datetime.timedelta(minutes=10)

        response = await self.get('/fs/foo', if_modified_since=ago)
        self.assertEqual(200, response.status)

        response = await self.get('/fs/foo', if_modified_since=hence)
        self.assertEqual(304, response.status)
Ejemplo n.º 5
0
    def start_app(self, http_gridfs=None):
        self.app = aiohttp.web.Application()
        resource = self.app.router.add_resource('/fs/{filename}')
        handler = http_gridfs or AIOHTTPGridFS(self.db)
        resource.add_route('GET', handler)
        resource.add_route('HEAD', handler)
        self.app_handler = self.app.make_handler()
        server = self.loop.create_server(self.app_handler,
                                         host='localhost',
                                         port=8088)

        self.srv, _ = yield from asyncio.gather(server,
                                                self.app.startup(),
                                                loop=self.loop)
Ejemplo n.º 6
0
    def test_tz_aware(self):
        client = AsyncIOMotorClient(test.env.uri,
                                    tz_aware=True,
                                    io_loop=self.loop)

        yield from self.start_app(AIOHTTPGridFS(client.motor_test))
        now = datetime.datetime.utcnow()
        ago = now - datetime.timedelta(minutes=10)
        hence = now + datetime.timedelta(minutes=10)

        response = yield from self.get('/fs/foo', if_modified_since=ago)
        self.assertEqual(200, response.status)

        response = yield from self.get('/fs/foo', if_modified_since=hence)
        self.assertEqual(304, response.status)
Ejemplo n.º 7
0
    async def start_app(self, http_gridfs=None, extra_routes=None):
        self.app = aiohttp.web.Application()
        resource = self.app.router.add_resource('/fs/{filename}')
        handler = http_gridfs or AIOHTTPGridFS(self.db)
        resource.add_route('GET', handler)
        resource.add_route('HEAD', handler)

        if extra_routes:
            for route, handler in extra_routes.items():
                resource = self.app.router.add_resource(route)
                resource.add_route('GET', handler)

        self.app_handler = self.app.make_handler()
        server = self.loop.create_server(self.app_handler,
                                         host='localhost',
                                         port=8088)

        self.srv, _ = await asyncio.gather(server, self.app.startup())
Ejemplo n.º 8
0
    def test_get_gridfs_file(self):
        def getter(bucket, filename, request):
            # Test overriding the get_gridfs_file() method, path is
            # interpreted as file_id instead of filename.
            return bucket.open_download_stream(file_id=filename)

        def cache_time(path, modified, mime_type):
            return 10

        def extras(response, gridout):
            response.headers['quux'] = 'fizzledy'

        yield from self.start_app(
            AIOHTTPGridFS(self.db,
                          get_gridfs_file=getter,
                          get_cache_time=cache_time,
                          set_extra_headers=extras))

        # We overrode get_gridfs_file so we expect getting by filename *not* to
        # work now; we'll get a 404. We have to get by file_id now.
        response = yield from self.get('/fs/foo')
        self.assertEqual(404, response.status)

        response = yield from self.get('/fs/' + str(self.file_id))
        self.assertEqual(200, response.status)

        self.assertEqual(self.contents, (yield from response.read()))
        cache_control = response.headers['Cache-Control']
        self.assertRegex(cache_control, r'max-age=\d+')
        self.assertEqual(10, int(cache_control.split('=')[1]))
        expiration = parse_date(response.headers['Expires'])
        now = datetime.datetime.utcnow()

        # It should expire about 10 seconds from now
        self.assertTrue(
            datetime.timedelta(seconds=8) < expiration -
            now < datetime.timedelta(seconds=12))

        self.assertEqual('fizzledy', response.headers['quux'])
Ejemplo n.º 9
0
        gfs = AsyncIOMotorGridFSBucket(client.my_database)
        tmp.seek(0)
        await gfs.upload_from_stream(filename="my_file",
                                     source=tmp,
                                     metadata={
                                         "contentType": "text",
                                         "compressed": True
                                     })


asyncio.get_event_loop().run_until_complete(put_gridfile())


# Add "Content-Encoding: gzip" header for compressed data.
def gzip_header(response, gridout):
    if gridout.metadata.get("compressed"):
        response.headers["Content-Encoding"] = "gzip"


gridfs_handler = AIOHTTPGridFS(client.my_database,
                               set_extra_headers=gzip_header)

app = aiohttp.web.Application()

# The GridFS URL pattern must have a "{filename}" variable.
resource = app.router.add_resource("/fs/{filename}")
resource.add_route("GET", gridfs_handler)
resource.add_route("HEAD", gridfs_handler)

aiohttp.web.run_app(app)
Ejemplo n.º 10
0
 def test_post(self):
     # Only allow GET and HEAD, even if a POST route is added.
     handler = AIOHTTPGridFS(self.db)
     yield from self.start_app(extra_routes={'/fs/{filename}': handler})
     result = yield from self.request('post', '/fs/foo')
     self.assertEqual(405, result.status)