os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/http.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
# Commands covered:  http::config, http::geturl, http::wait, http::reset
sl@0
     2
#
sl@0
     3
# This file contains a collection of tests for the http script library.
sl@0
     4
# Sourcing this file into Tcl runs the tests and
sl@0
     5
# generates output for errors.  No output means no errors were found.
sl@0
     6
#
sl@0
     7
# Copyright (c) 1991-1993 The Regents of the University of California.
sl@0
     8
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
sl@0
     9
# Copyright (c) 1998-2000 by Ajuba Solutions.
sl@0
    10
#
sl@0
    11
# See the file "license.terms" for information on usage and redistribution
sl@0
    12
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    13
#
sl@0
    14
#
sl@0
    15
# RCS: @(#) $Id: http.test,v 1.33.2.6 2006/10/06 19:00:53 hobbs Exp $
sl@0
    16
sl@0
    17
if {[lsearch [namespace children] ::tcltest] == -1} {
sl@0
    18
    package require tcltest 2
sl@0
    19
    namespace import -force ::tcltest::*
sl@0
    20
}
sl@0
    21
sl@0
    22
if {[catch {package require http 2} version]} {
sl@0
    23
    if {[info exists http2]} {
sl@0
    24
	catch {puts "Cannot load http 2.* package"}
sl@0
    25
	return
sl@0
    26
    } else {
sl@0
    27
	catch {puts "Running http 2.* tests in slave interp"}
sl@0
    28
	set interp [interp create http2]
sl@0
    29
	$interp eval [list set http2 "running"]
sl@0
    30
	$interp eval [list set argv $argv]
sl@0
    31
	$interp eval [list source [info script]]
sl@0
    32
	interp delete $interp
sl@0
    33
	return
sl@0
    34
    }
sl@0
    35
}
sl@0
    36
sl@0
    37
proc bgerror {args} {
sl@0
    38
    global errorInfo
sl@0
    39
    puts stderr "http.test bgerror"
sl@0
    40
    puts stderr [join $args]
sl@0
    41
    puts stderr $errorInfo
sl@0
    42
}
sl@0
    43
sl@0
    44
set port 8010
sl@0
    45
set bindata "This is binary data\x0d\x0amore\x0dmore\x0amore\x00null"
sl@0
    46
catch {unset data}
sl@0
    47
sl@0
    48
# Ensure httpd file exists
sl@0
    49
sl@0
    50
set origFile [file join [pwd] [file dirname [info script]] httpd]
sl@0
    51
set httpdFile [file join [temporaryDirectory] httpd_[pid]]
sl@0
    52
if {![file exists $httpdFile]} {
sl@0
    53
    makeFile "" $httpdFile
sl@0
    54
    file delete $httpdFile
sl@0
    55
    file copy $origFile $httpdFile
sl@0
    56
    set removeHttpd 1
sl@0
    57
}
sl@0
    58
sl@0
    59
if {[info commands testthread] == "testthread" && [file exists $httpdFile]} {
sl@0
    60
    set httpthread [testthread create "
sl@0
    61
	source [list $httpdFile]
sl@0
    62
	testthread wait
sl@0
    63
    "]
sl@0
    64
    testthread send $httpthread [list set port $port]
sl@0
    65
    testthread send $httpthread [list set bindata $bindata]
sl@0
    66
    testthread send $httpthread {httpd_init $port}
sl@0
    67
    puts "Running httpd in thread $httpthread"
sl@0
    68
} else {
sl@0
    69
    if {![file exists $httpdFile]} {
sl@0
    70
	puts "Cannot read $httpdFile script, http test skipped"
sl@0
    71
	unset port
sl@0
    72
	return
sl@0
    73
    }
sl@0
    74
    source $httpdFile
sl@0
    75
    # Let the OS pick the port; that's much more flexible
sl@0
    76
    if {[catch {httpd_init 0} listen]} {
sl@0
    77
	puts "Cannot start http server, http test skipped"
sl@0
    78
	unset port
sl@0
    79
	return
sl@0
    80
    } else {
sl@0
    81
	set port [lindex [fconfigure $listen -sockname] 2]
sl@0
    82
    }
sl@0
    83
}
sl@0
    84
sl@0
    85
test http-1.1 {http::config} {
sl@0
    86
    http::config
sl@0
    87
} [list -accept */* -proxyfilter http::ProxyRequired -proxyhost {} -proxyport {} -urlencoding utf-8 -useragent "Tcl http client package $version"]
sl@0
    88
test http-1.2 {http::config} {
sl@0
    89
    http::config -proxyfilter
sl@0
    90
} http::ProxyRequired
sl@0
    91
test http-1.3 {http::config} {
sl@0
    92
    catch {http::config -junk}
sl@0
    93
} 1
sl@0
    94
test http-1.4 {http::config} {
sl@0
    95
    set savedconf [http::config]
sl@0
    96
    http::config -proxyhost nowhere.come -proxyport 8080 \
sl@0
    97
	-proxyfilter myFilter -useragent "Tcl Test Suite" \
sl@0
    98
	-urlencoding iso8859-1
sl@0
    99
    set x [http::config]
sl@0
   100
    eval http::config $savedconf
sl@0
   101
    set x
sl@0
   102
} {-accept */* -proxyfilter myFilter -proxyhost nowhere.come -proxyport 8080 -urlencoding iso8859-1 -useragent {Tcl Test Suite}}
sl@0
   103
test http-1.5 {http::config} {
sl@0
   104
    list [catch {http::config -proxyhost {} -junk 8080} msg] $msg
sl@0
   105
} {1 {Unknown option -junk, must be: -accept, -proxyfilter, -proxyhost, -proxyport, -urlencoding, -useragent}}
sl@0
   106
test http-1.6 {http::config} {
sl@0
   107
    set enc [list [http::config -urlencoding]]
sl@0
   108
    http::config -urlencoding iso8859-1
sl@0
   109
    lappend enc [http::config -urlencoding]
sl@0
   110
    http::config -urlencoding [lindex $enc 0]
sl@0
   111
    set enc
sl@0
   112
} {utf-8 iso8859-1}
sl@0
   113
sl@0
   114
test http-2.1 {http::reset} {
sl@0
   115
    catch {http::reset http#1}
sl@0
   116
} 0
sl@0
   117
sl@0
   118
test http-3.1 {http::geturl} {
sl@0
   119
    list [catch {http::geturl -bogus flag} msg] $msg
sl@0
   120
} {1 {Unknown option flag, can be: -binary, -blocksize, -channel, -command, -handler, -headers, -progress, -query, -queryblocksize, -querychannel, -queryprogress, -validate, -timeout, -type}}
sl@0
   121
test http-3.2 {http::geturl} {
sl@0
   122
    catch {http::geturl http:junk} err
sl@0
   123
    set err
sl@0
   124
} {Unsupported URL: http:junk}
sl@0
   125
set url //[info hostname]:$port
sl@0
   126
set badurl //[info hostname]:6666
sl@0
   127
test http-3.3 {http::geturl} {
sl@0
   128
    set token [http::geturl $url]
sl@0
   129
    http::data $token
sl@0
   130
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   131
<h1>Hello, World!</h1>
sl@0
   132
<h2>GET /</h2>
sl@0
   133
</body></html>"
sl@0
   134
set tail /a/b/c
sl@0
   135
set url //[info hostname]:$port/a/b/c
sl@0
   136
set fullurl http://user:pass@[info hostname]:$port/a/b/c
sl@0
   137
set binurl //[info hostname]:$port/binary
sl@0
   138
set posturl //[info hostname]:$port/post
sl@0
   139
set badposturl //[info hostname]:$port/droppost
sl@0
   140
set badcharurl //%user@[info hostname]:$port/a/^b/c
sl@0
   141
sl@0
   142
test http-3.4 {http::geturl} {
sl@0
   143
    set token [http::geturl $url]
sl@0
   144
    http::data $token
sl@0
   145
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   146
<h1>Hello, World!</h1>
sl@0
   147
<h2>GET $tail</h2>
sl@0
   148
</body></html>"
sl@0
   149
proc selfproxy {host} {
sl@0
   150
    global port
sl@0
   151
    return [list [info hostname] $port]
sl@0
   152
}
sl@0
   153
test http-3.5 {http::geturl} {
sl@0
   154
    http::config -proxyfilter selfproxy
sl@0
   155
    set token [http::geturl $url]
sl@0
   156
    http::config -proxyfilter http::ProxyRequired
sl@0
   157
    http::data $token
sl@0
   158
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   159
<h1>Hello, World!</h1>
sl@0
   160
<h2>GET http:$url</h2>
sl@0
   161
</body></html>"
sl@0
   162
test http-3.6 {http::geturl} {
sl@0
   163
    http::config -proxyfilter bogus
sl@0
   164
    set token [http::geturl $url]
sl@0
   165
    http::config -proxyfilter http::ProxyRequired
sl@0
   166
    http::data $token
sl@0
   167
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   168
<h1>Hello, World!</h1>
sl@0
   169
<h2>GET $tail</h2>
sl@0
   170
</body></html>"
sl@0
   171
test http-3.7 {http::geturl} {
sl@0
   172
    set token [http::geturl $url -headers {Pragma no-cache}]
sl@0
   173
    http::data $token
sl@0
   174
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   175
<h1>Hello, World!</h1>
sl@0
   176
<h2>GET $tail</h2>
sl@0
   177
</body></html>"
sl@0
   178
test http-3.8 {http::geturl} {
sl@0
   179
    set token [http::geturl $url -query Name=Value&Foo=Bar -timeout 2000]
sl@0
   180
    http::data $token
sl@0
   181
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   182
<h1>Hello, World!</h1>
sl@0
   183
<h2>POST $tail</h2>
sl@0
   184
<h2>Query</h2>
sl@0
   185
<dl>
sl@0
   186
<dt>Name<dd>Value
sl@0
   187
<dt>Foo<dd>Bar
sl@0
   188
</dl>
sl@0
   189
</body></html>"
sl@0
   190
test http-3.9 {http::geturl} {
sl@0
   191
    set token [http::geturl $url -validate 1]
sl@0
   192
    http::code $token
sl@0
   193
} "HTTP/1.0 200 OK"
sl@0
   194
test http-3.10 {http::geturl queryprogress} {
sl@0
   195
    set query foo=bar
sl@0
   196
    set sep ""
sl@0
   197
    set i 0
sl@0
   198
    # Create about 120K of query data
sl@0
   199
    while {$i < 14} {
sl@0
   200
	incr i
sl@0
   201
	append query $sep$query
sl@0
   202
	set sep &
sl@0
   203
    }
sl@0
   204
sl@0
   205
    proc postProgress {token x y} {
sl@0
   206
	global postProgress
sl@0
   207
	lappend postProgress $y
sl@0
   208
    }
sl@0
   209
    set postProgress {}
sl@0
   210
    set t [http::geturl $posturl -query $query \
sl@0
   211
	    -queryprogress postProgress -queryblocksize 16384]
sl@0
   212
    http::wait $t
sl@0
   213
    list [http::status $t] [string length $query] $postProgress [http::data $t]
sl@0
   214
} {ok 122879 {16384 32768 49152 65536 81920 98304 114688 122879} {Got 122879 bytes}}
sl@0
   215
test http-3.11 {http::geturl querychannel with -command} {
sl@0
   216
    set query foo=bar
sl@0
   217
    set sep ""
sl@0
   218
    set i 0
sl@0
   219
    # Create about 120K of query data
sl@0
   220
    while {$i < 14} {
sl@0
   221
	incr i
sl@0
   222
	append query $sep$query
sl@0
   223
	set sep &
sl@0
   224
    }
sl@0
   225
    set file [makeFile $query outdata]
sl@0
   226
    set fp [open $file]
sl@0
   227
sl@0
   228
    proc asyncCB {token} {
sl@0
   229
	global postResult
sl@0
   230
	lappend postResult [http::data $token]
sl@0
   231
    }
sl@0
   232
    set postResult [list ]
sl@0
   233
    set t [http::geturl $posturl -querychannel $fp]
sl@0
   234
    http::wait $t
sl@0
   235
    set testRes [list [http::status $t] [string length $query] [http::data $t]]
sl@0
   236
sl@0
   237
    # Now do async
sl@0
   238
    http::cleanup $t
sl@0
   239
    close $fp
sl@0
   240
    set fp [open $file]
sl@0
   241
    set t [http::geturl $posturl -querychannel $fp -command asyncCB]
sl@0
   242
    set postResult [list PostStart]
sl@0
   243
    http::wait $t
sl@0
   244
    close $fp
sl@0
   245
sl@0
   246
    lappend testRes [http::status $t] $postResult
sl@0
   247
    removeFile outdata
sl@0
   248
    set testRes
sl@0
   249
} {ok 122879 {Got 122880 bytes} ok {PostStart {Got 122880 bytes}}}
sl@0
   250
# On Linux platforms when the client and server are on the same host, the
sl@0
   251
# client is unable to read the server's response one it hits the write error.
sl@0
   252
# The status is "eof".
sl@0
   253
# On Windows, the http::wait procedure gets a "connection reset by peer" error
sl@0
   254
# while reading the reply.
sl@0
   255
test http-3.12 {http::geturl querychannel with aborted request} {nonPortable} {
sl@0
   256
    set query foo=bar
sl@0
   257
    set sep ""
sl@0
   258
    set i 0
sl@0
   259
    # Create about 120K of query data
sl@0
   260
    while {$i < 14} {
sl@0
   261
	incr i
sl@0
   262
	append query $sep$query
sl@0
   263
	set sep &
sl@0
   264
    }
sl@0
   265
    set file [makeFile $query outdata]
sl@0
   266
    set fp [open $file]
sl@0
   267
sl@0
   268
    proc asyncCB {token} {
sl@0
   269
	global postResult
sl@0
   270
	lappend postResult [http::data $token]
sl@0
   271
    }
sl@0
   272
    proc postProgress {token x y} {
sl@0
   273
	global postProgress
sl@0
   274
	lappend postProgress $y
sl@0
   275
    }
sl@0
   276
    set postProgress {}
sl@0
   277
    # Now do async
sl@0
   278
    set postResult [list PostStart]
sl@0
   279
    if {[catch {
sl@0
   280
	set t [http::geturl $badposturl -querychannel $fp -command asyncCB \
sl@0
   281
		-queryprogress postProgress]
sl@0
   282
	http::wait $t
sl@0
   283
	upvar #0 $t state
sl@0
   284
    } err]} {
sl@0
   285
	puts $errorInfo
sl@0
   286
	error $err
sl@0
   287
    }
sl@0
   288
sl@0
   289
    removeFile outdata
sl@0
   290
    list [http::status $t] [http::code $t]
sl@0
   291
} {ok {HTTP/1.0 200 Data follows}}
sl@0
   292
test http-3.13 {http::geturl socket leak test} {
sl@0
   293
    set chanCount [llength [file channels]]
sl@0
   294
    for {set i 0} {$i < 3} {incr i} {
sl@0
   295
	catch {http::geturl $badurl -timeout 5000}
sl@0
   296
    }
sl@0
   297
sl@0
   298
    # No extra channels should be taken
sl@0
   299
    expr {[llength [file channels]] == $chanCount}
sl@0
   300
} 1
sl@0
   301
test http-3.14 "http::geturl $fullurl" {
sl@0
   302
    set token [http::geturl $fullurl -validate 1]
sl@0
   303
    http::code $token
sl@0
   304
} "HTTP/1.0 200 OK"
sl@0
   305
test http-3.15 {http::geturl parse failures} -body {
sl@0
   306
    http::geturl "{invalid}:url"
sl@0
   307
} -returnCodes error -result {Unsupported URL: {invalid}:url}
sl@0
   308
test http-3.16 {http::geturl parse failures} -body {
sl@0
   309
    http::geturl http:relative/url
sl@0
   310
} -returnCodes error -result {Unsupported URL: http:relative/url}
sl@0
   311
test http-3.17 {http::geturl parse failures} -body {
sl@0
   312
    http::geturl /absolute/url
sl@0
   313
} -returnCodes error -result {Missing host part: /absolute/url}
sl@0
   314
test http-3.18 {http::geturl parse failures} -body {
sl@0
   315
    http::geturl http://somewhere:123456789/
sl@0
   316
} -returnCodes error -result {Invalid port number: 123456789}
sl@0
   317
test http-3.19 {http::geturl parse failures} -body {
sl@0
   318
    set ::http::strict 1
sl@0
   319
    http::geturl http://{user}@somewhere
sl@0
   320
} -returnCodes error -result {Illegal characters in URL user}
sl@0
   321
test http-3.20 {http::geturl parse failures} -body {
sl@0
   322
    set ::http::strict 1
sl@0
   323
    http::geturl http://%user@somewhere
sl@0
   324
} -returnCodes error -result {Illegal encoding character usage "%us" in URL user}
sl@0
   325
test http-3.21 {http::geturl parse failures} -body {
sl@0
   326
    set ::http::strict 1
sl@0
   327
    http::geturl http://somewhere/{path}
sl@0
   328
} -returnCodes error -result {Illegal characters in URL path}
sl@0
   329
test http-3.22 {http::geturl parse failures} -body {
sl@0
   330
    set ::http::strict 1
sl@0
   331
    http::geturl http://somewhere/%path
sl@0
   332
} -returnCodes error -result {Illegal encoding character usage "%pa" in URL path}
sl@0
   333
test http-3.23 {http::geturl parse failures} -body {
sl@0
   334
    set ::http::strict 1
sl@0
   335
    http::geturl http://somewhere/path?{query}
sl@0
   336
} -returnCodes error -result {Illegal characters in URL path}
sl@0
   337
test http-3.24 {http::geturl parse failures} -body {
sl@0
   338
    set ::http::strict 1
sl@0
   339
    http::geturl http://somewhere/path?%query
sl@0
   340
} -returnCodes error -result {Illegal encoding character usage "%qu" in URL path}
sl@0
   341
test http-3.25 {http::geturl parse failures} -body {
sl@0
   342
    set ::http::strict 0
sl@0
   343
    set token [http::geturl $badcharurl]
sl@0
   344
    http::cleanup $token
sl@0
   345
} -returnCodes ok -result {}
sl@0
   346
sl@0
   347
test http-4.1 {http::Event} {
sl@0
   348
    set token [http::geturl $url]
sl@0
   349
    upvar #0 $token data
sl@0
   350
    array set meta $data(meta)
sl@0
   351
    expr ($data(totalsize) == $meta(Content-Length))
sl@0
   352
} 1
sl@0
   353
test http-4.2 {http::Event} {
sl@0
   354
    set token [http::geturl $url]
sl@0
   355
    upvar #0 $token data
sl@0
   356
    array set meta $data(meta)
sl@0
   357
    string compare $data(type) [string trim $meta(Content-Type)]
sl@0
   358
} 0
sl@0
   359
test http-4.3 {http::Event} {
sl@0
   360
    set token [http::geturl $url]
sl@0
   361
    http::code $token
sl@0
   362
} {HTTP/1.0 200 Data follows}
sl@0
   363
test http-4.4 {http::Event} {
sl@0
   364
    set testfile [makeFile "" testfile]
sl@0
   365
    set out [open $testfile w]
sl@0
   366
    set token [http::geturl $url -channel $out]
sl@0
   367
    close $out
sl@0
   368
    set in [open $testfile]
sl@0
   369
    set x [read $in]
sl@0
   370
    close $in
sl@0
   371
    removeFile $testfile
sl@0
   372
    set x
sl@0
   373
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   374
<h1>Hello, World!</h1>
sl@0
   375
<h2>GET $tail</h2>
sl@0
   376
</body></html>"
sl@0
   377
test http-4.5 {http::Event} {
sl@0
   378
    set testfile [makeFile "" testfile]
sl@0
   379
    set out [open $testfile w]
sl@0
   380
    set token [http::geturl $url -channel $out]
sl@0
   381
    close $out
sl@0
   382
    upvar #0 $token data
sl@0
   383
    removeFile $testfile
sl@0
   384
    expr $data(currentsize) == $data(totalsize)
sl@0
   385
} 1
sl@0
   386
test http-4.6 {http::Event} {
sl@0
   387
    set testfile [makeFile "" testfile]
sl@0
   388
    set out [open $testfile w]
sl@0
   389
    set token [http::geturl $binurl -channel $out]
sl@0
   390
    close $out
sl@0
   391
    set in [open $testfile]
sl@0
   392
    fconfigure $in -translation binary
sl@0
   393
    set x [read $in]
sl@0
   394
    close $in
sl@0
   395
    removeFile $testfile
sl@0
   396
    set x
sl@0
   397
} "$bindata[string trimleft $binurl /]"
sl@0
   398
proc myProgress {token total current} {
sl@0
   399
    global progress httpLog
sl@0
   400
    if {[info exists httpLog] && $httpLog} {
sl@0
   401
	puts "progress $total $current"
sl@0
   402
    }
sl@0
   403
    set progress [list $total $current]
sl@0
   404
}
sl@0
   405
if 0 {
sl@0
   406
    # This test hangs on Windows95 because the client never gets EOF
sl@0
   407
    set httpLog 1
sl@0
   408
    test http-4.6.1 {http::Event} knownBug {
sl@0
   409
	set token [http::geturl $url -blocksize 50 -progress myProgress]
sl@0
   410
	set progress
sl@0
   411
    } {111 111}
sl@0
   412
}
sl@0
   413
test http-4.7 {http::Event} {
sl@0
   414
    set token [http::geturl $url -progress myProgress]
sl@0
   415
    set progress
sl@0
   416
} {111 111}
sl@0
   417
test http-4.8 {http::Event} {
sl@0
   418
    set token [http::geturl $url]
sl@0
   419
    http::status $token
sl@0
   420
} {ok}
sl@0
   421
test http-4.9 {http::Event} {
sl@0
   422
    set token [http::geturl $url -progress myProgress]
sl@0
   423
    http::code $token
sl@0
   424
} {HTTP/1.0 200 Data follows}
sl@0
   425
test http-4.10 {http::Event} {
sl@0
   426
    set token [http::geturl $url -progress myProgress]
sl@0
   427
    http::size $token
sl@0
   428
} {111}
sl@0
   429
# Timeout cases
sl@0
   430
#	Short timeout to working server (the test server). This lets us try a
sl@0
   431
#	reset during the connection.
sl@0
   432
test http-4.11 {http::Event} {
sl@0
   433
    set token [http::geturl $url -timeout 1 -command {#}]
sl@0
   434
    http::reset $token
sl@0
   435
    http::status $token
sl@0
   436
} {reset}
sl@0
   437
#	Longer timeout with reset.
sl@0
   438
test http-4.12 {http::Event} {
sl@0
   439
    set token [http::geturl $url/?timeout=10 -command {#}]
sl@0
   440
    http::reset $token
sl@0
   441
    http::status $token
sl@0
   442
} {reset}
sl@0
   443
#	Medium timeout to working server that waits even longer. The timeout
sl@0
   444
#	hits while waiting for a reply.
sl@0
   445
test http-4.13 {http::Event} {
sl@0
   446
    set token [http::geturl $url?timeout=30 -timeout 10 -command {#}]
sl@0
   447
    http::wait $token
sl@0
   448
    http::status $token
sl@0
   449
} {timeout}
sl@0
   450
#	Longer timeout to good host, bad port, gets an error after the
sl@0
   451
#	connection "completes" but the socket is bad.
sl@0
   452
test http-4.14 {http::Event} {
sl@0
   453
    set code [catch {
sl@0
   454
	set token [http::geturl $badurl/?timeout=10 -timeout 10000 -command {#}]
sl@0
   455
	if {[string length $token] == 0} {
sl@0
   456
	    error "bogus return from http::geturl"
sl@0
   457
	}
sl@0
   458
	http::wait $token
sl@0
   459
	http::status $token
sl@0
   460
    } err]
sl@0
   461
    # error code varies among platforms.
sl@0
   462
    list $code [regexp {(connect failed|couldn't open socket)} $err]
sl@0
   463
} {1 1}
sl@0
   464
# Bogus host
sl@0
   465
test http-4.15 {http::Event} {
sl@0
   466
    # This test may fail if you use a proxy server.  That is to be
sl@0
   467
    # expected and is not a problem with Tcl.
sl@0
   468
    set code [catch {
sl@0
   469
	set token [http::geturl //not_a_host.tcl.tk -timeout 1000 -command {#}]
sl@0
   470
	http::wait $token
sl@0
   471
	http::status $token
sl@0
   472
    } err]
sl@0
   473
    # error code varies among platforms.
sl@0
   474
    list $code [string match "couldn't open socket*" $err]
sl@0
   475
} {1 1}
sl@0
   476
sl@0
   477
test http-5.1 {http::formatQuery} {
sl@0
   478
    http::formatQuery name1 value1 name2 "value two"
sl@0
   479
} {name1=value1&name2=value%20two}
sl@0
   480
# test http-5.2 obsoleted by 5.4 and 5.5 with http 2.5
sl@0
   481
test http-5.3 {http::formatQuery} {
sl@0
   482
    http::formatQuery lines "line1\nline2\nline3"
sl@0
   483
} {lines=line1%0d%0aline2%0d%0aline3}
sl@0
   484
test http-5.4 {http::formatQuery} {
sl@0
   485
    http::formatQuery name1 ~bwelch name2 \xa1\xa2\xa2
sl@0
   486
} {name1=~bwelch&name2=%c2%a1%c2%a2%c2%a2}
sl@0
   487
test http-5.5 {http::formatQuery} {
sl@0
   488
    set enc [http::config -urlencoding]
sl@0
   489
    http::config -urlencoding iso8859-1
sl@0
   490
    set res [http::formatQuery name1 ~bwelch name2 \xa1\xa2\xa2]
sl@0
   491
    http::config -urlencoding $enc
sl@0
   492
    set res
sl@0
   493
} {name1=~bwelch&name2=%a1%a2%a2}
sl@0
   494
sl@0
   495
test http-6.1 {http::ProxyRequired} {
sl@0
   496
    http::config -proxyhost [info hostname] -proxyport $port
sl@0
   497
    set token [http::geturl $url]
sl@0
   498
    http::wait $token
sl@0
   499
    http::config -proxyhost {} -proxyport {}
sl@0
   500
    upvar #0 $token data
sl@0
   501
    set data(body)
sl@0
   502
} "<html><head><title>HTTP/1.0 TEST</title></head><body>
sl@0
   503
<h1>Hello, World!</h1>
sl@0
   504
<h2>GET http:$url</h2>
sl@0
   505
</body></html>"
sl@0
   506
sl@0
   507
test http-7.1 {http::mapReply} {
sl@0
   508
    http::mapReply "abc\$\[\]\"\\()\}\{"
sl@0
   509
} {abc%24%5b%5d%22%5c%28%29%7d%7b}
sl@0
   510
test http-7.2 {http::mapReply} {
sl@0
   511
    # RFC 2718 specifies that we pass urlencoding on utf-8 chars by default,
sl@0
   512
    # so make sure this gets converted to utf-8 then urlencoded.
sl@0
   513
    http::mapReply "\u2208"
sl@0
   514
} {%e2%88%88}
sl@0
   515
test http-7.3 {http::formatQuery} {
sl@0
   516
    set enc [http::config -urlencoding]
sl@0
   517
    # this would be reverting to http <=2.4 behavior
sl@0
   518
    http::config -urlencoding ""
sl@0
   519
    set res [list [catch {http::mapReply "\u2208"} msg] $msg]
sl@0
   520
    http::config -urlencoding $enc
sl@0
   521
    set res
sl@0
   522
} [list 1 "can't read \"formMap(\u2208)\": no such element in array"]
sl@0
   523
test http-7.4 {http::formatQuery} {
sl@0
   524
    set enc [http::config -urlencoding]
sl@0
   525
    # this would be reverting to http <=2.4 behavior w/o errors
sl@0
   526
    # (unknown chars become '?')
sl@0
   527
    http::config -urlencoding "iso8859-1"
sl@0
   528
    set res [http::mapReply "\u2208"]
sl@0
   529
    http::config -urlencoding $enc
sl@0
   530
    set res
sl@0
   531
} {%3f}
sl@0
   532
sl@0
   533
# cleanup
sl@0
   534
catch {unset url}
sl@0
   535
catch {unset badurl}
sl@0
   536
catch {unset port}
sl@0
   537
catch {unset data}
sl@0
   538
if {[info exists httpthread]} {
sl@0
   539
    testthread send -async $httpthread {
sl@0
   540
	testthread exit
sl@0
   541
    }
sl@0
   542
} else {
sl@0
   543
    close $listen
sl@0
   544
}
sl@0
   545
sl@0
   546
if {[info exists removeHttpd]} {
sl@0
   547
    removeFile $httpdFile
sl@0
   548
}
sl@0
   549
sl@0
   550
rename bgerror {}
sl@0
   551
::tcltest::cleanupTests