# 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,v 1.5 2000/09/07 06:09:51 welch Exp $

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

# 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
}

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-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-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-4.1 {uri::geturl} {
    set token [uri::geturl file://[file join [pwd] ${::dirname} pkgIndex.tcl]]
    # Add one because the pkgIndex.tcl file has a trailing newline, which is
    # not included in the data field (I think)
    expr {[string length [lindex [array get $token data] 1]]+1}
} [file size [file join [pwd] $::dirname pkgIndex.tcl]]

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

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

::tcltest::cleanupTests
return
