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