Show More
@@ -0,0 +1,61 b'' | |||
|
1 | """Test HTML utils""" | |
|
2 | ||
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (C) 2013 The IPython Development Team | |
|
5 | # | |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
|
7 | # the file COPYING, distributed as part of this software. | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | import nose.tools as nt | |
|
15 | ||
|
16 | import IPython.testing.tools as tt | |
|
17 | from IPython.html.utils import url_escape, url_unescape | |
|
18 | ||
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # Test functions | |
|
21 | #----------------------------------------------------------------------------- | |
|
22 | ||
|
23 | def test_help_output(): | |
|
24 | """ipython notebook --help-all works""" | |
|
25 | tt.help_all_output_test('notebook') | |
|
26 | ||
|
27 | ||
|
28 | def test_url_escape(): | |
|
29 | ||
|
30 | # changes path or notebook name with special characters to url encoding | |
|
31 | # these tests specifically encode paths with spaces | |
|
32 | path = url_escape('/this is a test/for spaces/') | |
|
33 | nt.assert_equal(path, '/this%20is%20a%20test/for%20spaces/') | |
|
34 | ||
|
35 | path = url_escape('notebook with space.ipynb') | |
|
36 | nt.assert_equal(path, 'notebook%20with%20space.ipynb') | |
|
37 | ||
|
38 | path = url_escape('/path with a/notebook and space.ipynb') | |
|
39 | nt.assert_equal(path, '/path%20with%20a/notebook%20and%20space.ipynb') | |
|
40 | ||
|
41 | path = url_escape('/ !@$#%^&* / test %^ notebook @#$ name.ipynb') | |
|
42 | nt.assert_equal(path, | |
|
43 | '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') | |
|
44 | ||
|
45 | def test_url_unescape(): | |
|
46 | ||
|
47 | # decodes a url string to a plain string | |
|
48 | # these tests decode paths with spaces | |
|
49 | path = url_unescape('/this%20is%20a%20test/for%20spaces/') | |
|
50 | nt.assert_equal(path, '/this is a test/for spaces/') | |
|
51 | ||
|
52 | path = url_unescape('notebook%20with%20space.ipynb') | |
|
53 | nt.assert_equal(path, 'notebook with space.ipynb') | |
|
54 | ||
|
55 | path = url_unescape('/path%20with%20a/notebook%20and%20space.ipynb') | |
|
56 | nt.assert_equal(path, '/path with a/notebook and space.ipynb') | |
|
57 | ||
|
58 | path = url_unescape( | |
|
59 | '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') | |
|
60 | nt.assert_equal(path, '/ !@$#%^&* / test %^ notebook @#$ name.ipynb') | |
|
61 |
@@ -24,7 +24,7 b' from zmq.utils import jsonapi' | |||
|
24 | 24 | |
|
25 | 25 | from ..base.handlers import IPythonHandler |
|
26 | 26 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex |
|
27 | from ..utils import url_path_join | |
|
27 | from ..utils import url_path_join, url_escape, url_unescape | |
|
28 | 28 | from urllib import quote |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
@@ -64,8 +64,8 b' class NamedNotebookHandler(IPythonHandler):' | |||
|
64 | 64 | # a .ipynb filename was given |
|
65 | 65 | if not nbm.notebook_exists(name, path): |
|
66 | 66 | raise web.HTTPError(404, u'Notebook does not exist: %s/%s' % (path, name)) |
|
67 |
name = |
|
|
68 |
path = |
|
|
67 | name = url_escape(name) | |
|
68 | path = url_escape(path) | |
|
69 | 69 | self.write(self.render_template('notebook.html', |
|
70 | 70 | project=self.project_dir, |
|
71 | 71 | notebook_path=path, |
@@ -91,18 +91,6 b' class NotebookManager(LoggingConfigurable):' | |||
|
91 | 91 | path = os.path.join(self.notebook_dir, *parts) |
|
92 | 92 | return path |
|
93 | 93 | |
|
94 | def url_encode(self, path): | |
|
95 | """Takes a URL path with special characters and returns | |
|
96 | the path with all these characters URL encoded""" | |
|
97 | parts = path.split('/') | |
|
98 | return '/'.join([quote(p) for p in parts]) | |
|
99 | ||
|
100 | def url_decode(self, path): | |
|
101 | """Takes a URL path with encoded special characters and | |
|
102 | returns the URL with special characters decoded""" | |
|
103 | parts = path.split('/') | |
|
104 | return '/'.join([unquote(p) for p in parts]) | |
|
105 | ||
|
106 | 94 | def _notebook_dir_changed(self, name, old, new): |
|
107 | 95 | """Do a bit of validation of the notebook dir.""" |
|
108 | 96 | if not os.path.isabs(new): |
@@ -67,42 +67,6 b' class TestNotebookManager(TestCase):' | |||
|
67 | 67 | except OSError: |
|
68 | 68 | print "Directory already exists." |
|
69 | 69 | |
|
70 | def test_url_encode(self): | |
|
71 | nm = NotebookManager() | |
|
72 | ||
|
73 | # changes path or notebook name with special characters to url encoding | |
|
74 | # these tests specifically encode paths with spaces | |
|
75 | path = nm.url_encode('/this is a test/for spaces/') | |
|
76 | self.assertEqual(path, '/this%20is%20a%20test/for%20spaces/') | |
|
77 | ||
|
78 | path = nm.url_encode('notebook with space.ipynb') | |
|
79 | self.assertEqual(path, 'notebook%20with%20space.ipynb') | |
|
80 | ||
|
81 | path = nm.url_encode('/path with a/notebook and space.ipynb') | |
|
82 | self.assertEqual(path, '/path%20with%20a/notebook%20and%20space.ipynb') | |
|
83 | ||
|
84 | path = nm.url_encode('/ !@$#%^&* / test %^ notebook @#$ name.ipynb') | |
|
85 | self.assertEqual(path, | |
|
86 | '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') | |
|
87 | ||
|
88 | def test_url_decode(self): | |
|
89 | nm = NotebookManager() | |
|
90 | ||
|
91 | # decodes a url string to a plain string | |
|
92 | # these tests decode paths with spaces | |
|
93 | path = nm.url_decode('/this%20is%20a%20test/for%20spaces/') | |
|
94 | self.assertEqual(path, '/this is a test/for spaces/') | |
|
95 | ||
|
96 | path = nm.url_decode('notebook%20with%20space.ipynb') | |
|
97 | self.assertEqual(path, 'notebook with space.ipynb') | |
|
98 | ||
|
99 | path = nm.url_decode('/path%20with%20a/notebook%20and%20space.ipynb') | |
|
100 | self.assertEqual(path, '/path with a/notebook and space.ipynb') | |
|
101 | ||
|
102 | path = nm.url_decode( | |
|
103 | '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') | |
|
104 | self.assertEqual(path, '/ !@$#%^&* / test %^ notebook @#$ name.ipynb') | |
|
105 | ||
|
106 | 70 | def test_create_notebook_model(self): |
|
107 | 71 | with TemporaryDirectory() as td: |
|
108 | 72 | # Test in root directory |
@@ -49,3 +49,18 b' def url2path(url):' | |||
|
49 | 49 | path = os.path.join(*pieces) |
|
50 | 50 | return path |
|
51 | 51 | |
|
52 | def url_escape(path): | |
|
53 | """Escape special characters in a URL path | |
|
54 | ||
|
55 | Turns '/foo bar/' into '/foo%20bar/' | |
|
56 | """ | |
|
57 | parts = path.split('/') | |
|
58 | return '/'.join([quote(p) for p in parts]) | |
|
59 | ||
|
60 | def url_unescape(path): | |
|
61 | """Unescape special characters in a URL path | |
|
62 | ||
|
63 | Turns '/foo%20bar/' into '/foo bar/' | |
|
64 | """ | |
|
65 | return '/'.join([unquote(p) for p in path.split('/')]) | |
|
66 |
General Comments 0
You need to be logged in to leave comments.
Login now