# Tests for the uri module.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2000 by Zveno Pty Ltd.
#
# RCS: @(#) $Id: uri.test 380544 2003-04-11 21:01:29Z jigen $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}
set dirname [file dirname [info script]]
source      [file join $dirname uri.tcl]
package require uri

puts "uri [package present uri]"

# Take a key-value list and return list sorted by key, 
# but with corresponding values staying with their key
proc kvsort args {
    array set arr $args
    set result {}
    foreach key [lsort [array names arr]] {
	lappend result $key $arr($key)
    }
    return $result
}

# -------------------------------------------------------------------------
# Split tests

test uri-1.1 {uri::split - http w/- query} {
    eval kvsort [uri::split http://test.net/path/path2?query]
} {host test.net path path/path2 port {} query query scheme http}

test uri-1.2 {uri::split - https w/- query} {
    eval kvsort [uri::split https://test.net/path/path2?query]
} {host test.net path path/path2 port {} query query scheme https}

test uri-1.3 {uri::split - http w/- port} {
    eval kvsort [uri::split http://test.net:8080]
} {host test.net path {} port 8080 query {} scheme http}

test uri-1.4 {uri::split - https w/- port} {
    eval kvsort [uri::split https://test.net:8888]
} {host test.net path {} port 8888 query {} scheme https}

test uri-1.5 {uri::split - ftp} {
    eval kvsort [uri::split ftp://ftp.test.net/path/to/resource]
} {host ftp.test.net path path/to/resource port {} pwd {} scheme ftp type {} user {}}

test uri-1.6 {uri::split - ftp with userinfo} {
    eval kvsort [uri::split {ftp://user:passwd@localhost/a/b/c.d}]
} {host localhost path a/b/c.d port {} pwd passwd scheme ftp type {} user user}

test uri-1.7 {uri::split - ftp with type} {
    eval kvsort [uri::split {ftp://localhost/a/b/c.d;type=i}]
} {host localhost path a/b/c.d port {} pwd {} scheme ftp type i user {}}

test uri-1.8 {uri::split - ftp with port} {
    eval kvsort [uri::split {ftp://localhost:21/a/b/c.d}]
} {host localhost path a/b/c.d port 21 pwd {} scheme ftp type {} user {}}

test uri-1.9 {uri::split - news with message-id} {
    eval kvsort [uri::split {news:87lm40t3v7.fsf@dedasys.com}]
} {message-id 87lm40t3v7.fsf@dedasys.com scheme news}

test uri-1.10 {uri::split - news with newsgroup-name} {
    eval kvsort [uri::split {news:comp.lang.tcl}]
} {newsgroup-name comp.lang.tcl scheme news}

# -------------------------------------------------------------------------

test uri-2.1 {uri::join - http} {
    uri::join scheme http path / host www.w3.org
} http://www.w3.org/

test uri-2.2 {uri::join - https} {
    uri::join scheme https path / host www.w3.org
} https://www.w3.org/

test uri-2.3 {uri::join - http w/- query} {
    uri::join scheme http query abc=def&ghi=jkl host www.test.net path /path/
} http://www.test.net/path/?abc=def&ghi=jkl

test uri-2.4 {uri::join - https w/- query} {
    uri::join scheme https query abc=def&ghi=jkl host www.test.net path /path/
} https://www.test.net/path/?abc=def&ghi=jkl

test uri-2.5 {uri::join - http w/- port} {
    uri::join scheme http port 8080 host www.test.net path /path/
} http://www.test.net:8080/path/

test uri-2.6 {uri::join - https w/- port} {
    uri::join scheme https port 8888 host www.test.net path /path/
} https://www.test.net:8888/path/

test uri-2.7 {uri::join - ftp} {
    uri::join host ftp.test.net path /my/file scheme ftp
} ftp://ftp.test.net/my/file

test uri-2.8 {uri::join - identity function} {
    eval uri::join [uri::split http://www.w3.org/XML/?abc=def]
} http://www.w3.org/XML/?abc=def

test uri-2.9 {uri::join - ftp userinfo check} {
    eval uri::join scheme ftp host localhost port 21 path /filename user user pwd passwd
} {ftp://user:passwd@localhost:21/filename}

test uri-2.10 {uri::join - ftp userinfo check with no passwd} {
    eval uri::join scheme ftp host localhost path /filename user user
} {ftp://user@localhost/filename}

test uri-2.11 {uri::join - ftp path prefix} {
    eval uri::join scheme ftp host localhost path a/b/c.d
} ftp://localhost/a/b/c.d

test uri-2.12 {uri::join - ftp w/- image type} {
    eval uri::join scheme ftp host localhost path a/b/c.d type i
} {ftp://localhost/a/b/c.d;type=i}

test uri-2.13 {uri::join - ftp w/- ascii type} {
    eval uri::join scheme ftp host localhost path a/b/c.d type a
} {ftp://localhost/a/b/c.d;type=a}

# I am not sure that this shouldn't produce an error. The semi-colon is 
# reserved so in this case with an invalid suffix the semi-colon should
# probably be quoted. [PT]
test uri-2.14 {uri::join - ftp w/- invalid type} {
    eval uri::join scheme ftp host localhost path a/b/c.d type X
} {ftp://localhost/a/b/c.d;type=X}

test uri-2.15 {uri::join - news message-id} {
    eval uri::join scheme news message-id 87lm40t3v7.fsf@dedasys.com
} {news:87lm40t3v7.fsf@dedasys.com}

test uri-2.16 {uri::join - news newsgroup-name} {
    eval uri::join scheme news newsgroup-name comp.lang.tcl
} {news:comp.lang.tcl}


# -------------------------------------------------------------------------

test uri-3.1 {uri::resolve - relative URL, base trailing slash} {
    uri::resolve http://www.w3.org/path/ test.html
} http://www.w3.org/path/test.html

test uri-3.2 {uri::resolve - relative URL path, base trailing slash} {
    uri::resolve http://www.w3.org/path/ relpath/test.html
} http://www.w3.org/path/relpath/test.html

test uri-3.3 {uri::resolve - relative URL, base no trailing slash} {
    uri::resolve http://www.w3.org/path test.html
} http://www.w3.org/test.html

test uri-3.4 {uri::resolve - relative URL path, base no trailing slash} {
    uri::resolve http://www.w3.org/path relpath/test.html
} http://www.w3.org/relpath/test.html

test uri-3.5 {uri::resolve - relative URL w/- query} {
    uri::resolve http://www.w3.org/path/ test.html?abc=def
} http://www.w3.org/path/test.html?abc=def

test uri-3.6 {uri::resolve - absolute URL} {
    uri::resolve http://www.w3.org/path/ http://test.net/test.html
} http://test.net/test.html

test uri-3.7 {uri::resolve - two queries - one sans path} {
    uri::resolve http://www.example.com/foo/bar.rvt?foo=bar ?shoo=bee
} http://www.example.com/foo/bar.rvt?shoo=bee

test uri-3.8 {uri::resolve - two queries} {
    uri::resolve http://www.example.com/baz/?foo=bar ?shoo=bee
} http://www.example.com/baz/?shoo=bee

test uri-3.9 {uri::resolve - two absolute URL's with queries} {
    uri::resolve http://www.example.com/?foo=bar http://www.example.com/?shoo=bee
} http://www.example.com/?shoo=bee

test uri-3.10 {uri::resolve - two queries,
    one absolute URL, one absolute path} {
    uri::resolve http://www.example.com/baz?foo=bar /baz?shoo=bee
} http://www.example.com/baz?shoo=bee


# -------------------------------------------------------------------------

test uri-4.1 {uri::geturl} {
    removeFile __testdata
    set data [info commands]
    set file [makeFile {} __testdata]
    set f [open $file w]
    puts -nonewline $f $data
    close $f

    set token [uri::geturl file://$file]
    string compare $data [set [subst $token](data)]
} 0

# -------------------------------------------------------------------------

test uri-5.1-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/path1/./remove/../path2/resource
} http://www.test.net/path1/path2/resource

test uri-5.2-0 {uri::canonicalize infinite loop} {
    uri::canonicalize http://www.test.net/../path2/resource
} {http://www.test.net/path2/resource}

test uri-5.3-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./path1/./remove/../path2/../resource
} http://www.test.net/path1/resource

test uri-5.4-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./././path1/./remove/../path2/../resource
} http://www.test.net/path1/resource

test uri-5.5-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./././path1/./remove/path2/../../resource
} http://www.test.net/path1/resource

test uri-5.6-0 {uri::canonicalize infinite loop} {
    uri::canonicalize http://www.test.net/../../../path2/resource
} {http://www.test.net/path2/resource}

test uri-5.7-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/path1/./remove/../path.html/resource
} http://www.test.net/path1/path.html/resource

test uri-5.8-0 {uri::canonicalize infinite loop} {
    uri::canonicalize http://www.test.net/../path.html/resource
} {http://www.test.net/path.html/resource}

test uri-5.9-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./path1/./remove/../path.html/../resource
} http://www.test.net/path1/resource

test uri-5.10-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./././path1/./remove/../path.html/../resource
} http://www.test.net/path1/resource

test uri-5.11-0 {uri::canonicalize} {
    uri::canonicalize http://www.test.net/./././path1/./remove/path.html/../../resource
} http://www.test.net/path1/resource

test uri-5.12-0 {uri::canonicalize infinite loop} {
    uri::canonicalize http://www.test.net/../../../path.html/resource
} {http://www.test.net/path.html/resource}

test uri-5.13-0 {uri::canonicalize} {
    uri::canonicalize http://www.eldritchpress.org/jc/../help.html
} {http://www.eldritchpress.org/help.html}

test uri-5.14-0 {uri::canonicalize trailing ..} {
    uri::canonicalize http://www.example.com/foo/bar/..
} {http://www.example.com/foo/}

test uri-5.14-0 {uri::canonicalize trailing ..} {
    uri::canonicalize http://www.example.com/..
} {http://www.example.com/}

test uri-5.1-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/path1/./remove/../path2/resource
} ftp://ftp.test.net/path1/path2/resource

test uri-5.2-1 {uri::canonicalize infinite loop} {
    uri::canonicalize ftp://ftp.test.net/../path2/resource
} {ftp://ftp.test.net/path2/resource}

test uri-5.3-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./path1/./remove/../path2/../resource
} ftp://ftp.test.net/path1/resource

test uri-5.4-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./././path1/./remove/../path2/../resource
} ftp://ftp.test.net/path1/resource

test uri-5.5-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./././path1/./remove/path2/../../resource
} ftp://ftp.test.net/path1/resource

test uri-5.6-1 {uri::canonicalize infinite loop} {
    uri::canonicalize ftp://ftp.test.net/../../../path2/resource
} {ftp://ftp.test.net/path2/resource}

test uri-5.7-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/path1/./remove/../path.html/resource
} ftp://ftp.test.net/path1/path.html/resource

test uri-5.8-1 {uri::canonicalize infinite loop} {
    uri::canonicalize ftp://ftp.test.net/../path.html/resource
} {ftp://ftp.test.net/path.html/resource}

test uri-5.9-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./path1/./remove/../path.html/../resource
} ftp://ftp.test.net/path1/resource

test uri-5.10-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./././path1/./remove/../path.html/../resource
} ftp://ftp.test.net/path1/resource

test uri-5.11-1 {uri::canonicalize} {
    uri::canonicalize ftp://ftp.test.net/./././path1/./remove/path.html/../../resource
} ftp://ftp.test.net/path1/resource

test uri-5.12-1 {uri::canonicalize infinite loop} {
    uri::canonicalize ftp://ftp.test.net/../../../path.html/resource
} {ftp://ftp.test.net/path.html/resource}

test uri-5.1-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/path1/./remove/../path2/resource
} file://goo.test.net/path1/path2/resource

test uri-5.2-2 {uri::canonicalize infinite loop} {
    uri::canonicalize file://goo.test.net/../path2/resource
} {file://goo.test.net/path2/resource}

test uri-5.3-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./path1/./remove/../path2/../resource
} file://goo.test.net/path1/resource

test uri-5.4-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./././path1/./remove/../path2/../resource
} file://goo.test.net/path1/resource

test uri-5.5-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./././path1/./remove/path2/../../resource
} file://goo.test.net/path1/resource

test uri-5.6-2 {uri::canonicalize infinite loop} {
    uri::canonicalize file://goo.test.net/../../../path2/resource
} {file://goo.test.net/path2/resource}

test uri-5.7-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/path1/./remove/../path.html/resource
} file://goo.test.net/path1/path.html/resource

test uri-5.8-2 {uri::canonicalize infinite loop} {
    uri::canonicalize file://goo.test.net/../path.html/resource
} {file://goo.test.net/path.html/resource}

test uri-5.9-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./path1/./remove/../path.html/../resource
} file://goo.test.net/path1/resource

test uri-5.10-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./././path1/./remove/../path.html/../resource
} file://goo.test.net/path1/resource

test uri-5.11-2 {uri::canonicalize} {
    uri::canonicalize file://goo.test.net/./././path1/./remove/path.html/../../resource
} file://goo.test.net/path1/resource

test uri-5.12-2 {uri::canonicalize infinite loop} {
    uri::canonicalize file://goo.test.net/../../../path.html/resource
} {file://goo.test.net/path.html/resource}

test uri-5.1-3 {uri::canonicalize} {
    uri::canonicalize file:///path1/./remove/../path2/resource
} file:///path1/path2/resource

test uri-5.2-3 {uri::canonicalize infinite loop} {
    uri::canonicalize file:///../path2/resource
} {file:///path2/resource}

test uri-5.3-3 {uri::canonicalize} {
    uri::canonicalize file:///./path1/./remove/../path2/../resource
} file:///path1/resource

test uri-5.4-3 {uri::canonicalize} {
    uri::canonicalize file:///./././path1/./remove/../path2/../resource
} file:///path1/resource

test uri-5.5-3 {uri::canonicalize} {
    uri::canonicalize file:///./././path1/./remove/path2/../../resource
} file:///path1/resource

test uri-5.6-3 {uri::canonicalize infinite loop} {
    uri::canonicalize file:///../../../path2/resource
} {file:///path2/resource}

test uri-5.7-3 {uri::canonicalize} {
    uri::canonicalize file:///path1/./remove/../path.html/resource
} file:///path1/path.html/resource

test uri-5.8-3 {uri::canonicalize infinite loop} {
    uri::canonicalize file:///../path.html/resource
} {file:///path.html/resource}

test uri-5.9-3 {uri::canonicalize} {
    uri::canonicalize file:///./path1/./remove/../path.html/../resource
} file:///path1/resource

test uri-5.10-3 {uri::canonicalize} {
    uri::canonicalize file:///./././path1/./remove/../path.html/../resource
} file:///path1/resource

test uri-5.11-3 {uri::canonicalize} {
    uri::canonicalize file:///./././path1/./remove/path.html/../../resource
} file:///path1/resource

test uri-5.12-3 {uri::canonicalize infinite loop} {
    uri::canonicalize file:///../../../path.html/resource
} {file:///path.html/resource}

test uri-6.0 {uri::canonicalize} {
    uri::canonicalize telnet://goo.test.net/
} telnet://goo.test.net/

test uri-7.0 {uri::split & uri::join} {
    set ls [uri::split http://tcl.apache.org/websh/faq.ws3\#generic?foo=bar]
    eval uri::join $ls
} {http://tcl.apache.org/websh/faq.ws3#generic?foo=bar}

# -------------------------------------------------------------------------

test uri-8.0 {uri::split bug #676976, ill. char in scheme} {
    set ls [uri::split ht,tp://tcl.apache.org/websh]
    eval uri::join $ls
} {http:///ht,tp://tcl.apache.org/websh}

# -------------------------------------------------------------------------


::tcltest::cleanupTests
return

# -------------------------------------------------------------------------
# Local Variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End:
