httpold.test
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:8k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # Commands covered:  http_config, http_get, 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
  5. # generates output for errors.  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-1999 by Scriptics Corporation.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14. # RCS: @(#) $Id: httpold.test,v 1.11 2002/10/03 13:34:32 dkf Exp $
  15. if {[lsearch [namespace children] ::tcltest] == -1} {
  16.     package require tcltest
  17.     namespace import -force ::tcltest::*
  18. }
  19. if {[catch {package require http 1.0}]} {
  20.     if {[info exists httpold]} {
  21. catch {puts "Cannot load http 1.0 package"}
  22. ::tcltest::cleanupTests
  23. return
  24.     } else {
  25. catch {puts "Running http 1.0 tests in slave interp"}
  26. set interp [interp create httpold]
  27. $interp eval [list set httpold "running"]
  28. $interp eval [list set argv $argv]
  29. $interp eval [list source [info script]]
  30. interp delete $interp
  31. ::tcltest::cleanupTests
  32. return
  33.     }
  34. }
  35. set bindata "This is binary datax0dx0amorex0dmorex0amorex00null"
  36. catch {unset data}
  37. ## 
  38. ## The httpd script implement a stub http server
  39. ##
  40. source [file join [file dirname [info script]] httpd]
  41. set port 8010
  42. if [catch {httpd_init $port} listen] {
  43.     puts "Cannot start http server, http test skipped"
  44.     unset port
  45.     ::tcltest::cleanupTests
  46.     return
  47. }
  48. test httpold-1.1 {http_config} {
  49.     http_config
  50. } {-accept */* -proxyfilter httpProxyRequired -proxyhost {} -proxyport {} -useragent {Tcl http client package 1.0}}
  51. test httpold-1.2 {http_config} {
  52.     http_config -proxyfilter
  53. } httpProxyRequired
  54. test httpold-1.3 {http_config} {
  55.     catch {http_config -junk}
  56. } 1
  57. test httpold-1.4 {http_config} {
  58.     http_config -proxyhost nowhere.come -proxyport 8080 -proxyfilter myFilter -useragent "Tcl Test Suite"
  59.     set x [http_config]
  60.     http_config -proxyhost {} -proxyport {} -proxyfilter httpProxyRequired 
  61. -useragent "Tcl http client package 1.0"
  62.     set x
  63. } {-accept */* -proxyfilter myFilter -proxyhost nowhere.come -proxyport 8080 -useragent {Tcl Test Suite}}
  64. test httpold-1.5 {http_config} {
  65.     catch {http_config -proxyhost {} -junk 8080}
  66. } 1
  67. test httpold-2.1 {http_reset} {
  68.     catch {http_reset http#1}
  69. } 0
  70. test httpold-3.1 {http_get} {
  71.     catch {http_get -bogus flag}
  72. } 1
  73. test httpold-3.2 {http_get} {
  74.     catch {http_get http:junk} err
  75.     set err
  76. } {Unsupported URL: http:junk}
  77. set url [info hostname]:$port
  78. test httpold-3.3 {http_get} {
  79.     set token [http_get $url]
  80.     http_data $token
  81. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  82. <h1>Hello, World!</h1>
  83. <h2>GET /</h2>
  84. </body></html>"
  85. set tail /a/b/c
  86. set url [info hostname]:$port/a/b/c
  87. set binurl [info hostname]:$port/binary
  88. test httpold-3.4 {http_get} {
  89.     set token [http_get $url]
  90.     http_data $token
  91. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  92. <h1>Hello, World!</h1>
  93. <h2>GET $tail</h2>
  94. </body></html>"
  95. proc selfproxy {host} {
  96.     global port
  97.     return [list [info hostname] $port]
  98. }
  99. test httpold-3.5 {http_get} {
  100.     http_config -proxyfilter selfproxy
  101.     set token [http_get $url]
  102.     http_config -proxyfilter httpProxyRequired
  103.     http_data $token
  104. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  105. <h1>Hello, World!</h1>
  106. <h2>GET http://$url</h2>
  107. </body></html>"
  108. test httpold-3.6 {http_get} {
  109.     http_config -proxyfilter bogus
  110.     set token [http_get $url]
  111.     http_config -proxyfilter httpProxyRequired
  112.     http_data $token
  113. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  114. <h1>Hello, World!</h1>
  115. <h2>GET $tail</h2>
  116. </body></html>"
  117. test httpold-3.7 {http_get} {
  118.     set token [http_get $url -headers {Pragma no-cache}]
  119.     http_data $token
  120. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  121. <h1>Hello, World!</h1>
  122. <h2>GET $tail</h2>
  123. </body></html>"
  124. test httpold-3.8 {http_get} {
  125.     set token [http_get $url -query Name=Value&Foo=Bar]
  126.     http_data $token
  127. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  128. <h1>Hello, World!</h1>
  129. <h2>POST $tail</h2>
  130. <h2>Query</h2>
  131. <dl>
  132. <dt>Name<dd>Value
  133. <dt>Foo<dd>Bar
  134. </dl>
  135. </body></html>"
  136. test httpold-3.9 {http_get} {
  137.     set token [http_get $url -validate 1]
  138.     http_code $token
  139. } "HTTP/1.0 200 OK"
  140. test httpold-4.1 {httpEvent} {
  141.     set token [http_get $url]
  142.     upvar #0 $token data
  143.     array set meta $data(meta)
  144.     expr ($data(totalsize) == $meta(Content-Length))
  145. } 1
  146. test httpold-4.2 {httpEvent} {
  147.     set token [http_get $url]
  148.     upvar #0 $token data
  149.     array set meta $data(meta)
  150.     string compare $data(type) [string trim $meta(Content-Type)]
  151. } 0
  152. test httpold-4.3 {httpEvent} {
  153.     set token [http_get $url]
  154.     http_code $token
  155. } {HTTP/1.0 200 Data follows}
  156. test httpold-4.4 {httpEvent} {
  157.     set testfile [makeFile "" testfile]
  158.     set out [open $testfile w]
  159.     set token [http_get $url -channel $out]
  160.     close $out
  161.     set in [open $testfile]
  162.     set x [read $in]
  163.     close $in
  164.     removeFile $testfile
  165.     set x
  166. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  167. <h1>Hello, World!</h1>
  168. <h2>GET $tail</h2>
  169. </body></html>"
  170. test httpold-4.5 {httpEvent} {
  171.     set testfile [makeFile "" testfile]
  172.     set out [open $testfile w]
  173.     set token [http_get $url -channel $out]
  174.     close $out
  175.     upvar #0 $token data
  176.     removeFile $testfile
  177.     expr $data(currentsize) == $data(totalsize)
  178. } 1
  179. test httpold-4.6 {httpEvent} {
  180.     set testfile [makeFile "" testfile]
  181.     set out [open $testfile w]
  182.     set token [http_get $binurl -channel $out]
  183.     close $out
  184.     set in [open $testfile]
  185.     fconfigure $in -translation binary
  186.     set x [read $in]
  187.     close $in
  188.     removeFile $testfile
  189.     set x
  190. } "$bindata$binurl"
  191. proc myProgress {token total current} {
  192.     global progress httpLog
  193.     if {[info exists httpLog] && $httpLog} {
  194. puts "progress $total $current"
  195.     }
  196.     set progress [list $total $current]
  197. }
  198. if 0 {
  199.     # This test hangs on Windows95 because the client never gets EOF
  200.     set httpLog 1
  201.     test httpold-4.6 {httpEvent} {
  202. set token [http_get $url -blocksize 50 -progress myProgress]
  203. set progress
  204.     } {111 111}
  205. }
  206. test httpold-4.7 {httpEvent} {
  207.     set token [http_get $url -progress myProgress]
  208.     set progress
  209. } {111 111}
  210. test httpold-4.8 {httpEvent} {
  211.     set token [http_get $url]
  212.     http_status $token
  213. } {ok}
  214. test httpold-4.9 {httpEvent} {
  215.     set token [http_get $url -progress myProgress]
  216.     http_code $token
  217. } {HTTP/1.0 200 Data follows}
  218. test httpold-4.10 {httpEvent} {
  219.     set token [http_get $url -progress myProgress]
  220.     http_size $token
  221. } {111}
  222. test httpold-4.11 {httpEvent} {
  223.     set token [http_get $url -timeout 1 -command {#}]
  224.     http_reset $token
  225.     http_status $token
  226. } {reset}
  227. test httpold-4.12 {httpEvent} {
  228.     update
  229.     set x {}
  230.     after 500 {lappend x ok}
  231.     set token [http_get $url -timeout 1 -command {lappend x fail}]
  232.     vwait x
  233.     list [http_status $token] $x
  234. } {timeout ok}
  235. test httpold-5.1 {http_formatQuery} {
  236.     http_formatQuery name1 value1 name2 "value two"
  237. } {name1=value1&name2=value+two}
  238. test httpold-5.2 {http_formatQuery} {
  239.     http_formatQuery name1 ~bwelch name2 xa1xa2xa2
  240. } {name1=%7ebwelch&name2=%a1%a2%a2}
  241. test httpold-5.3 {http_formatQuery} {
  242.     http_formatQuery lines "line1nline2nline3"
  243. } {lines=line1%0d%0aline2%0d%0aline3}
  244. test httpold-6.1 {httpProxyRequired} {
  245.     update
  246.     http_config -proxyhost [info hostname] -proxyport $port
  247.     set token [http_get $url]
  248.     http_wait $token
  249.     http_config -proxyhost {} -proxyport {}
  250.     upvar #0 $token data
  251.     set data(body)
  252. } "<html><head><title>HTTP/1.0 TEST</title></head><body>
  253. <h1>Hello, World!</h1>
  254. <h2>GET http://$url</h2>
  255. </body></html>"
  256. # cleanup
  257. catch {unset url}
  258. catch {unset port}
  259. catch {unset data}
  260. close $listen
  261. ::tcltest::cleanupTests
  262. return