Debian Codegraph @

tests/httpold.test

raw
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 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
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 child 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
36 # Do not use [info hostname].
37 # Name resolution is often a problem on OSX; not focus of HTTP package anyway.
38 # Also a problem on other platforms for http-4.14 (test with bad port number).
39 set HOST localhost
40
41 set bindata "This is binary data\x0d\x0amore\x0dmore\x0amore\x00null"
42 catch {unset data}
43
44 ##
45 ## The httpd script implement a stub http server
46 ## Sourcing httpd overwrites the value of HOST.
47 ##
48 source [file join [file dirname [info script]] httpd]
49
50 set port 8010
51 if {[catch {httpd_init $port} listen]} {
52 puts "Cannot start http server, http test skipped"
53 unset port
54 ::tcltest::cleanupTests
55 return
56 }
57
58 test httpold-1.1 {http_config} {
59 http_config
60 } {-accept */* -proxyfilter httpProxyRequired -proxyhost {} -proxyport {} -useragent {Tcl http client package 1.0}}
61
62 test httpold-1.2 {http_config} {
63 http_config -proxyfilter
64 } httpProxyRequired
65
66 test httpold-1.3 {http_config} {
67 catch {http_config -junk}
68 } 1
69
70 test httpold-1.4 {http_config} {
71 http_config -proxyhost nowhere.come -proxyport 8080 -proxyfilter myFilter -useragent "Tcl Test Suite"
72 set x [http_config]
73 http_config -proxyhost {} -proxyport {} -proxyfilter httpProxyRequired \
74 -useragent "Tcl http client package 1.0"
75 set x
76 } {-accept */* -proxyfilter myFilter -proxyhost nowhere.come -proxyport 8080 -useragent {Tcl Test Suite}}
77
78 test httpold-1.5 {http_config} {
79 catch {http_config -proxyhost {} -junk 8080}
80 } 1
81
82 test httpold-2.1 {http_reset} {
83 catch {http_reset http#1}
84 } 0
85
86 test httpold-3.1 {http_get} {
87 catch {http_get -bogus flag}
88 } 1
89 test httpold-3.2 {http_get} {
90 catch {http_get http:junk} err
91 set err
92 } {Unsupported URL: http:junk}
93
94 set url ${::HOST}:$port
95 test httpold-3.3 {http_get} {
96 set token [http_get $url]
97 http_data $token
98 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
99 <h1>Hello, World!</h1>
100 <h2>GET /</h2>
101 </body></html>"
102
103 set tail /a/b/c
104 set url ${::HOST}:$port/a/b/c
105 set binurl ${::HOST}:$port/binary
106
107 test httpold-3.4 {http_get} {
108 set token [http_get $url]
109 http_data $token
110 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
111 <h1>Hello, World!</h1>
112 <h2>GET $tail</h2>
113 </body></html>"
114
115 proc selfproxy {host} {
116 global port
117 return [list ${::HOST} $port]
118 }
119 test httpold-3.5 {http_get} {
120 http_config -proxyfilter selfproxy
121 set token [http_get $url]
122 http_config -proxyfilter httpProxyRequired
123 http_data $token
124 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
125 <h1>Hello, World!</h1>
126 <h2>GET http://$url</h2>
127 </body></html>"
128
129 test httpold-3.6 {http_get} {
130 http_config -proxyfilter bogus
131 set token [http_get $url]
132 http_config -proxyfilter httpProxyRequired
133 http_data $token
134 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
135 <h1>Hello, World!</h1>
136 <h2>GET $tail</h2>
137 </body></html>"
138
139 test httpold-3.7 {http_get} {
140 set token [http_get $url -headers {Pragma no-cache}]
141 http_data $token
142 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
143 <h1>Hello, World!</h1>
144 <h2>GET $tail</h2>
145 </body></html>"
146
147 test httpold-3.8 {http_get} {
148 set token [http_get $url -query Name=Value&Foo=Bar]
149 http_data $token
150 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
151 <h1>Hello, World!</h1>
152 <h2>POST $tail</h2>
153 <h2>Query</h2>
154 <dl>
155 <dt>Name<dd>Value
156 <dt>Foo<dd>Bar
157 </dl>
158 </body></html>"
159
160 test httpold-3.9 {http_get} {
161 set token [http_get $url -validate 1]
162 http_code $token
163 } "HTTP/1.0 200 OK"
164
165
166 test httpold-4.1 {httpEvent} {
167 set token [http_get $url]
168 upvar #0 $token data
169 array set meta $data(meta)
170 expr {$data(totalsize) == $meta(Content-Length)}
171 } 1
172
173 test httpold-4.2 {httpEvent} {
174 set token [http_get $url]
175 upvar #0 $token data
176 array set meta $data(meta)
177 string compare $data(type) [string trim $meta(Content-Type)]
178 } 0
179
180 test httpold-4.3 {httpEvent} {
181 set token [http_get $url]
182 http_code $token
183 } {HTTP/1.0 200 Data follows}
184
185 test httpold-4.4 {httpEvent} {
186 set testfile [makeFile "" testfile]
187 set out [open $testfile w]
188 set token [http_get $url -channel $out]
189 close $out
190 set in [open $testfile]
191 set x [read $in]
192 close $in
193 removeFile $testfile
194 set x
195 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
196 <h1>Hello, World!</h1>
197 <h2>GET $tail</h2>
198 </body></html>"
199
200 test httpold-4.5 {httpEvent} {
201 set testfile [makeFile "" testfile]
202 set out [open $testfile w]
203 set token [http_get $url -channel $out]
204 close $out
205 upvar #0 $token data
206 removeFile $testfile
207 expr {$data(currentsize) == $data(totalsize)}
208 } 1
209
210 test httpold-4.6 {httpEvent} {
211 set testfile [makeFile "" testfile]
212 set out [open $testfile w]
213 set token [http_get $binurl -channel $out]
214 close $out
215 set in [open $testfile]
216 fconfigure $in -translation binary
217 set x [read $in]
218 close $in
219 removeFile $testfile
220 set x
221 } "$bindata$binurl"
222
223 proc myProgress {token total current} {
224 global progress httpLog
225 if {[info exists httpLog] && $httpLog} {
226 puts "progress $total $current"
227 }
228 set progress [list $total $current]
229 }
230 if 0 {
231 # This test hangs on Windows95 because the client never gets EOF
232 set httpLog 1
233 test httpold-4.6 {httpEvent} {
234 set token [http_get $url -blocksize 50 -progress myProgress]
235 set progress
236 } {111 111}
237 }
238 test httpold-4.7 {httpEvent} {
239 set token [http_get $url -progress myProgress]
240 set progress
241 } {111 111}
242 test httpold-4.8 {httpEvent} {
243 set token [http_get $url]
244 http_status $token
245 } {ok}
246 test httpold-4.9 {httpEvent} {
247 set token [http_get $url -progress myProgress]
248 http_code $token
249 } {HTTP/1.0 200 Data follows}
250 test httpold-4.10 {httpEvent} {
251 set token [http_get $url -progress myProgress]
252 http_size $token
253 } {111}
254 test httpold-4.11 {httpEvent} {
255 set token [http_get $url -timeout 1 -command {#}]
256 http_reset $token
257 http_status $token
258 } {reset}
259 test httpold-4.12 {httpEvent} -body {
260 set tout {}
261 update
262 set x {}
263 set token [http_get $url?delay=500 -timeout 1 -command {lappend x fail}]
264 set i 0; while {$x eq {} && [incr i] < 50} {
265 set tout [after 20 {set x progress}]
266 vwait x
267 if {$x ne "progress"} break
268 set x [http_status $token]
269 }
270 set x
271 } -cleanup {
272 if {$tout ne {}} {after cancel $tout}
273 } -result timeout
274
275 test httpold-5.1 {http_formatQuery} {
276 http_formatQuery name1 value1 name2 "value two"
277 } {name1=value1&name2=value+two}
278
279 test httpold-5.2 {http_formatQuery} {
280 http_formatQuery name1 ~bwelch name2 \xa1\xa2\xa2
281 } {name1=%7ebwelch&name2=%a1%a2%a2}
282
283 test httpold-5.3 {http_formatQuery} {
284 http_formatQuery lines "line1\nline2\nline3"
285 } {lines=line1%0d%0aline2%0d%0aline3}
286
287 test httpold-6.1 {httpProxyRequired} {
288 update
289 http_config -proxyhost ${::HOST} -proxyport $port
290 set token [http_get $url]
291 http_wait $token
292 http_config -proxyhost {} -proxyport {}
293 upvar #0 $token data
294 set data(body)
295 } "<html><head><title>HTTP/1.0 TEST</title></head><body>
296 <h1>Hello, World!</h1>
297 <h2>GET http://$url</h2>
298 </body></html>"
299
300 # cleanup
301 catch {unset url}
302 catch {unset port}
303 catch {unset data}
304 close $listen
305 ::tcltest::cleanupTests
306 return