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