| 1 |
# Commands covered: http::config, http::geturl, http::wait, http::reset |
| 2 |
# |
| 3 |
# This file contains a collection of tests for the http script library. |
| 4 |
# Sourcing this file into Tcl runs the tests and generates output for errors. |
| 5 |
# No output means no errors were found. |
| 6 |
# |
| 7 |
# Copyright (c) 1991-1993 The Regents of the University of California. |
| 8 |
# Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 9 |
# Copyright (c) 1998-2000 by Ajuba Solutions. |
| 10 |
# |
| 11 |
# See the file "license.terms" for information on usage and redistribution of |
| 12 |
# this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
|
| 14 |
if {"::tcltest" ni [namespace children]} { |
| 15 |
package require tcltest 2.5 |
| 16 |
namespace import -force ::tcltest::* |
| 17 |
} |
| 18 |
|
| 19 |
if {[catch {package require http 2} version]} { |
| 20 |
if {[info exists http2]} { |
| 21 |
catch {puts "Cannot load http 2.* package"} |
| 22 |
return |
| 23 |
} else { |
| 24 |
catch {puts "Running http 2.* tests in child interp"} |
| 25 |
set interp [interp create http2] |
| 26 |
$interp eval [list set http2 "running"] |
| 27 |
$interp eval [list set argv $argv] |
| 28 |
$interp eval [list source [info script]] |
| 29 |
interp delete $interp |
| 30 |
return |
| 31 |
} |
| 32 |
} |
| 33 |
testConstraint http2.9.7 [package vsatisfies [package provide http] 2.9.7] |
| 34 |
testConstraint http2.9.9 [package vsatisfies [package provide http] 2.9.9] |
| 35 |
|
| 36 |
proc bgerror {args} { |
| 37 |
global errorInfo |
| 38 |
puts stderr "http.test bgerror" |
| 39 |
puts stderr [join $args] |
| 40 |
puts stderr $errorInfo |
| 41 |
} |
| 42 |
|
| 43 |
# Do not use [info hostname]. |
| 44 |
# Name resolution is often a problem on OSX; not focus of HTTP package anyway. |
| 45 |
# Also a problem on other platforms for http-4.14 (test with bad port number). |
| 46 |
set HOST localhost |
| 47 |
set port 8010 |
| 48 |
set bindata "This is binary data\x0d\x0amore\x0dmore\x0amore\x00null" |
| 49 |
catch {unset data} |
| 50 |
|
| 51 |
# Ensure httpd file exists |
| 52 |
|
| 53 |
set origFile [file join [pwd] [file dirname [info script]] httpd] |
| 54 |
set httpdFile [file join [temporaryDirectory] httpd_[pid]] |
| 55 |
if {![file exists $httpdFile]} { |
| 56 |
makeFile "" $httpdFile |
| 57 |
file delete $httpdFile |
| 58 |
file copy $origFile $httpdFile |
| 59 |
set removeHttpd 1 |
| 60 |
} |
| 61 |
|
| 62 |
catch {package require Thread 2.7-} |
| 63 |
if {[catch {package present Thread}] == 0 && [file exists $httpdFile]} { |
| 64 |
set httpthread [thread::create -preserved] |
| 65 |
thread::send $httpthread [list source $httpdFile] |
| 66 |
thread::send $httpthread [list set port $port] |
| 67 |
thread::send $httpthread [list set bindata $bindata] |
| 68 |
thread::send $httpthread {httpd_init $port} |
| 69 |
puts "Running httpd in thread $httpthread" |
| 70 |
} else { |
| 71 |
if {![file exists $httpdFile]} { |
| 72 |
puts "Cannot read $httpdFile script, http test skipped" |
| 73 |
unset port |
| 74 |
return |
| 75 |
} |
| 76 |
source $httpdFile |
| 77 |
# Let the OS pick the port; that's much more flexible |
| 78 |
if {[catch {httpd_init 0} listen]} { |
| 79 |
puts "Cannot start http server, http test skipped" |
| 80 |
unset port |
| 81 |
return |
| 82 |
} else { |
| 83 |
# Running httpd in the current thread overwrites the values of port |
| 84 |
# (here) and HOST (in the sourced server file). |
| 85 |
set port [lindex [fconfigure $listen -sockname] 2] |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
test http-1.1 {http::config} { |
| 90 |
http::config -useragent UserAgent |
| 91 |
http::config |
| 92 |
} [list -accept */* -pipeline 1 -postfresh 0 -proxyfilter http::ProxyRequired -proxyhost {} -proxyport {} -repost 0 -urlencoding utf-8 -useragent UserAgent -zip 1] |
| 93 |
test http-1.2 {http::config} { |
| 94 |
http::config -proxyfilter |
| 95 |
} http::ProxyRequired |
| 96 |
test http-1.3 {http::config} { |
| 97 |
catch {http::config -junk} |
| 98 |
} 1 |
| 99 |
test http-1.4 {http::config} { |
| 100 |
set savedconf [http::config] |
| 101 |
http::config -proxyhost nowhere.come -proxyport 8080 \ |
| 102 |
-proxyfilter myFilter -useragent "Tcl Test Suite" \ |
| 103 |
-urlencoding iso8859-1 |
| 104 |
set x [http::config] |
| 105 |
http::config {*}$savedconf |
| 106 |
set x |
| 107 |
} {-accept */* -pipeline 1 -postfresh 0 -proxyfilter myFilter -proxyhost nowhere.come -proxyport 8080 -repost 0 -urlencoding iso8859-1 -useragent {Tcl Test Suite} -zip 1} |
| 108 |
test http-1.5 {http::config} -returnCodes error -body { |
| 109 |
http::config -proxyhost {} -junk 8080 |
| 110 |
} -result {Unknown option -junk, must be: -accept, -pipeline, -postfresh, -proxyfilter, -proxyhost, -proxyport, -repost, -urlencoding, -useragent, -zip} |
| 111 |
test http-1.6 {http::config} -setup { |
| 112 |
set oldenc [http::config -urlencoding] |
| 113 |
} -body { |
| 114 |
set enc [list [http::config -urlencoding]] |
| 115 |
http::config -urlencoding iso8859-1 |
| 116 |
lappend enc [http::config -urlencoding] |
| 117 |
} -cleanup { |
| 118 |
http::config -urlencoding $oldenc |
| 119 |
} -result {utf-8 iso8859-1} |
| 120 |
|
| 121 |
test http-2.1 {http::reset} { |
| 122 |
catch {http::reset http#1} |
| 123 |
} 0 |
| 124 |
test http-2.2 {http::CharsetToEncoding} http2.9.7 { |
| 125 |
http::CharsetToEncoding iso-8859-11 |
| 126 |
} iso8859-11 |
| 127 |
test http-2.3 {http::CharsetToEncoding} http2.9.7 { |
| 128 |
http::CharsetToEncoding iso-2022-kr |
| 129 |
} iso2022-kr |
| 130 |
test http-2.4 {http::CharsetToEncoding} http2.9.7 { |
| 131 |
http::CharsetToEncoding shift-jis |
| 132 |
} shiftjis |
| 133 |
test http-2.5 {http::CharsetToEncoding} http2.9.7 { |
| 134 |
http::CharsetToEncoding windows-437 |
| 135 |
} cp437 |
| 136 |
test http-2.6 {http::CharsetToEncoding} http2.9.7 { |
| 137 |
http::CharsetToEncoding latin5 |
| 138 |
} iso8859-9 |
| 139 |
test http-2.7 {http::CharsetToEncoding} http2.9.7 { |
| 140 |
http::CharsetToEncoding latin1 |
| 141 |
} iso8859-1 |
| 142 |
test http-2.8 {http::CharsetToEncoding} http2.9.7 { |
| 143 |
http::CharsetToEncoding latin4 |
| 144 |
} binary |
| 145 |
|
| 146 |
test http-3.1 {http::geturl} -returnCodes error -body { |
| 147 |
http::geturl -bogus flag |
| 148 |
} -result {Unknown option flag, can be: -binary, -blocksize, -channel, -command, -handler, -headers, -keepalive, -method, -myaddr, -progress, -protocol, -query, -queryblocksize, -querychannel, -queryprogress, -strict, -timeout, -type, -validate} |
| 149 |
test http-3.2 {http::geturl} -returnCodes error -body { |
| 150 |
http::geturl http:junk |
| 151 |
} -result {Unsupported URL: http:junk} |
| 152 |
set url //${::HOST}:$port |
| 153 |
set badurl //${::HOST}:[expr {$port+1}] |
| 154 |
test http-3.3 {http::geturl} -body { |
| 155 |
set token [http::geturl $url] |
| 156 |
http::data $token |
| 157 |
} -cleanup { |
| 158 |
http::cleanup $token |
| 159 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 160 |
<h1>Hello, World!</h1> |
| 161 |
<h2>GET /</h2> |
| 162 |
</body></html>" |
| 163 |
set tail /a/b/c |
| 164 |
set url //${::HOST}:$port/a/b/c |
| 165 |
set fullurl HTTP://user:pass@${::HOST}:$port/a/b/c |
| 166 |
set binurl //${::HOST}:$port/binary |
| 167 |
set xmlurl //${::HOST}:$port/xml |
| 168 |
set posturl //${::HOST}:$port/post |
| 169 |
set badposturl //${::HOST}:$port/droppost |
| 170 |
set authorityurl //${::HOST}:$port |
| 171 |
set ipv6url http://\[::1\]:$port/ |
| 172 |
test http-3.4 {http::geturl} -body { |
| 173 |
set token [http::geturl $url] |
| 174 |
http::data $token |
| 175 |
} -cleanup { |
| 176 |
http::cleanup $token |
| 177 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 178 |
<h1>Hello, World!</h1> |
| 179 |
<h2>GET $tail</h2> |
| 180 |
</body></html>" |
| 181 |
proc selfproxy {host} { |
| 182 |
global port |
| 183 |
return [list ${::HOST} $port] |
| 184 |
} |
| 185 |
test http-3.5 {http::geturl} -body { |
| 186 |
http::config -proxyfilter selfproxy |
| 187 |
set token [http::geturl $url] |
| 188 |
http::data $token |
| 189 |
} -cleanup { |
| 190 |
http::config -proxyfilter http::ProxyRequired |
| 191 |
http::cleanup $token |
| 192 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 193 |
<h1>Hello, World!</h1> |
| 194 |
<h2>GET http:$url</h2> |
| 195 |
</body></html>" |
| 196 |
test http-3.6 {http::geturl} -body { |
| 197 |
http::config -proxyfilter bogus |
| 198 |
set token [http::geturl $url] |
| 199 |
http::data $token |
| 200 |
} -cleanup { |
| 201 |
http::config -proxyfilter http::ProxyRequired |
| 202 |
http::cleanup $token |
| 203 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 204 |
<h1>Hello, World!</h1> |
| 205 |
<h2>GET $tail</h2> |
| 206 |
</body></html>" |
| 207 |
test http-3.7 {http::geturl} -body { |
| 208 |
set token [http::geturl $url -headers {Pragma no-cache}] |
| 209 |
http::data $token |
| 210 |
} -cleanup { |
| 211 |
http::cleanup $token |
| 212 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 213 |
<h1>Hello, World!</h1> |
| 214 |
<h2>GET $tail</h2> |
| 215 |
</body></html>" |
| 216 |
test http-3.8 {http::geturl} -body { |
| 217 |
set token [http::geturl $url -query Name=Value&Foo=Bar -timeout 3000] |
| 218 |
http::data $token |
| 219 |
} -cleanup { |
| 220 |
http::cleanup $token |
| 221 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 222 |
<h1>Hello, World!</h1> |
| 223 |
<h2>POST $tail</h2> |
| 224 |
<h2>Query</h2> |
| 225 |
<dl> |
| 226 |
<dt>Name<dd>Value |
| 227 |
<dt>Foo<dd>Bar |
| 228 |
</dl> |
| 229 |
</body></html>" |
| 230 |
test http-3.9 {http::geturl} -body { |
| 231 |
set token [http::geturl $url -validate 1] |
| 232 |
http::code $token |
| 233 |
} -cleanup { |
| 234 |
http::cleanup $token |
| 235 |
} -result "HTTP/1.0 200 OK" |
| 236 |
test http-3.10 {http::geturl queryprogress} -setup { |
| 237 |
set query foo=bar |
| 238 |
set sep "" |
| 239 |
set i 0 |
| 240 |
# Create about 120K of query data |
| 241 |
while {$i < 14} { |
| 242 |
incr i |
| 243 |
append query $sep$query |
| 244 |
set sep & |
| 245 |
} |
| 246 |
} -body { |
| 247 |
proc postProgress {token x y} { |
| 248 |
global postProgress |
| 249 |
lappend postProgress $y |
| 250 |
} |
| 251 |
set postProgress {} |
| 252 |
set t [http::geturl $posturl -keepalive 0 -query $query \ |
| 253 |
-queryprogress postProgress -queryblocksize 16384] |
| 254 |
http::wait $t |
| 255 |
list [http::status $t] [string length $query] $postProgress [http::data $t] |
| 256 |
} -cleanup { |
| 257 |
http::cleanup $t |
| 258 |
} -result {ok 122879 {16384 32768 49152 65536 81920 98304 114688 122879} {Got 122879 bytes}} |
| 259 |
test http-3.11 {http::geturl querychannel with -command} -setup { |
| 260 |
set query foo=bar |
| 261 |
set sep "" |
| 262 |
set i 0 |
| 263 |
# Create about 120K of query data |
| 264 |
while {$i < 14} { |
| 265 |
incr i |
| 266 |
append query $sep$query |
| 267 |
set sep & |
| 268 |
} |
| 269 |
set file [makeFile $query outdata] |
| 270 |
} -body { |
| 271 |
set fp [open $file] |
| 272 |
proc asyncCB {token} { |
| 273 |
global postResult |
| 274 |
lappend postResult [http::data $token] |
| 275 |
} |
| 276 |
set postResult [list ] |
| 277 |
set t [http::geturl $posturl -querychannel $fp] |
| 278 |
http::wait $t |
| 279 |
set testRes [list [http::status $t] [string length $query] [http::data $t]] |
| 280 |
# Now do async |
| 281 |
http::cleanup $t |
| 282 |
close $fp |
| 283 |
set fp [open $file] |
| 284 |
set t [http::geturl $posturl -querychannel $fp -command asyncCB] |
| 285 |
set postResult [list PostStart] |
| 286 |
http::wait $t |
| 287 |
close $fp |
| 288 |
lappend testRes [http::status $t] $postResult |
| 289 |
} -cleanup { |
| 290 |
removeFile outdata |
| 291 |
http::cleanup $t |
| 292 |
} -result {ok 122879 {Got 122880 bytes} ok {PostStart {Got 122880 bytes}}} |
| 293 |
# On Linux platforms when the client and server are on the same host, the |
| 294 |
# client is unable to read the server's response one it hits the write error. |
| 295 |
# The status is "eof". |
| 296 |
# On Windows, the http::wait procedure gets a "connection reset by peer" error |
| 297 |
# while reading the reply. |
| 298 |
test http-3.12 {http::geturl querychannel with aborted request} -setup { |
| 299 |
set query foo=bar |
| 300 |
set sep "" |
| 301 |
set i 0 |
| 302 |
# Create about 120K of query data |
| 303 |
while {$i < 14} { |
| 304 |
incr i |
| 305 |
append query $sep$query |
| 306 |
set sep & |
| 307 |
} |
| 308 |
set file [makeFile $query outdata] |
| 309 |
} -constraints {nonPortable} -body { |
| 310 |
set fp [open $file] |
| 311 |
proc asyncCB {token} { |
| 312 |
global postResult |
| 313 |
lappend postResult [http::data $token] |
| 314 |
} |
| 315 |
proc postProgress {token x y} { |
| 316 |
global postProgress |
| 317 |
lappend postProgress $y |
| 318 |
} |
| 319 |
set postProgress {} |
| 320 |
# Now do async |
| 321 |
set postResult [list PostStart] |
| 322 |
if {[catch { |
| 323 |
set t [http::geturl $badposturl -querychannel $fp -command asyncCB \ |
| 324 |
-queryprogress postProgress] |
| 325 |
http::wait $t |
| 326 |
upvar #0 $t state |
| 327 |
} err]} { |
| 328 |
puts $::errorInfo |
| 329 |
error $err |
| 330 |
} |
| 331 |
list [http::status $t] [http::code $t] |
| 332 |
} -cleanup { |
| 333 |
removeFile outdata |
| 334 |
http::cleanup $t |
| 335 |
} -result {ok {HTTP/1.0 200 Data follows}} |
| 336 |
test http-3.13 {http::geturl socket leak test} { |
| 337 |
set chanCount [llength [file channels]] |
| 338 |
for {set i 0} {$i < 3} {incr i} { |
| 339 |
catch {http::geturl $badurl -timeout 5000} |
| 340 |
} |
| 341 |
# No extra channels should be taken |
| 342 |
expr {[llength [file channels]] == $chanCount} |
| 343 |
} 1 |
| 344 |
test http-3.14 "http::geturl $fullurl" -body { |
| 345 |
set token [http::geturl $fullurl -validate 1] |
| 346 |
http::code $token |
| 347 |
} -cleanup { |
| 348 |
http::cleanup $token |
| 349 |
} -result "HTTP/1.0 200 OK" |
| 350 |
test http-3.15 {http::geturl parse failures} -body { |
| 351 |
http::geturl "{invalid}:url" |
| 352 |
} -returnCodes error -result {Unsupported URL: {invalid}:url} |
| 353 |
test http-3.16 {http::geturl parse failures} -body { |
| 354 |
http::geturl http:relative/url |
| 355 |
} -returnCodes error -result {Unsupported URL: http:relative/url} |
| 356 |
test http-3.17 {http::geturl parse failures} -body { |
| 357 |
http::geturl /absolute/url |
| 358 |
} -returnCodes error -result {Missing host part: /absolute/url} |
| 359 |
test http-3.18 {http::geturl parse failures} -body { |
| 360 |
http::geturl http://somewhere:123456789/ |
| 361 |
} -returnCodes error -result {Invalid port number: 123456789} |
| 362 |
test http-3.19 {http::geturl parse failures} -body { |
| 363 |
http::geturl http://{user}@somewhere |
| 364 |
} -returnCodes error -result {Illegal characters in URL user} |
| 365 |
test http-3.20 {http::geturl parse failures} -body { |
| 366 |
http::geturl http://%user@somewhere |
| 367 |
} -returnCodes error -result {Illegal encoding character usage "%us" in URL user} |
| 368 |
test http-3.21 {http::geturl parse failures} -body { |
| 369 |
http::geturl http://somewhere/{path} |
| 370 |
} -returnCodes error -result {Illegal characters in URL path} |
| 371 |
test http-3.22 {http::geturl parse failures} -body { |
| 372 |
http::geturl http://somewhere/%path |
| 373 |
} -returnCodes error -result {Illegal encoding character usage "%pa" in URL path} |
| 374 |
test http-3.23 {http::geturl parse failures} -body { |
| 375 |
http::geturl http://somewhere/path?{query}? |
| 376 |
} -returnCodes error -result {Illegal characters in URL path} |
| 377 |
test http-3.24 {http::geturl parse failures} -body { |
| 378 |
http::geturl http://somewhere/path?%query |
| 379 |
} -returnCodes error -result {Illegal encoding character usage "%qu" in URL path} |
| 380 |
test http-3.25 {http::meta} -setup { |
| 381 |
unset -nocomplain m token |
| 382 |
} -body { |
| 383 |
set token [http::geturl $url -timeout 3000] |
| 384 |
array set m [http::meta $token] |
| 385 |
lsort [array names m] |
| 386 |
} -cleanup { |
| 387 |
http::cleanup $token |
| 388 |
unset -nocomplain m token |
| 389 |
} -result {Content-Length Content-Type Date} |
| 390 |
test http-3.26 {http::meta} -setup { |
| 391 |
unset -nocomplain m token |
| 392 |
} -body { |
| 393 |
set token [http::geturl $url -headers {X-Check 1} -timeout 3000] |
| 394 |
array set m [http::meta $token] |
| 395 |
lsort [array names m] |
| 396 |
} -cleanup { |
| 397 |
http::cleanup $token |
| 398 |
unset -nocomplain m token |
| 399 |
} -result {Content-Length Content-Type Date X-Check} |
| 400 |
test http-3.27 {http::geturl: -headers override -type} -body { |
| 401 |
set token [http::geturl $url/headers -type "text/plain" -query dummy \ |
| 402 |
-headers [list "Content-Type" "text/plain;charset=utf-8"]] |
| 403 |
http::data $token |
| 404 |
} -cleanup { |
| 405 |
http::cleanup $token |
| 406 |
} -match regexp -result {(?n)Host .* |
| 407 |
User-Agent .* |
| 408 |
Connection close |
| 409 |
Content-Type {text/plain;charset=utf-8} |
| 410 |
Accept \*/\* |
| 411 |
Accept-Encoding .* |
| 412 |
Content-Length 5} |
| 413 |
test http-3.28 {http::geturl: -headers override -type default} -body { |
| 414 |
set token [http::geturl $url/headers -query dummy \ |
| 415 |
-headers [list "Content-Type" "text/plain;charset=utf-8"]] |
| 416 |
http::data $token |
| 417 |
} -cleanup { |
| 418 |
http::cleanup $token |
| 419 |
} -match regexp -result {(?n)Host .* |
| 420 |
User-Agent .* |
| 421 |
Connection close |
| 422 |
Content-Type {text/plain;charset=utf-8} |
| 423 |
Accept \*/\* |
| 424 |
Accept-Encoding .* |
| 425 |
Content-Length 5} |
| 426 |
test http-3.29 {http::geturl IPv6 address} -body { |
| 427 |
# We only want to see if the URL gets parsed correctly. This is |
| 428 |
# the case if http::geturl succeeds or returns a socket related |
| 429 |
# error. If the parsing is wrong, we'll get a parse error. |
| 430 |
# It'd be better to separate the URL parser from http::geturl, so |
| 431 |
# that it can be tested without also trying to make a connection. |
| 432 |
set error [catch {http::geturl $ipv6url -validate 1} token] |
| 433 |
if {$error && [string match "couldn't open socket: *" $token]} { |
| 434 |
set error 0 |
| 435 |
} |
| 436 |
set error |
| 437 |
} -cleanup { |
| 438 |
catch { http::cleanup $token } |
| 439 |
} -result 0 |
| 440 |
test http-3.30 {http::geturl query without path} -body { |
| 441 |
set token [http::geturl $authorityurl?var=val] |
| 442 |
http::ncode $token |
| 443 |
} -cleanup { |
| 444 |
catch { http::cleanup $token } |
| 445 |
} -result 200 |
| 446 |
test http-3.31 {http::geturl fragment without path} -body { |
| 447 |
set token [http::geturl "$authorityurl#fragment42"] |
| 448 |
http::ncode $token |
| 449 |
} -cleanup { |
| 450 |
catch { http::cleanup $token } |
| 451 |
} -result 200 |
| 452 |
# Bug c11a51c482 |
| 453 |
test http-3.32 {http::geturl: -headers override -accept default} -body { |
| 454 |
set token [http::geturl $url/headers -query dummy \ |
| 455 |
-headers [list "Accept" "text/plain,application/tcl-test-value"]] |
| 456 |
http::data $token |
| 457 |
} -cleanup { |
| 458 |
http::cleanup $token |
| 459 |
} -match regexp -result {(?n)Host .* |
| 460 |
User-Agent .* |
| 461 |
Connection close |
| 462 |
Accept text/plain,application/tcl-test-value |
| 463 |
Accept-Encoding .* |
| 464 |
Content-Type application/x-www-form-urlencoded |
| 465 |
Content-Length 5} |
| 466 |
# Bug 838e99a76d |
| 467 |
test http-3.33 {http::geturl application/xml is text} -body { |
| 468 |
set token [http::geturl "$xmlurl"] |
| 469 |
scan [http::data $token] "<%\[^>]>%c<%\[^>]>" |
| 470 |
} -cleanup { |
| 471 |
catch { http::cleanup $token } |
| 472 |
} -result {test 4660 /test} |
| 473 |
|
| 474 |
|
| 475 |
test http-3.34 {http::geturl -headers not a list} -returnCodes error -body { |
| 476 |
http::geturl http://test/t -headers \" |
| 477 |
} -constraints http2.9.9 -result {Bad value for -headers ("), must be list} |
| 478 |
test http-3.35 {http::geturl -headers not even number of elements} -returnCodes error -body { |
| 479 |
http::geturl http://test/t -headers {List Length 3} |
| 480 |
} -constraints http2.9.9 -result {Bad value for -headers (List Length 3), number of list elements must be even} |
| 481 |
|
| 482 |
test http-4.1 {http::Event} -body { |
| 483 |
set token [http::geturl $url -keepalive 0] |
| 484 |
upvar #0 $token data |
| 485 |
array set meta $data(meta) |
| 486 |
expr {($data(totalsize) == $meta(Content-Length))} |
| 487 |
} -cleanup { |
| 488 |
http::cleanup $token |
| 489 |
} -result 1 |
| 490 |
test http-4.2 {http::Event} -body { |
| 491 |
set token [http::geturl $url] |
| 492 |
upvar #0 $token data |
| 493 |
array set meta $data(meta) |
| 494 |
string compare $data(type) [string trim $meta(Content-Type)] |
| 495 |
} -cleanup { |
| 496 |
http::cleanup $token |
| 497 |
} -result 0 |
| 498 |
test http-4.3 {http::Event} -body { |
| 499 |
set token [http::geturl $url] |
| 500 |
http::code $token |
| 501 |
} -cleanup { |
| 502 |
http::cleanup $token |
| 503 |
} -result {HTTP/1.0 200 Data follows} |
| 504 |
test http-4.4 {http::Event} -setup { |
| 505 |
set testfile [makeFile "" testfile] |
| 506 |
} -body { |
| 507 |
set out [open $testfile w] |
| 508 |
set token [http::geturl $url -channel $out] |
| 509 |
close $out |
| 510 |
set in [open $testfile] |
| 511 |
set x [read $in] |
| 512 |
} -cleanup { |
| 513 |
catch {close $in} |
| 514 |
catch {close $out} |
| 515 |
removeFile $testfile |
| 516 |
http::cleanup $token |
| 517 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 518 |
<h1>Hello, World!</h1> |
| 519 |
<h2>GET $tail</h2> |
| 520 |
</body></html>" |
| 521 |
test http-4.5 {http::Event} -setup { |
| 522 |
set testfile [makeFile "" testfile] |
| 523 |
} -body { |
| 524 |
set out [open $testfile w] |
| 525 |
fconfigure $out -translation lf |
| 526 |
set token [http::geturl $url -channel $out] |
| 527 |
close $out |
| 528 |
upvar #0 $token data |
| 529 |
expr {$data(currentsize) == $data(totalsize)} |
| 530 |
} -cleanup { |
| 531 |
removeFile $testfile |
| 532 |
http::cleanup $token |
| 533 |
} -result 1 |
| 534 |
test http-4.6 {http::Event} -setup { |
| 535 |
set testfile [makeFile "" testfile] |
| 536 |
} -body { |
| 537 |
set out [open $testfile w] |
| 538 |
set token [http::geturl $binurl -channel $out] |
| 539 |
close $out |
| 540 |
set in [open $testfile] |
| 541 |
fconfigure $in -translation binary |
| 542 |
read $in |
| 543 |
} -cleanup { |
| 544 |
catch {close $in} |
| 545 |
catch {close $out} |
| 546 |
removeFile $testfile |
| 547 |
http::cleanup $token |
| 548 |
} -result "$bindata[string trimleft $binurl /]" |
| 549 |
proc myProgress {token total current} { |
| 550 |
global progress httpLog |
| 551 |
if {[info exists httpLog] && $httpLog} { |
| 552 |
puts "progress $total $current" |
| 553 |
} |
| 554 |
set progress [list $total $current] |
| 555 |
} |
| 556 |
test http-4.6.1 {http::Event} knownBug { |
| 557 |
set token [http::geturl $url -blocksize 50 -progress myProgress] |
| 558 |
return $progress |
| 559 |
} {111 111} |
| 560 |
test http-4.7 {http::Event} -body { |
| 561 |
set token [http::geturl $url -keepalive 0 -progress myProgress] |
| 562 |
return $progress |
| 563 |
} -cleanup { |
| 564 |
http::cleanup $token |
| 565 |
} -result {111 111} |
| 566 |
test http-4.8 {http::Event} -body { |
| 567 |
set token [http::geturl $url] |
| 568 |
http::status $token |
| 569 |
} -cleanup { |
| 570 |
http::cleanup $token |
| 571 |
} -result {ok} |
| 572 |
test http-4.9 {http::Event} -body { |
| 573 |
set token [http::geturl $url -progress myProgress] |
| 574 |
http::code $token |
| 575 |
} -cleanup { |
| 576 |
http::cleanup $token |
| 577 |
} -result {HTTP/1.0 200 Data follows} |
| 578 |
test http-4.10 {http::Event} -body { |
| 579 |
set token [http::geturl $url -progress myProgress] |
| 580 |
http::size $token |
| 581 |
} -cleanup { |
| 582 |
http::cleanup $token |
| 583 |
} -result {111} |
| 584 |
# Timeout cases |
| 585 |
# Short timeout to working server (the test server). This lets us try a |
| 586 |
# reset during the connection. |
| 587 |
test http-4.11 {http::Event} -body { |
| 588 |
set token [http::geturl $url -timeout 1 -keepalive 0 -command \#] |
| 589 |
http::reset $token |
| 590 |
http::status $token |
| 591 |
} -cleanup { |
| 592 |
http::cleanup $token |
| 593 |
} -result {reset} |
| 594 |
# Longer timeout with reset. |
| 595 |
test http-4.12 {http::Event} -body { |
| 596 |
set token [http::geturl $url/?timeout=10 -keepalive 0 -command \#] |
| 597 |
http::reset $token |
| 598 |
http::status $token |
| 599 |
} -cleanup { |
| 600 |
http::cleanup $token |
| 601 |
} -result {reset} |
| 602 |
# Medium timeout to working server that waits even longer. The timeout |
| 603 |
# hits while waiting for a reply. |
| 604 |
test http-4.13 {http::Event} -body { |
| 605 |
set token [http::geturl $url?timeout=30 -keepalive 0 -timeout 10 -command \#] |
| 606 |
http::wait $token |
| 607 |
http::status $token |
| 608 |
} -cleanup { |
| 609 |
http::cleanup $token |
| 610 |
} -result {timeout} |
| 611 |
# Longer timeout to good host, bad port, gets an error after the |
| 612 |
# connection "completes" but the socket is bad. |
| 613 |
test http-4.14 {http::Event} -body { |
| 614 |
set token [http::geturl $badurl/?timeout=10 -timeout 10000 -command \#] |
| 615 |
if {$token eq ""} { |
| 616 |
error "bogus return from http::geturl" |
| 617 |
} |
| 618 |
http::wait $token |
| 619 |
lindex [http::error $token] 0 |
| 620 |
} -cleanup { |
| 621 |
catch {http::cleanup $token} |
| 622 |
} -result {connect failed connection refused} |
| 623 |
# Bogus host |
| 624 |
test http-4.15 {http::Event} -body { |
| 625 |
# This test may fail if you use a proxy server. That is to be |
| 626 |
# expected and is not a problem with Tcl. |
| 627 |
set token [http::geturl //not_a_host.tcl.tk -timeout 3000 -command \#] |
| 628 |
http::wait $token |
| 629 |
http::status $token |
| 630 |
# error codes vary among platforms. |
| 631 |
} -cleanup { |
| 632 |
catch {http::cleanup $token} |
| 633 |
} -returnCodes 1 -match glob -result "couldn't open socket*" |
| 634 |
test http-4.16 {Leak with Close vs Keepalive (bug [6ca52aec14]} -setup { |
| 635 |
proc list-difference {l1 l2} { |
| 636 |
lmap item $l2 {if {$item in $l1} continue; set item} |
| 637 |
} |
| 638 |
} -body { |
| 639 |
set before [chan names] |
| 640 |
set token [http::geturl $url -headers {X-Connection keep-alive}] |
| 641 |
http::cleanup $token |
| 642 |
update |
| 643 |
# Compute what channels have been unexpectedly leaked past cleanup |
| 644 |
list-difference $before [chan names] |
| 645 |
} -cleanup { |
| 646 |
rename list-difference {} |
| 647 |
} -result {} |
| 648 |
|
| 649 |
test http-5.1 {http::formatQuery} { |
| 650 |
http::formatQuery name1 value1 name2 "value two" |
| 651 |
} {name1=value1&name2=value%20two} |
| 652 |
# test http-5.2 obsoleted by 5.4 and 5.5 with http 2.5 |
| 653 |
test http-5.3 {http::formatQuery} { |
| 654 |
http::formatQuery lines "line1\nline2\nline3" |
| 655 |
} {lines=line1%0D%0Aline2%0D%0Aline3} |
| 656 |
test http-5.4 {http::formatQuery} { |
| 657 |
http::formatQuery name1 ~bwelch name2 \xa1\xa2\xa2 |
| 658 |
} {name1=~bwelch&name2=%C2%A1%C2%A2%C2%A2} |
| 659 |
test http-5.5 {http::formatQuery} { |
| 660 |
set enc [http::config -urlencoding] |
| 661 |
http::config -urlencoding iso8859-1 |
| 662 |
set res [http::formatQuery name1 ~bwelch name2 \xa1\xa2\xa2] |
| 663 |
http::config -urlencoding $enc |
| 664 |
set res |
| 665 |
} {name1=~bwelch&name2=%A1%A2%A2} |
| 666 |
|
| 667 |
test http-6.1 {http::ProxyRequired} -body { |
| 668 |
http::config -proxyhost ${::HOST} -proxyport $port |
| 669 |
set token [http::geturl $url] |
| 670 |
http::wait $token |
| 671 |
upvar #0 $token data |
| 672 |
set data(body) |
| 673 |
} -cleanup { |
| 674 |
http::config -proxyhost {} -proxyport {} |
| 675 |
http::cleanup $token |
| 676 |
} -result "<html><head><title>HTTP/1.0 TEST</title></head><body> |
| 677 |
<h1>Hello, World!</h1> |
| 678 |
<h2>GET http:$url</h2> |
| 679 |
</body></html>" |
| 680 |
|
| 681 |
test http-7.1 {http::mapReply} { |
| 682 |
http::mapReply "abc\$\[\]\"\\()\}\{" |
| 683 |
} {abc%24%5B%5D%22%5C%28%29%7D%7B} |
| 684 |
test http-7.2 {http::mapReply} { |
| 685 |
# RFC 2718 specifies that we pass urlencoding on utf-8 chars by default, |
| 686 |
# so make sure this gets converted to utf-8 then urlencoded. |
| 687 |
http::mapReply "\u2208" |
| 688 |
} {%E2%88%88} |
| 689 |
test http-7.3 {http::formatQuery} -setup { |
| 690 |
set enc [http::config -urlencoding] |
| 691 |
} -returnCodes error -body { |
| 692 |
# this would be reverting to http <=2.4 behavior |
| 693 |
http::config -urlencoding "" |
| 694 |
http::mapReply "\u2208" |
| 695 |
} -cleanup { |
| 696 |
http::config -urlencoding $enc |
| 697 |
} -result "can't read \"formMap(\u2208)\": no such element in array" |
| 698 |
test http-7.4 {http::formatQuery} -setup { |
| 699 |
set enc [http::config -urlencoding] |
| 700 |
} -body { |
| 701 |
# this would be reverting to http <=2.4 behavior w/o errors |
| 702 |
# (unknown chars become '?') |
| 703 |
http::config -urlencoding "iso8859-1" |
| 704 |
http::mapReply "\u2208" |
| 705 |
} -cleanup { |
| 706 |
http::config -urlencoding $enc |
| 707 |
} -result {%3F} |
| 708 |
|
| 709 |
# cleanup |
| 710 |
catch {unset url} |
| 711 |
catch {unset badurl} |
| 712 |
catch {unset port} |
| 713 |
catch {unset data} |
| 714 |
if {[info exists httpthread]} { |
| 715 |
thread::release $httpthread |
| 716 |
} else { |
| 717 |
close $listen |
| 718 |
} |
| 719 |
|
| 720 |
if {[info exists removeHttpd]} { |
| 721 |
removeFile $httpdFile |
| 722 |
} |
| 723 |
|
| 724 |
rename bgerror {} |
| 725 |
::tcltest::cleanupTests |
| 726 |
|
| 727 |
# Local variables: |
| 728 |
# mode: tcl |
| 729 |
# End: |