##// END OF EJS Templates
correctly handle base64 and json, improve bin-file test
Manuel Riel -
Show More
@@ -6,6 +6,7 b''
6 import logging
6 import logging
7 import os
7 import os
8 import mimetypes
8 import mimetypes
9 import json
9 try:
10 try:
10 # py3
11 # py3
11 from http.client import responses
12 from http.client import responses
@@ -34,7 +35,9 b' class FilesHandler(IPythonHandler):'
34 raise web.HTTPError(404)
35 raise web.HTTPError(404)
35
36
36 path, name = os.path.split(path)
37 path, name = os.path.split(path)
37 if name.endswith('.ipynb'):
38 model = cm.get_model(name, path)
39
40 if model['type'] == 'notebook':
38 self.set_header('Content-Type', 'application/json')
41 self.set_header('Content-Type', 'application/json')
39 else:
42 else:
40 cur_mime = mimetypes.guess_type(name)[0]
43 cur_mime = mimetypes.guess_type(name)[0]
@@ -42,6 +45,12 b' class FilesHandler(IPythonHandler):'
42 self.set_header('Content-Type', cur_mime)
45 self.set_header('Content-Type', cur_mime)
43
46
44 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
47 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
45 self.write(cm.get_model(name, path)['content'])
48
49 if model['format'] == 'base64':
50 self.write(model['content'].decode('base64'))
51 elif model['format'] == 'json':
52 self.write(json.dumps(model['content']))
53 else:
54 self.write(model['content'])
46 self.flush()
55 self.flush()
47
56
@@ -76,7 +76,7 b' class FilesTest(NotebookTestBase):'
76 write(nb, f, format='ipynb')
76 write(nb, f, format='ipynb')
77
77
78 with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
78 with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
79 f.write(b'\xFF' + os.urandom(5))
79 f.write(b'\xff' + os.urandom(5))
80 f.close()
80 f.close()
81
81
82 with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
82 with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
@@ -91,7 +91,9 b' class FilesTest(NotebookTestBase):'
91 r = requests.get(url_path_join(base, 'files', 'test.bin'))
91 r = requests.get(url_path_join(base, 'files', 'test.bin'))
92 self.assertEqual(r.status_code, 200)
92 self.assertEqual(r.status_code, 200)
93 self.assertEqual(r.headers['content-type'], 'application/octet-stream')
93 self.assertEqual(r.headers['content-type'], 'application/octet-stream')
94 self.assertTrue(r.content.startswith(b'\xFF'))
94
95 self.assertEqual("{:02x}".format(ord(r.content[0])), 'ff')
96 self.assertEqual(len(r.content), 6)
95
97
96 r = requests.get(url_path_join(base, 'files', 'test.txt'))
98 r = requests.get(url_path_join(base, 'files', 'test.txt'))
97 self.assertEqual(r.status_code, 200)
99 self.assertEqual(r.status_code, 200)
General Comments 0
You need to be logged in to leave comments. Login now