##// END OF EJS Templates
use request.text for Python 3 (content is always bytes)
MinRK -
Show More
@@ -1,85 +1,85 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
11
12 from IPython.html.utils import url_path_join
12 from IPython.html.utils import url_path_join
13 from .launchnotebook import NotebookTestBase
13 from .launchnotebook import NotebookTestBase
14 from IPython.utils import py3compat
14 from IPython.utils import py3compat
15
15
16 class FilesTest(NotebookTestBase):
16 class FilesTest(NotebookTestBase):
17 def test_hidden_files(self):
17 def test_hidden_files(self):
18 not_hidden = [
18 not_hidden = [
19 u'Γ₯ b',
19 u'Γ₯ b',
20 u'Γ₯ b/Γ§. d',
20 u'Γ₯ b/Γ§. d',
21 ]
21 ]
22 hidden = [
22 hidden = [
23 u'.Γ₯ b',
23 u'.Γ₯ b',
24 u'Γ₯ b/.Γ§ d',
24 u'Γ₯ b/.Γ§ d',
25 ]
25 ]
26 dirs = not_hidden + hidden
26 dirs = not_hidden + hidden
27
27
28 nbdir = self.notebook_dir.name
28 nbdir = self.notebook_dir.name
29 for d in dirs:
29 for d in dirs:
30 path = pjoin(nbdir, d.replace('/', os.sep))
30 path = pjoin(nbdir, d.replace('/', os.sep))
31 if not os.path.exists(path):
31 if not os.path.exists(path):
32 os.mkdir(path)
32 os.mkdir(path)
33 with open(pjoin(path, 'foo'), 'w') as f:
33 with open(pjoin(path, 'foo'), 'w') as f:
34 f.write('foo')
34 f.write('foo')
35 with open(pjoin(path, '.foo'), 'w') as f:
35 with open(pjoin(path, '.foo'), 'w') as f:
36 f.write('.foo')
36 f.write('.foo')
37 url = self.base_url()
37 url = self.base_url()
38
38
39 for d in not_hidden:
39 for d in not_hidden:
40 path = pjoin(nbdir, d.replace('/', os.sep))
40 path = pjoin(nbdir, d.replace('/', os.sep))
41 r = requests.get(url_path_join(url, 'files', d, 'foo'))
41 r = requests.get(url_path_join(url, 'files', d, 'foo'))
42 r.raise_for_status()
42 r.raise_for_status()
43 self.assertEqual(r.content, b'foo')
43 self.assertEqual(r.text, 'foo')
44 r = requests.get(url_path_join(url, 'files', d, '.foo'))
44 r = requests.get(url_path_join(url, 'files', d, '.foo'))
45 self.assertEqual(r.status_code, 403)
45 self.assertEqual(r.status_code, 403)
46
46
47 for d in hidden:
47 for d in hidden:
48 path = pjoin(nbdir, d.replace('/', os.sep))
48 path = pjoin(nbdir, d.replace('/', os.sep))
49 for foo in ('foo', '.foo'):
49 for foo in ('foo', '.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, 403)
51 self.assertEqual(r.status_code, 403)
52
52
53 def test_old_files_redirect(self):
53 def test_old_files_redirect(self):
54 """pre-2.0 'files/' prefixed links are properly redirected"""
54 """pre-2.0 'files/' prefixed links are properly redirected"""
55 nbdir = self.notebook_dir.name
55 nbdir = self.notebook_dir.name
56 base = self.base_url()
56 base = self.base_url()
57
57
58 os.mkdir(pjoin(nbdir, 'files'))
58 os.mkdir(pjoin(nbdir, 'files'))
59 os.makedirs(pjoin(nbdir, 'sub', 'files'))
59 os.makedirs(pjoin(nbdir, 'sub', 'files'))
60
60
61 for prefix in ('', 'sub'):
61 for prefix in ('', 'sub'):
62 with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f:
62 with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f:
63 f.write(prefix + '/files/f1')
63 f.write(prefix + '/files/f1')
64 with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f:
64 with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f:
65 f.write(prefix + '/files/f2')
65 f.write(prefix + '/files/f2')
66 with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f:
66 with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f:
67 f.write(prefix + '/f2')
67 f.write(prefix + '/f2')
68 with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f:
68 with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f:
69 f.write(prefix + '/f3')
69 f.write(prefix + '/f3')
70
70
71 url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt')
71 url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt')
72 r = requests.get(url)
72 r = requests.get(url)
73 self.assertEqual(r.status_code, 200)
73 self.assertEqual(r.status_code, 200)
74 self.assertEqual(r.content, prefix + '/files/f1')
74 self.assertEqual(r.text, prefix + '/files/f1')
75
75
76 url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt')
76 url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt')
77 r = requests.get(url)
77 r = requests.get(url)
78 self.assertEqual(r.status_code, 200)
78 self.assertEqual(r.status_code, 200)
79 self.assertEqual(r.content, prefix + '/files/f2')
79 self.assertEqual(r.text, prefix + '/files/f2')
80
80
81 url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt')
81 url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt')
82 r = requests.get(url)
82 r = requests.get(url)
83 self.assertEqual(r.status_code, 200)
83 self.assertEqual(r.status_code, 200)
84 self.assertEqual(r.content, prefix + '/f3')
84 self.assertEqual(r.text, prefix + '/f3')
85
85
General Comments 0
You need to be logged in to leave comments. Login now