1 ;;;; web-uri.test --- URI library -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-web-uri)
21 #:use-module (web uri)
22 #:use-module (ice-9 regex)
23 #:use-module (test-suite lib))
26 ;; FIXME: need more decode / encode tests
29 (define* (uri=? uri #:key scheme userinfo host port path query fragment)
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)))
39 (define-syntax pass-if-uri-exception
44 (lambda () exp (error "expected uri-error exception"))
45 (lambda (k message args)
46 (if (string-match pat message)
48 (error "unexpected uri-error exception" message args))))))))
50 (with-test-prefix "build-uri"
52 (uri=? (build-uri 'ftp)
57 (uri=? (build-uri 'ftp #:path "foo")
62 (uri=? (build-uri 'ftp #:host "foo")
67 (pass-if "ftp://foo/bar"
68 (uri=? (build-uri 'ftp #:host "foo" #:path "/bar")
73 (pass-if "ftp://foo@bar:22/baz"
74 (uri=? (build-uri 'ftp #:userinfo "foo" #:host "bar" #:port 22 #:path "/baz")
81 (pass-if-uri-exception "non-symbol scheme"
85 (pass-if-uri-exception "http://bad.host.1"
87 (build-uri 'http #:host "bad.host.1"))
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 ""))
93 (pass-if "http://1.good.host"
94 (uri=? (build-uri 'http #:host "1.good.host")
95 #:scheme 'http #:host "1.good.host" #:path ""))
97 (pass-if "http://192.0.2.1"
98 (uri=? (build-uri 'http #:host "192.0.2.1")
99 #:scheme 'http #:host "192.0.2.1" #:path ""))
101 (pass-if "http://[2001:db8::1]"
102 (uri=? (build-uri 'http #:host "2001:db8::1")
103 #:scheme 'http #:host "2001:db8::1" #:path ""))
105 (pass-if "http://[::ffff:192.0.2.1]"
106 (uri=? (build-uri 'http #:host "::ffff:192.0.2.1")
107 #:scheme 'http #:host "::ffff:192.0.2.1" #:path ""))
109 (pass-if-uri-exception "http://foo:not-a-port"
111 (build-uri 'http #:host "foo" #:port "not-a-port"))
113 (pass-if-uri-exception "http://foo:10 but port as string"
115 (build-uri 'http #:host "foo" #:port "10"))
117 (pass-if-uri-exception "http://:10"
119 (build-uri 'http #:port 10))
121 (pass-if-uri-exception "http://foo@"
123 (build-uri 'http #:userinfo "foo")))
126 (with-test-prefix "string->uri"
128 (uri=? (string->uri "ftp:")
133 (uri=? (string->uri "ftp:foo")
137 (pass-if "ftp://foo/bar"
138 (uri=? (string->uri "ftp://foo/bar")
143 (pass-if "ftp://foo@bar:22/baz"
144 (uri=? (string->uri "ftp://foo@bar:22/baz")
151 (pass-if "http://bad.host.1"
152 (not (string->uri "http://bad.host.1")))
154 (pass-if "http://1.good.host"
155 (uri=? (string->uri "http://1.good.host")
156 #:scheme 'http #:host "1.good.host" #:path ""))
158 (pass-if "http://192.0.2.1"
159 (uri=? (string->uri "http://192.0.2.1")
160 #:scheme 'http #:host "192.0.2.1" #:path ""))
162 (pass-if "http://[2001:db8::1]"
163 (uri=? (string->uri "http://[2001:db8::1]")
164 #:scheme 'http #:host "2001:db8::1" #:path ""))
166 (pass-if "http://[2001:db8::1]:80"
167 (uri=? (string->uri "http://[2001:db8::1]:80")
173 (pass-if "http://[::ffff:192.0.2.1]"
174 (uri=? (string->uri "http://[::ffff:192.0.2.1]")
175 #:scheme 'http #:host "::ffff:192.0.2.1" #:path ""))
177 (pass-if "http://foo:"
178 (uri=? (string->uri "http://foo:")
179 #:scheme 'http #:host "foo" #:path ""))
181 (pass-if "http://foo:/"
182 (uri=? (string->uri "http://foo:/")
183 #:scheme 'http #:host "foo" #:path "/"))
185 (pass-if "http://2012.jsconf.us/"
186 (uri=? (string->uri "http://2012.jsconf.us/")
187 #:scheme 'http #:host "2012.jsconf.us" #:path "/"))
189 (pass-if "http://foo:not-a-port"
190 (not (string->uri "http://foo:not-a-port")))
192 (pass-if "http://:10"
193 (not (string->uri "http://:10")))
195 (pass-if "http://foo@"
196 (not (string->uri "http://foo@")))
199 (uri=? (string->uri "file:/")
203 (pass-if "file:/etc/hosts"
204 (uri=? (string->uri "file:/etc/hosts")
206 #:path "/etc/hosts"))
208 (pass-if "file:///etc/hosts"
209 (uri=? (string->uri "file:///etc/hosts")
211 #:path "/etc/hosts")))
213 (with-test-prefix "uri->string"
216 (uri->string (string->uri "ftp:"))))
220 (uri->string (string->uri "ftp:foo"))))
222 (pass-if "ftp://foo/bar"
223 (equal? "ftp://foo/bar"
224 (uri->string (string->uri "ftp://foo/bar"))))
226 (pass-if "ftp://foo@bar:22/baz"
227 (equal? "ftp://foo@bar:22/baz"
228 (uri->string (string->uri "ftp://foo@bar:22/baz"))))
230 (pass-if "http://192.0.2.1"
231 (equal? "http://192.0.2.1"
232 (uri->string (string->uri "http://192.0.2.1"))))
234 (pass-if "http://[2001:db8::1]"
235 (equal? "http://[2001:db8::1]"
236 (uri->string (string->uri "http://[2001:db8::1]"))))
238 (pass-if "http://[::ffff:192.0.2.1]"
239 (equal? "http://[::ffff:192.0.2.1]"
240 (uri->string (string->uri "http://[::ffff:192.0.2.1]"))))
242 (pass-if "http://foo:"
244 (uri->string (string->uri "http://foo:"))))
246 (pass-if "http://foo:/"
247 (equal? "http://foo/"
248 (uri->string (string->uri "http://foo:/")))))
250 (with-test-prefix "decode"
252 (equal? "foo bar" (uri-decode "foo%20bar")))
255 (equal? "foo bar" (uri-decode "foo+bar"))))
257 (with-test-prefix "encode"
258 (pass-if (equal? "foo%20bar" (uri-encode "foo bar"))))