URI parsing errors throw to `uri-error'
[bpt/guile.git] / test-suite / tests / web-uri.test
1 ;;;; web-uri.test --- URI library -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2010 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20 (define-module (test-web-uri)
21 #:use-module (web uri)
22 #:use-module (ice-9 regex)
23 #:use-module (test-suite lib))
24
25
26 ;; FIXME: need more decode / encode tests
27
28
29 (define* (uri=? uri #:key scheme userinfo host port path query fragment)
30 (and (uri? uri)
31 (equal? (uri-scheme uri) scheme)
32 (equal? (uri-userinfo uri) userinfo)
33 (equal? (uri-host uri) host)
34 (equal? (uri-port uri) port)
35 (equal? (uri-path uri) path)
36 (equal? (uri-query uri) query)
37 (equal? (uri-fragment uri) fragment)))
38
39 (define-syntax pass-if-uri-exception
40 (syntax-rules ()
41 ((_ name pat exp)
42 (pass-if name
43 (catch 'uri-error
44 (lambda () exp (error "expected uri-error exception"))
45 (lambda (k message args)
46 (if (string-match pat message)
47 #t
48 (error "unexpected uri-error exception" message args))))))))
49
50 (with-test-prefix "build-uri"
51 (pass-if "ftp:"
52 (uri=? (build-uri 'ftp)
53 #:scheme 'ftp
54 #:path ""))
55
56 (pass-if "ftp:foo"
57 (uri=? (build-uri 'ftp #:path "foo")
58 #:scheme 'ftp
59 #:path "foo"))
60
61 (pass-if "ftp://foo"
62 (uri=? (build-uri 'ftp #:host "foo")
63 #:scheme 'ftp
64 #:host "foo"
65 #:path ""))
66
67 (pass-if "ftp://foo/bar"
68 (uri=? (build-uri 'ftp #:host "foo" #:path "/bar")
69 #:scheme 'ftp
70 #:host "foo"
71 #:path "/bar"))
72
73 (pass-if "ftp://foo@bar:22/baz"
74 (uri=? (build-uri 'ftp #:userinfo "foo" #:host "bar" #:port 22 #:path "/baz")
75 #:scheme 'ftp
76 #:userinfo "foo"
77 #:host "bar"
78 #:port 22
79 #:path "/baz"))
80
81 (pass-if-uri-exception "non-symbol scheme"
82 "Expected.*symbol"
83 (build-uri "nonsym"))
84
85 (pass-if-uri-exception "http://bad.host.1"
86 "Expected.*host"
87 (build-uri 'http #:host "bad.host.1"))
88
89 (pass-if "http://bad.host.1 (no validation)"
90 (uri=? (build-uri 'http #:host "bad.host.1" #:validate? #f)
91 #:scheme 'http #:host "bad.host.1" #:path ""))
92
93 (pass-if-uri-exception "http://foo:not-a-port"
94 "Expected.*port"
95 (build-uri 'http #:host "foo" #:port "not-a-port"))
96
97 (pass-if-uri-exception "http://foo:10 but port as string"
98 "Expected.*port"
99 (build-uri 'http #:host "foo" #:port "10"))
100
101 (pass-if-uri-exception "http://:10"
102 "Expected.*host"
103 (build-uri 'http #:port 10))
104
105 (pass-if-uri-exception "http://foo@"
106 "Expected.*host"
107 (build-uri 'http #:userinfo "foo")))
108
109
110 (with-test-prefix "parse-uri"
111 (pass-if "ftp:"
112 (uri=? (parse-uri "ftp:")
113 #:scheme 'ftp
114 #:path ""))
115
116 (pass-if "ftp:foo"
117 (uri=? (parse-uri "ftp:foo")
118 #:scheme 'ftp
119 #:path "foo"))
120
121 (pass-if "ftp://foo/bar"
122 (uri=? (parse-uri "ftp://foo/bar")
123 #:scheme 'ftp
124 #:host "foo"
125 #:path "/bar"))
126
127 (pass-if "ftp://foo@bar:22/baz"
128 (uri=? (parse-uri "ftp://foo@bar:22/baz")
129 #:scheme 'ftp
130 #:userinfo "foo"
131 #:host "bar"
132 #:port 22
133 #:path "/baz"))
134
135 (pass-if "http://bad.host.1"
136 (not (parse-uri "http://bad.host.1")))
137
138 (pass-if "http://foo:"
139 (uri=? (parse-uri "http://foo:")
140 #:scheme 'http #:host "foo" #:path ""))
141
142 (pass-if "http://foo:/"
143 (uri=? (parse-uri "http://foo:/")
144 #:scheme 'http #:host "foo" #:path "/"))
145
146 (pass-if "http://foo:not-a-port"
147 (not (parse-uri "http://foo:not-a-port")))
148
149 (pass-if "http://:10"
150 (not (parse-uri "http://:10")))
151
152 (pass-if "http://foo@"
153 (not (parse-uri "http://foo@"))))
154
155 (with-test-prefix "unparse-uri"
156 (pass-if "ftp:"
157 (equal? "ftp:"
158 (unparse-uri (parse-uri "ftp:"))))
159
160 (pass-if "ftp:foo"
161 (equal? "ftp:foo"
162 (unparse-uri (parse-uri "ftp:foo"))))
163
164 (pass-if "ftp://foo/bar"
165 (equal? "ftp://foo/bar"
166 (unparse-uri (parse-uri "ftp://foo/bar"))))
167
168 (pass-if "ftp://foo@bar:22/baz"
169 (equal? "ftp://foo@bar:22/baz"
170 (unparse-uri (parse-uri "ftp://foo@bar:22/baz"))))
171
172 (pass-if "http://foo:"
173 (equal? "http://foo"
174 (unparse-uri (parse-uri "http://foo:"))))
175
176 (pass-if "http://foo:/"
177 (equal? "http://foo/"
178 (unparse-uri (parse-uri "http://foo:/")))))
179
180 (with-test-prefix "decode"
181 (pass-if (equal? "foo bar" (uri-decode "foo%20bar"))))
182
183 (with-test-prefix "encode"
184 (pass-if (equal? "foo%20bar" (uri-encode "foo bar"))))