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