Show More
@@ -1,136 +1,137 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Test the /files/ handler.""" |
|
3 | 3 | |
|
4 | 4 | import io |
|
5 | 5 | import os |
|
6 | 6 | from unicodedata import normalize |
|
7 | 7 | |
|
8 | 8 | pjoin = os.path.join |
|
9 | 9 | |
|
10 | 10 | import requests |
|
11 | 11 | import json |
|
12 | import binascii | |
|
12 | 13 | |
|
13 | 14 | from IPython.nbformat.current import (new_notebook, write, new_worksheet, |
|
14 | 15 | new_heading_cell, new_code_cell, |
|
15 | 16 | new_output) |
|
16 | 17 | |
|
17 | 18 | from IPython.html.utils import url_path_join |
|
18 | 19 | from .launchnotebook import NotebookTestBase |
|
19 | 20 | from IPython.utils import py3compat |
|
20 | 21 | |
|
21 | 22 | |
|
22 | 23 | class FilesTest(NotebookTestBase): |
|
23 | 24 | def test_hidden_files(self): |
|
24 | 25 | not_hidden = [ |
|
25 | 26 | u'Γ₯ b', |
|
26 | 27 | u'Γ₯ b/Γ§. d', |
|
27 | 28 | ] |
|
28 | 29 | hidden = [ |
|
29 | 30 | u'.Γ₯ b', |
|
30 | 31 | u'Γ₯ b/.Γ§ d', |
|
31 | 32 | ] |
|
32 | 33 | dirs = not_hidden + hidden |
|
33 | 34 | |
|
34 | 35 | nbdir = self.notebook_dir.name |
|
35 | 36 | for d in dirs: |
|
36 | 37 | path = pjoin(nbdir, d.replace('/', os.sep)) |
|
37 | 38 | if not os.path.exists(path): |
|
38 | 39 | os.mkdir(path) |
|
39 | 40 | with open(pjoin(path, 'foo'), 'w') as f: |
|
40 | 41 | f.write('foo') |
|
41 | 42 | with open(pjoin(path, '.foo'), 'w') as f: |
|
42 | 43 | f.write('.foo') |
|
43 | 44 | url = self.base_url() |
|
44 | 45 | |
|
45 | 46 | for d in not_hidden: |
|
46 | 47 | path = pjoin(nbdir, d.replace('/', os.sep)) |
|
47 | 48 | r = requests.get(url_path_join(url, 'files', d, 'foo')) |
|
48 | 49 | r.raise_for_status() |
|
49 | 50 | self.assertEqual(r.text, 'foo') |
|
50 | 51 | r = requests.get(url_path_join(url, 'files', d, '.foo')) |
|
51 | 52 | self.assertEqual(r.status_code, 404) |
|
52 | 53 | |
|
53 | 54 | for d in hidden: |
|
54 | 55 | path = pjoin(nbdir, d.replace('/', os.sep)) |
|
55 | 56 | for foo in ('foo', '.foo'): |
|
56 | 57 | r = requests.get(url_path_join(url, 'files', d, foo)) |
|
57 | 58 | self.assertEqual(r.status_code, 404) |
|
58 | 59 | |
|
59 | 60 | def test_contents_manager(self): |
|
60 | 61 | "make sure ContentsManager returns right files (ipynb, bin, txt)." |
|
61 | 62 | |
|
62 | 63 | nbdir = self.notebook_dir.name |
|
63 | 64 | base = self.base_url() |
|
64 | 65 | |
|
65 | 66 | nb = new_notebook(name='testnb') |
|
66 | 67 | |
|
67 | 68 | ws = new_worksheet() |
|
68 | 69 | nb.worksheets = [ws] |
|
69 | 70 | ws.cells.append(new_heading_cell(u'Created by test Β³')) |
|
70 | 71 | cc1 = new_code_cell(input=u'print(2*6)') |
|
71 | 72 | cc1.outputs.append(new_output(output_text=u'12', output_type='stream')) |
|
72 | 73 | ws.cells.append(cc1) |
|
73 | 74 | |
|
74 | 75 | with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', |
|
75 | 76 | encoding='utf-8') as f: |
|
76 | 77 | write(nb, f, format='ipynb') |
|
77 | 78 | |
|
78 | 79 | with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: |
|
79 | 80 | f.write(b'\xff' + os.urandom(5)) |
|
80 | 81 | f.close() |
|
81 | 82 | |
|
82 | 83 | with io.open(pjoin(nbdir, 'test.txt'), 'w') as f: |
|
83 | 84 | f.write(u'foobar') |
|
84 | 85 | f.close() |
|
85 | 86 | |
|
86 | 87 | r = requests.get(url_path_join(base, 'files', 'testnb.ipynb')) |
|
87 | 88 | self.assertEqual(r.status_code, 200) |
|
88 | 89 | self.assertIn('print(2*6)', r.text) |
|
89 | 90 | json.loads(r.text) |
|
90 | 91 | |
|
91 | 92 | r = requests.get(url_path_join(base, 'files', 'test.bin')) |
|
92 | 93 | self.assertEqual(r.status_code, 200) |
|
93 | 94 | self.assertEqual(r.headers['content-type'], 'application/octet-stream') |
|
94 | 95 | |
|
95 |
self.assertEqual( |
|
|
96 | self.assertEqual(binascii.hexlify(r.content[0]), 'ff') | |
|
96 | 97 | self.assertEqual(len(r.content), 6) |
|
97 | 98 | |
|
98 | 99 | r = requests.get(url_path_join(base, 'files', 'test.txt')) |
|
99 | 100 | self.assertEqual(r.status_code, 200) |
|
100 | 101 | self.assertEqual(r.headers['content-type'], 'text/plain') |
|
101 | 102 | self.assertEqual(r.text, 'foobar') |
|
102 | 103 | |
|
103 | 104 | |
|
104 | 105 | def test_old_files_redirect(self): |
|
105 | 106 | """pre-2.0 'files/' prefixed links are properly redirected""" |
|
106 | 107 | nbdir = self.notebook_dir.name |
|
107 | 108 | base = self.base_url() |
|
108 | 109 | |
|
109 | 110 | os.mkdir(pjoin(nbdir, 'files')) |
|
110 | 111 | os.makedirs(pjoin(nbdir, 'sub', 'files')) |
|
111 | 112 | |
|
112 | 113 | for prefix in ('', 'sub'): |
|
113 | 114 | with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f: |
|
114 | 115 | f.write(prefix + '/files/f1') |
|
115 | 116 | with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f: |
|
116 | 117 | f.write(prefix + '/files/f2') |
|
117 | 118 | with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f: |
|
118 | 119 | f.write(prefix + '/f2') |
|
119 | 120 | with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f: |
|
120 | 121 | f.write(prefix + '/f3') |
|
121 | 122 | |
|
122 | 123 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt') |
|
123 | 124 | r = requests.get(url) |
|
124 | 125 | self.assertEqual(r.status_code, 200) |
|
125 | 126 | self.assertEqual(r.text, prefix + '/files/f1') |
|
126 | 127 | |
|
127 | 128 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt') |
|
128 | 129 | r = requests.get(url) |
|
129 | 130 | self.assertEqual(r.status_code, 200) |
|
130 | 131 | self.assertEqual(r.text, prefix + '/files/f2') |
|
131 | 132 | |
|
132 | 133 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt') |
|
133 | 134 | r = requests.get(url) |
|
134 | 135 | self.assertEqual(r.status_code, 200) |
|
135 | 136 | self.assertEqual(r.text, prefix + '/f3') |
|
136 | 137 |
General Comments 0
You need to be logged in to leave comments.
Login now