1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/hunchentoot/test/test.lisp,v 1.24 2008/03/06 07:46:53 edi Exp $
4 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :hunchentoot-test
)
32 (defun file-length-string (pathname)
33 (with-open-file (f pathname
)
34 (princ-to-string (file-length f
))))
36 (defun say (fmt &rest args
)
38 (apply #'format t fmt args
)
41 (defun test-hunchentoot (base-url &key
(make-cookie-jar
43 (make-instance 'drakma
:cookie-jar
))))
44 "Runs the built-in confidence test. BASE-URL is the base URL to use
45 for testing, it should not have a trailing slash. The keyword
46 arguments accepted are for future extension and should not currently
49 The script expects the Hunchentoot example test server to be running
50 at the given BASE-URL and retrieves various pages from that server,
51 expecting certain responses."
52 (with-script-context (:base-url
(format nil
"~A/hunchentoot/test/" base-url
))
54 (say "Request home page")
56 (http-assert 'status-code
200)
57 (http-assert-header :content-type
"^text/html")
60 (let ((cookie-jar (funcall make-cookie-jar
)))
61 (http-request "cookie.html" :cookie-jar cookie-jar
)
62 (http-request "cookie.html" :cookie-jar cookie-jar
)
63 (http-assert-body "(?ms)COOKIE-IN "pumpkin".*"barking""))
65 (say "Test session variables")
66 (let ((cookie-jar (funcall make-cookie-jar
)))
67 (http-request "session.html" :cookie-jar cookie-jar
68 :method
:post
:parameters
'(("new-foo-value" .
"ABC") ("new-bar-value" .
"DEF")))
69 (http-request "session.html" :cookie-jar cookie-jar
)
70 ;; These assertions assume that SESSION-VALUE returns the found alist value as second value
71 (http-assert-body "\(HUNCHENTOOT-TEST::FOO . "ABC"\)")
72 (http-assert-body "\(HUNCHENTOOT-TEST::BAR . "DEF"\)"))
74 (say "Test GET parameters with foreign characters (Latin-1)")
75 (http-request "parameter_latin1_get.html?foo=H%FChner")
76 (http-assert-header :content-type
"text/html; charset=ISO-8859-1")
77 (http-assert-body "(72 252 104 110 101 114)")
78 (http-assert-body ""Hühner"")
80 (say "Test POST parameters with foreign characters (Latin-1)")
81 (http-request "parameter_latin1_post.html"
82 :method
:post
:parameters
(list (cons "foo" (format nil
"H~Chner" #.
(code-char 252)))))
83 (http-assert-header :content-type
"text/html; charset=ISO-8859-1")
84 (http-assert-body "(72 252 104 110 101 114)")
85 (http-assert-body ""Hühner"")
87 (say "Test GET parameters with foreign characters (UTF-8)")
88 (http-request "parameter_utf8_get.html?foo=H%C3%BChner")
89 (http-assert-header :content-type
"text/html; charset=UTF-8")
90 (http-assert-body "(72 252 104 110 101 114)")
91 (http-assert-body ""Hühner"")
93 (say "Test POST parameters with foreign characters (UTF-8)")
94 (http-request "parameter_utf8_post.html"
96 :external-format-out
:utf-8
97 :parameters
(list (cons "foo" (format nil
"H~Chner" #.
(code-char 252)))))
98 (http-assert-header :content-type
"text/html; charset=UTF-8")
99 (http-assert-body "(72 252 104 110 101 114)")
100 (http-assert-body ""Hühner"")
102 (say "Test redirection")
103 (http-request "redir.html")
104 (http-assert 'uri
(lambda (uri)
105 (matches (princ-to-string uri
) "info.html\\?redirected=1")))
107 (say "Test authorization")
108 (http-request "authorization.html")
109 (http-assert 'status-code
401)
110 (http-request "authorization.html"
111 :basic-authorization
'("nanook" "igloo"))
112 (http-assert 'status-code
200)
114 (say "Request the Zappa image")
115 (http-request "image.jpg")
116 (http-assert-header :content-length
(file-length-string #P
"fz.jpg"))
117 (http-assert-header :content-type
"image/jpeg")
118 (http-assert 'body
(complement #'mismatch
) (file-contents #P
"fz.jpg"))
120 (say "Request the Zappa image from RAM")
121 (http-request "image-ram.jpg")
122 (http-assert-header :content-length
(file-length-string #P
"fz.jpg"))
123 (http-assert-header :content-type
"image/jpeg")
124 (http-assert 'body
(complement #'mismatch
) (file-contents #P
"fz.jpg"))
126 (say "Upload a file")
127 (http-request "upload.html"
128 :method
:post
:parameters
'(("clean" .
"doit")))
129 (http-request "upload.html"
130 :method
:post
:parameters
'(("file1" #P
"fz.jpg")))
131 (http-request "upload.html")
132 (http-assert-body (format nil
"fz.jpg.*>~A Bytes" (file-length-string #P
"fz.jpg")))
136 (let* ((range-test-file-size (* 256 1024)) ; large enough to have hunchentoot use multiple buffers when reading back data, should be aligned to 1024
137 (range-test-buffer (make-array range-test-file-size
:element-type
'(unsigned-byte 8)))
138 (uploaded-file-url "files/?path=user-stream") ; The uploaded file will appear under the name "user-stream" in hunchentoot
139 (expected-content-range (format nil
"bytes 0-~D/*" range-test-file-size
)))
141 (dotimes (i range-test-file-size
)
142 (setf (aref range-test-buffer i
) (random 256)))
144 (flex:with-input-from-sequence
(upload-stream range-test-buffer
)
145 (http-request "upload.html"
146 :method
:post
:parameters
`(("file1" ,upload-stream
))))
148 (say " Request the uploaded file, verify contents")
149 (http-request uploaded-file-url
)
150 (http-assert-header :content-length
(princ-to-string range-test-file-size
))
151 (http-assert 'body
(complement #'mismatch
) range-test-buffer
)
153 (say " Verify responses to partial requests")
155 (say " Request just one byte")
156 (http-request uploaded-file-url
:range
'(0 0))
157 (http-assert 'status-code
206)
158 (http-assert 'body
'equalp
(subseq range-test-buffer
0 1))
159 (http-assert-header :content-range expected-content-range
)
161 (say " End out of range")
162 (http-request uploaded-file-url
:range
(list 0 range-test-file-size
))
163 (http-assert 'status-code
416)
164 (http-assert-header :content-range expected-content-range
)
166 (say " Request whole file as partial")
167 (http-request uploaded-file-url
:range
(list 0 (1- range-test-file-size
)))
168 (http-assert 'status-code
206)
169 (http-assert 'body
'equalp range-test-buffer
)
170 (http-assert-header :content-range expected-content-range
)
172 (say " Request something in the middle")
173 (let ((start-offset (/ range-test-file-size
4))
174 (length (/ range-test-file-size
2)))
175 (http-request uploaded-file-url
:range
(list start-offset
(1- length
)))
176 (http-assert 'status-code
206)
177 (http-assert 'body
'equalp
(subseq range-test-buffer start-offset length
))
178 (http-assert-header :content-range expected-content-range
)))