@@ -24,27 +24,25 @@ Let's assume that we're building something that takes user input for a URL and
24
24
we want to ensure that URL is only ever using a specific domain with https. In
25
25
that case, our code would look like this:
26
26
27
- .. doctest ::
28
-
29
- >>> from rfc3986 import validators, uri_reference
30
- >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
31
- >>> validator = validators.Validator().allow_schemes(
32
- ... ' https' ,
33
- ... ).allow_hosts(
34
- ... ' github.com' ,
35
- ... )
36
- >>> validator.validate(uri_reference(
37
- ... ' https://github.com/sigmavirus24/rfc3986'
38
- ... ))
39
- >>> validator.validate(uri_reference(
40
- ... ' https://github.com/'
41
- ... ))
42
- >>> validator.validate(uri_reference(
43
- ... ' http://example.com'
44
- ... ))
45
- Traceback (most recent call last):
46
- ...
47
- rfc3986.exceptions.UnpermittedComponentError
27
+ >>> from rfc3986 import validators, uri_reference
28
+ >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
29
+ >>> validator = validators.Validator().allow_schemes(
30
+ ... ' https' ,
31
+ ... ).allow_hosts(
32
+ ... ' github.com' ,
33
+ ... )
34
+ >>> validator.validate(uri_reference(
35
+ ... ' https://github.com/sigmavirus24/rfc3986'
36
+ ... ))
37
+ >>> validator.validate(uri_reference(
38
+ ... ' https://github.com/'
39
+ ... ))
40
+ >>> validator.validate(uri_reference(
41
+ ... ' http://example.com'
42
+ ... ))
43
+ Traceback (most recent call last):
44
+ ...
45
+ rfc3986.exceptions.UnpermittedComponentError
48
46
49
47
First notice that we can easily reuse our validator object for each URL.
50
48
This allows users to not have to constantly reconstruct Validators for each
@@ -65,36 +63,34 @@ Next, let's imagine that we want to prevent leaking user credentials. In that
65
63
case, we want to ensure that there is no password in the user information
66
64
portion of the authority. In that case, our new validator would look like this:
67
65
68
- .. doctest ::
69
-
70
- >>> from rfc3986 import validators, uri_reference
71
- >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
72
- >>> validator = validators.Validator().allow_schemes(
73
- ... ' https' ,
74
- ... ).allow_hosts(
75
- ... ' github.com' ,
76
- ... ).forbid_use_of_password()
77
- >>> validator.validate(uri_reference(
78
- ... ' https://github.com/sigmavirus24/rfc3986'
79
- ... ))
80
- >>> validator.validate(uri_reference(
81
- ... ' https://github.com/'
82
- ... ))
83
- >>> validator.validate(uri_reference(
84
- ... ' http://example.com'
85
- ... ))
86
- Traceback (most recent call last):
87
- ...
88
- rfc3986.exceptions.UnpermittedComponentError
89
- >>> validator.validate(uri_reference(
90
-
91
- ... ))
92
- >>> validator.validate(uri_reference(
93
- ...
' https://sigmavirus24:[email protected] '
94
- ... ))
95
- Traceback (most recent call last):
96
- ...
97
- rfc3986.exceptions.PasswordForbidden
66
+ >>> from rfc3986 import validators, uri_reference
67
+ >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
68
+ >>> validator = validators.Validator().allow_schemes(
69
+ ... ' https' ,
70
+ ... ).allow_hosts(
71
+ ... ' github.com' ,
72
+ ... ).forbid_use_of_password()
73
+ >>> validator.validate(uri_reference(
74
+ ... ' https://github.com/sigmavirus24/rfc3986'
75
+ ... ))
76
+ >>> validator.validate(uri_reference(
77
+ ... ' https://github.com/'
78
+ ... ))
79
+ >>> validator.validate(uri_reference(
80
+ ... ' http://example.com'
81
+ ... ))
82
+ Traceback (most recent call last):
83
+ ...
84
+ rfc3986.exceptions.UnpermittedComponentError
85
+ >>> validator.validate(uri_reference(
86
+
87
+ ... ))
88
+ >>> validator.validate(uri_reference(
89
+ ...
' https://sigmavirus24:[email protected] '
90
+ ... ))
91
+ Traceback (most recent call last):
92
+ ...
93
+ rfc3986.exceptions.PasswordForbidden
98
94
99
95
Requiring the Presence of Components
100
96
------------------------------------
@@ -104,47 +100,43 @@ components for validation. For example, we assume that we will have a URL that
104
100
has a scheme and hostname. However, our current validation doesn't require
105
101
those items exist.
106
102
107
- .. doctest ::
108
-
109
- >>> from rfc3986 import validators, uri_reference
110
- >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
111
- >>> validator = validators.Validator().allow_schemes(
112
- ... ' https' ,
113
- ... ).allow_hosts(
114
- ... ' github.com' ,
115
- ... ).forbid_use_of_password()
116
- >>> validator.validate(uri_reference(' //github.com' ))
117
- >>> validator.validate(uri_reference(' https:/' ))
103
+ >>> from rfc3986 import validators, uri_reference
104
+ >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
105
+ >>> validator = validators.Validator().allow_schemes(
106
+ ... ' https' ,
107
+ ... ).allow_hosts(
108
+ ... ' github.com' ,
109
+ ... ).forbid_use_of_password()
110
+ >>> validator.validate(uri_reference(' //github.com' ))
111
+ >>> validator.validate(uri_reference(' https:/' ))
118
112
119
113
In the first case, we have a host name but no scheme and in the second we have
120
114
a scheme and a path but no host. If we want to ensure that those components
121
115
are there and that they are *always * what we allow, then we must add one last
122
116
item to our validator:
123
117
124
- .. doctest ::
125
-
126
- >>> from rfc3986 import validators, uri_reference
127
- >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
128
- >>> validator = validators.Validator().allow_schemes(
129
- ... ' https' ,
130
- ... ).allow_hosts(
131
- ... ' github.com' ,
132
- ... ).forbid_use_of_password(
133
- ... ).require_presence_of(
134
- ... ' scheme' , ' host' ,
135
- ... )
136
- >>> validator.validate(uri_reference(' //github.com' ))
137
- Traceback (most recent call last):
138
- ...
139
- rfc3986.exceptions.MissingComponentError
140
- >>> validator.validate(uri_reference(' https:/' ))
141
- Traceback (most recent call last):
142
- ...
143
- rfc3986.exceptions.MissingComponentError
144
- >>> validator.validate(uri_reference(' https://github.com' ))
145
- >>> validator.validate(uri_reference(
146
- ... ' https://github.com/sigmavirus24/rfc3986'
147
- ... ))
118
+ >>> from rfc3986 import validators, uri_reference
119
+ >>> user_url = ' https://github.com/sigmavirus24/rfc3986'
120
+ >>> validator = validators.Validator().allow_schemes(
121
+ ... ' https' ,
122
+ ... ).allow_hosts(
123
+ ... ' github.com' ,
124
+ ... ).forbid_use_of_password(
125
+ ... ).require_presence_of(
126
+ ... ' scheme' , ' host' ,
127
+ ... )
128
+ >>> validator.validate(uri_reference(' //github.com' ))
129
+ Traceback (most recent call last):
130
+ ...
131
+ rfc3986.exceptions.MissingComponentError
132
+ >>> validator.validate(uri_reference(' https:/' ))
133
+ Traceback (most recent call last):
134
+ ...
135
+ rfc3986.exceptions.MissingComponentError
136
+ >>> validator.validate(uri_reference(' https://github.com' ))
137
+ >>> validator.validate(uri_reference(
138
+ ... ' https://github.com/sigmavirus24/rfc3986'
139
+ ... ))
148
140
149
141
150
142
Checking the Validity of Components
@@ -156,26 +148,24 @@ examples we can also check that a URI is valid per :rfc:`3986`. The validation
156
148
of the components is pre-determined so all we need to do is specify which
157
149
components we want to validate:
158
150
159
- .. doctest ::
160
-
161
- >>> from rfc3986 import validators, uri_reference
162
- >>> valid_uri = uri_reference(' https://github.com/' )
163
- >>> validator = validators.Validator().allow_schemes(
164
- ... ' https' ,
165
- ... ).allow_hosts(
166
- ... ' github.com' ,
167
- ... ).forbid_use_of_password(
168
- ... ).require_presence_of(
169
- ... ' scheme' , ' host' ,
170
- ... ).check_validity_of(
171
- ... ' scheme' , ' host' , ' path' ,
172
- ... )
173
- >>> validator.validate(valid_uri)
174
- >>> invalid_uri = valid_uri.copy_with(path = ' /#invalid/path' )
175
- >>> validator.validate(invalid_uri)
176
- Traceback (most recent call last):
177
- ...
178
- rfc3986.exceptions.InvalidComponentsError
151
+ >>> from rfc3986 import validators, uri_reference
152
+ >>> valid_uri = uri_reference(' https://github.com/' )
153
+ >>> validator = validators.Validator().allow_schemes(
154
+ ... ' https' ,
155
+ ... ).allow_hosts(
156
+ ... ' github.com' ,
157
+ ... ).forbid_use_of_password(
158
+ ... ).require_presence_of(
159
+ ... ' scheme' , ' host' ,
160
+ ... ).check_validity_of(
161
+ ... ' scheme' , ' host' , ' path' ,
162
+ ... )
163
+ >>> validator.validate(valid_uri)
164
+ >>> invalid_uri = valid_uri.copy_with(path = ' /#invalid/path' )
165
+ >>> validator.validate(invalid_uri)
166
+ Traceback (most recent call last):
167
+ ...
168
+ rfc3986.exceptions.InvalidComponentsError
179
169
180
170
Paths are not allowed to contain a ``# `` character unless it's
181
171
percent-encoded. This is why our ``invalid_uri `` raises an exception when we
0 commit comments