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