Show More
@@ -1,134 +1,134 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 | f.write("\x5F\x9D\x3E") | |
|
79 | f.write(b"\x5F\x9D\x3E") | |
|
80 | 80 | f.close() |
|
81 | 81 | |
|
82 | 82 | with io.open(pjoin(nbdir, 'test.txt'), 'w') as f: |
|
83 | 83 | f.write(u'foo\nbar') |
|
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(u'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 | 94 | self.assertEqual(r.content, 'X50+\n') |
|
95 | 95 | |
|
96 | 96 | r = requests.get(url_path_join(base, 'files', 'test.txt')) |
|
97 | 97 | self.assertEqual(r.status_code, 200) |
|
98 | 98 | self.assertEqual(r.headers['content-type'], 'text/plain') |
|
99 | 99 | self.assertEqual(r.content, u'foo\nbar') |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | def test_old_files_redirect(self): |
|
103 | 103 | """pre-2.0 'files/' prefixed links are properly redirected""" |
|
104 | 104 | nbdir = self.notebook_dir.name |
|
105 | 105 | base = self.base_url() |
|
106 | 106 | |
|
107 | 107 | os.mkdir(pjoin(nbdir, 'files')) |
|
108 | 108 | os.makedirs(pjoin(nbdir, 'sub', 'files')) |
|
109 | 109 | |
|
110 | 110 | for prefix in ('', 'sub'): |
|
111 | 111 | with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f: |
|
112 | 112 | f.write(prefix + '/files/f1') |
|
113 | 113 | with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f: |
|
114 | 114 | f.write(prefix + '/files/f2') |
|
115 | 115 | with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f: |
|
116 | 116 | f.write(prefix + '/f2') |
|
117 | 117 | with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f: |
|
118 | 118 | f.write(prefix + '/f3') |
|
119 | 119 | |
|
120 | 120 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt') |
|
121 | 121 | r = requests.get(url) |
|
122 | 122 | self.assertEqual(r.status_code, 200) |
|
123 | 123 | self.assertEqual(r.text, prefix + '/files/f1') |
|
124 | 124 | |
|
125 | 125 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt') |
|
126 | 126 | r = requests.get(url) |
|
127 | 127 | self.assertEqual(r.status_code, 200) |
|
128 | 128 | self.assertEqual(r.text, prefix + '/files/f2') |
|
129 | 129 | |
|
130 | 130 | url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt') |
|
131 | 131 | r = requests.get(url) |
|
132 | 132 | self.assertEqual(r.status_code, 200) |
|
133 | 133 | self.assertEqual(r.text, prefix + '/f3') |
|
134 | 134 |
General Comments 0
You need to be logged in to leave comments.
Login now