Skip to content
Tcl Source Code Artifact [e34db5efa1]
Login

Artifact [e34db5efa1]

Artifact e34db5efa1fcd24bfb86775e1ab6652d04371bf9:


# Commands covered:  http::config, http::geturl, http::wait, http::reset
#
# This file contains a collection of tests for the http script library.
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# This file is a rewrite to demostrate TIP 452 (enhancement to tcltest for
# stubs/mocks and seams).  NOTE -- due to the use of stubs/mocks, no actual
# I/O is done, so the httpd package is no longer being used. Please also note,
# the seams feature is not used, only the stub/mock feature.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
# Copyright (c) 1998-2000 by Ajuba Solutions.
# Copyright (c) 2017 by Gerald W. Lester.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

package require tcltest 2.5

if {[catch {package require http 2} version]} {
    if {[info exists http2]} {
	catch {puts "Cannot load http 2.* package"}
	return
    } else {
	catch {puts "Running http 2.* tests in slave interp"}
	set interp [interp create http2]
	$interp eval [list set http2 "running"]
	$interp eval [list set argv $argv]
	$interp eval [list source [info script]]
	interp delete $interp
	return
    }
}

proc bgerror {args} {
    global errorInfo
    puts stderr "http.test bgerror"
    puts stderr [join $args]
    puts stderr $errorInfo
}

set bindata "This is binary data\x0d\x0amore\x0dmore\x0amore\x00null"
catch {unset data}


::tcltest::test http-1.1 {http::config} {
    http::config -useragent UserAgent
    http::config
} [list -accept */* -proxyfilter http::ProxyRequired -proxyhost {} -proxyport {} -urlencoding utf-8 -useragent "UserAgent"]
::tcltest::test http-1.2 {http::config} {
    http::config -proxyfilter
} http::ProxyRequired
::tcltest::test http-1.3 {http::config} {
    catch {http::config -junk}
} 1
::tcltest::test http-1.4 {http::config} {
    set savedconf [http::config]
    http::config -proxyhost nowhere.come -proxyport 8080 \
	-proxyfilter myFilter -useragent "Tcl Test Suite" \
	-urlencoding iso8859-1
    set x [http::config]
    http::config {*}$savedconf
    set x
} {-accept */* -proxyfilter myFilter -proxyhost nowhere.come -proxyport 8080 -urlencoding iso8859-1 -useragent {Tcl Test Suite}}
::tcltest::test http-1.5 {http::config} -returnCodes error -body {
    http::config -proxyhost {} -junk 8080
} -result {Unknown option -junk, must be: -accept, -proxyfilter, -proxyhost, -proxyport, -urlencoding, -useragent}
::tcltest::test http-1.6 {http::config} -setup {
    set oldenc [http::config -urlencoding]
} -body {
    set enc [list [http::config -urlencoding]]
    http::config -urlencoding iso8859-1
    lappend enc [http::config -urlencoding]
} -cleanup {
    http::config -urlencoding $oldenc
} -result {utf-8 iso8859-1}


##
## Happy path
##
::tcltest::test http-2.1 {http::reset} \
    -setup {
        array set ::http_1 {
            sock none
        }
        unset -nocomplain ::http_1(error)
        ::tcltest::testSetup {
            ::fileevent {
                * {
                    returns {Test error}
                    code {error}
                    errorcode {HTTP TEST RESET FILEEVENT}
                }
            }
            ::http::Finish {
                * {
                    returns {No Error}
                    errorcode {}
                    set {
                        ::http_1 A {
                            status {---}
                        }
                    }
                }
            }
        }
    } \
    -body {
        list [catch {::http::reset ::http_1}] [info exists ::http_1] [::tcltest::callCount]
    } \
    -result {0 1 {::fileevent,count 2 ::http::Finish,count 1}} \
    -cleanup {
        ::tcltest::testCleanup
    }
 
## Test error path
##
::tcltest::test http-2.2 {http::reset} \
    -setup {
        array set ::http_1 {
            sock none
        }
        unset -nocomplain ::http_1(error)
        ::tcltest::testSetup {
            ::fileevent {
                * {
                    returns {Test error}
                    code {error}
                    errorcode {HTTP TEST RESET FILEEVENT}
                }
            }
            ::http::Finish {
                * {
                    returns {Finish Error}
                    errorcode {}
                    set {
                        ::http_1 A {
                            status {---}
                            error {{HTTP TEST ERROR}}
                        }
                    }
                }
            }
        }
    } \
    -body {
        list [catch {::http::reset ::http_1} result] [info exists ::http_1] [::tcltest::callCount] $result
    } \
    -result {1 0 {::fileevent,count 2 ::http::Finish,count 1} {HTTP TEST ERROR}} \
    -cleanup {
        ::tcltest::testCleanup
    }
 

# cleanup
catch {unset url}
catch {unset badurl}
catch {unset port}
catch {unset data}

if {[info exists removeHttpd]} {
    ::tcltest::removeFile $httpdFile
}

rename bgerror {}
::tcltest::cleanupTests

# Local variables:
# mode: tcl
# End: