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