source: uri/tests/base.py@ 1355

Last change on this file since 1355 was 230, checked in by wouter, 3 years ago

#91 clone https://pypi.org/project/rfc3986/

File size: 6.8 KB
RevLine 
[230]1# -*- coding: utf-8 -*-
2# Copyright (c) 2015 Ian Stapleton Cordasco
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17class BaseTestParsesURIs:
18 test_class = None
19
20 """Tests for self.test_class handling of URIs."""
21
22 def test_handles_basic_uri(self, basic_uri):
23 """Test that self.test_class can handle a simple URI."""
24 uri = self.test_class.from_string(basic_uri)
25 assert uri.scheme == "http"
26 assert uri.authority == basic_uri[7:] # len('http://')
27 assert uri.host == uri.authority
28 assert uri.path is None
29 assert uri.query is None
30 assert uri.fragment is None
31 assert uri.port is None
32 assert uri.userinfo is None
33
34 def test_handles_basic_uri_with_port(self, basic_uri_with_port):
35 """Test that self.test_class can handle a simple URI with a port."""
36 uri = self.test_class.from_string(basic_uri_with_port)
37 assert uri.scheme == "ftp"
38 assert uri.authority == basic_uri_with_port[6:]
39 assert uri.host != uri.authority
40 assert str(uri.port) == "21"
41 assert uri.path is None
42 assert uri.query is None
43 assert uri.fragment is None
44 assert uri.userinfo is None
45
46 def test_handles_uri_with_port_and_userinfo(
47 self, uri_with_port_and_userinfo
48 ):
49 """
50 Test that self.test_class can handle a URI with a port and userinfo.
51 """
52 uri = self.test_class.from_string(uri_with_port_and_userinfo)
53 assert uri.scheme == "ssh"
54 # 6 == len('ftp://')
55 assert uri.authority == uri_with_port_and_userinfo[6:]
56 assert uri.host != uri.authority
57 assert str(uri.port) == "22"
58 assert uri.path is None
59 assert uri.query is None
60 assert uri.fragment is None
61 assert uri.userinfo == "user:pass"
62
63 def test_handles_tricky_userinfo(self, uri_with_port_and_tricky_userinfo):
64 """
65 Test that self.test_class can handle a URI with unusual
66 (non a-z) chars in userinfo.
67 """
68 uri = self.test_class.from_string(uri_with_port_and_tricky_userinfo)
69 assert uri.scheme == "ssh"
70 # 6 == len('ftp://')
71 assert uri.authority == uri_with_port_and_tricky_userinfo[6:]
72 assert uri.host != uri.authority
73 assert str(uri.port) == "22"
74 assert uri.path is None
75 assert uri.query is None
76 assert uri.fragment is None
77 assert uri.userinfo == "user%20!=:pass"
78
79 def test_handles_basic_uri_with_path(self, basic_uri_with_path):
80 """Test that self.test_class can handle a URI with a path."""
81 uri = self.test_class.from_string(basic_uri_with_path)
82 assert uri.scheme == "http"
83 assert basic_uri_with_path == (
84 uri.scheme + "://" + uri.authority + uri.path
85 )
86 assert uri.host == uri.authority
87 assert uri.path == "/path/to/resource"
88 assert uri.query is None
89 assert uri.fragment is None
90 assert uri.userinfo is None
91 assert uri.port is None
92
93 def test_handles_uri_with_path_and_query(self, uri_with_path_and_query):
94 """
95 Test that self.test_class can handle a URI with a path and query.
96 """
97 uri = self.test_class.from_string(uri_with_path_and_query)
98 assert uri.scheme == "http"
99 assert uri.host == uri.authority
100 assert uri.path == "/path/to/resource"
101 assert uri.query == "key=value"
102 assert uri.fragment is None
103 assert uri.userinfo is None
104 assert uri.port is None
105
106 def test_handles_uri_with_everything(self, uri_with_everything):
107 """
108 Test that self.test_class can handle and with everything in it.
109 """
110 uri = self.test_class.from_string(uri_with_everything)
111 assert uri.scheme == "https"
112 assert uri.path == "/path/to/resource"
113 assert uri.query == "key=value"
114 assert uri.fragment == "fragment"
115 assert uri.userinfo == "user:pass"
116 assert str(uri.port) == "443"
117
118 def test_handles_relative_uri(self, relative_uri):
119 """Test that self.test_class can handle a relative URI."""
120 uri = self.test_class.from_string(relative_uri)
121 assert uri.scheme is None
122 assert uri.authority == relative_uri[2:]
123
124 def test_handles_percent_in_path(self, uri_path_with_percent):
125 """Test that self.test_class encodes the % character properly."""
126 uri = self.test_class.from_string(uri_path_with_percent)
127 print(uri.path)
128 assert uri.path == "/%25%20"
129
130 def test_handles_percent_in_query(self, uri_query_with_percent):
131 uri = self.test_class.from_string(uri_query_with_percent)
132 assert uri.query == "a=%25"
133
134 def test_handles_percent_in_fragment(self, uri_fragment_with_percent):
135 uri = self.test_class.from_string(uri_fragment_with_percent)
136 assert uri.fragment == "perc%25ent"
137
138
139class BaseTestUnsplits:
140 test_class = None
141
142 def test_basic_uri_unsplits(self, basic_uri):
143 uri = self.test_class.from_string(basic_uri)
144 assert uri.unsplit() == basic_uri
145
146 def test_basic_uri_with_port_unsplits(self, basic_uri_with_port):
147 uri = self.test_class.from_string(basic_uri_with_port)
148 assert uri.unsplit() == basic_uri_with_port
149
150 def test_uri_with_port_and_userinfo_unsplits(
151 self, uri_with_port_and_userinfo
152 ):
153 uri = self.test_class.from_string(uri_with_port_and_userinfo)
154 assert uri.unsplit() == uri_with_port_and_userinfo
155
156 def test_basic_uri_with_path_unsplits(self, basic_uri_with_path):
157 uri = self.test_class.from_string(basic_uri_with_path)
158 assert uri.unsplit() == basic_uri_with_path
159
160 def test_uri_with_path_and_query_unsplits(self, uri_with_path_and_query):
161 uri = self.test_class.from_string(uri_with_path_and_query)
162 assert uri.unsplit() == uri_with_path_and_query
163
164 def test_uri_with_everything_unsplits(self, uri_with_everything):
165 uri = self.test_class.from_string(uri_with_everything)
166 assert uri.unsplit() == uri_with_everything
167
168 def test_relative_uri_unsplits(self, relative_uri):
169 uri = self.test_class.from_string(relative_uri)
170 assert uri.unsplit() == relative_uri
171
172 def test_absolute_path_uri_unsplits(self, absolute_path_uri):
173 uri = self.test_class.from_string(absolute_path_uri)
174 assert uri.unsplit() == absolute_path_uri
Note: See TracBrowser for help on using the repository browser.