Skip to content

Commit ec56509

Browse files
committed
test GridFS helpers (and fix some bugs along the way)
1 parent 72fdfdd commit ec56509

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

flask_pymongo/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ def get_upload(filename):
211211

212212
# mostly copied from flask/helpers.py, with
213213
# modifications for GridFS
214-
data = wrap_file(request.environ, fileobj, buffer_size=1024 * 256)
214+
data = wrap_file(request.environ, fileobj, buffer_size=1024 * 255)
215215
response = current_app.response_class(
216216
data,
217217
mimetype=fileobj.content_type,
218-
direct_passthrough=True)
218+
direct_passthrough=True,
219+
)
219220
response.content_length = fileobj.length
220221
response.last_modified = fileobj.upload_date
221222
response.set_etag(fileobj.md5)
222223
response.cache_control.max_age = cache_for
223-
response.cache_control.s_max_age = cache_for
224224
response.cache_control.public = True
225225
response.make_conditional(request)
226226
return response

flask_pymongo/tests/test_gridfs.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from hashlib import md5
2+
from io import BytesIO
3+
4+
from gridfs import GridFS
5+
from werkzeug.exceptions import NotFound
6+
import pytest
7+
8+
from flask_pymongo.tests.util import FlaskPyMongoTest
9+
10+
11+
class GridFSCleanupMixin(object):
12+
13+
def tearDown(self):
14+
gridfs = GridFS(self.mongo.db)
15+
files = list(gridfs.find())
16+
for gridfile in files:
17+
gridfs.delete(gridfile._id)
18+
19+
super(GridFSCleanupMixin, self).tearDown()
20+
21+
22+
class TestSaveFile(GridFSCleanupMixin, FlaskPyMongoTest):
23+
24+
def test_it_saves_files(self):
25+
fileobj = BytesIO(b"these are the bytes")
26+
27+
self.mongo.save_file("my-file", fileobj)
28+
29+
gridfs = GridFS(self.mongo.db)
30+
assert gridfs.exists({"filename": "my-file"})
31+
32+
def test_it_guesses_type_from_filename(self):
33+
fileobj = BytesIO(b"these are the bytes")
34+
35+
self.mongo.save_file("my-file.txt", fileobj)
36+
37+
gridfs = GridFS(self.mongo.db)
38+
gridfile = gridfs.find_one({"filename": "my-file.txt"})
39+
assert gridfile.content_type == "text/plain"
40+
41+
42+
class TestSendFile(GridFSCleanupMixin, FlaskPyMongoTest):
43+
44+
def setUp(self):
45+
super(TestSendFile, self).setUp()
46+
47+
# make it bigger than 1 gridfs chunk
48+
self.myfile = BytesIO(b"a" * 500 * 1024)
49+
self.mongo.save_file("myfile.txt", self.myfile)
50+
51+
def test_it_404s_for_missing_files(self):
52+
with pytest.raises(NotFound):
53+
self.mongo.send_file("no-such-file.txt")
54+
55+
def test_it_sets_content_type(self):
56+
resp = self.mongo.send_file("myfile.txt")
57+
assert resp.content_type.startswith("text/plain")
58+
59+
def test_it_sets_content_length(self):
60+
resp = self.mongo.send_file("myfile.txt")
61+
assert resp.content_length == len(self.myfile.getvalue())
62+
63+
def test_it_sets_supports_conditional_gets(self):
64+
# a basic conditional GET
65+
environ_args = {
66+
"method": "GET",
67+
"headers": {
68+
"If-None-Match": md5(self.myfile.getvalue()).hexdigest(),
69+
},
70+
}
71+
72+
with self.app.test_request_context(**environ_args):
73+
resp = self.mongo.send_file("myfile.txt")
74+
assert resp.status_code == 304
75+
76+
def test_it_sets_cache_headers(self):
77+
resp = self.mongo.send_file("myfile.txt", cache_for=60)
78+
assert resp.cache_control.max_age == 60
79+
assert resp.cache_control.public is True
80+
81+
def test_it_streams_results(self):
82+
resp = self.mongo.send_file("myfile.txt")
83+
assert resp.is_streamed

0 commit comments

Comments
 (0)