Close SSL streams after processing connection.
[hunchentoot.git] / test / script.lisp
bloba63837840151446f5a60933877927dd44c5c5399
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
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.
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.
29 (in-package :hunchentoot-test)
31 (defun file-length-string (pathname)
32 (with-open-file (f pathname)
33 (princ-to-string (file-length f))))
35 (defun say (fmt &rest args)
36 (format t "; ")
37 (apply #'format t fmt args)
38 (terpri))
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.
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))
53 (say "Request home page")
54 (http-request "")
55 (http-assert 'status-code 200)
56 (http-assert-header :content-type "^text/html")
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""))
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"\)"))
73 (say "Test GET parameters with foreign characters (Latin-1)")
74 (http-request "parameter_latin1_get.html"
75 :external-format-out :iso-8859-1
76 :parameters (list (cons "foo" (format nil "H~Chner" #.(code-char 252))))
77 :additional-headers '(("Content-Type" . "text/plain; charset=iso-8859-1")))
78 (http-assert-header :content-type "(?i)text/html; charset=ISO-8859-1")
79 (http-assert-body "(72 252 104 110 101 114)")
80 (http-assert-body "(?i)"Hühner"")
82 (say "Test POST parameters with foreign characters (Latin-1)")
83 (http-request "parameter_latin1_post.html"
84 :external-format-out :iso-8859-1
85 :method :post :parameters (list (cons "foo" (format nil "H~Chner" #.(code-char 252)))))
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"")
90 (say "Test GET parameters with foreign characters (UTF-8)")
91 (http-request "parameter_utf8_get.html"
92 :external-format-out :utf-8
93 :parameters (list (cons "foo" (format nil "H~Chner" #.(code-char 252)))))
94 (http-assert-header :content-type "(?i)text/html; charset=UTF-8")
95 (http-assert-body "(72 252 104 110 101 114)")
96 (http-assert-body "(?i)"Hühner"")
98 (say "Test POST parameters with foreign characters (UTF-8)")
99 (http-request "parameter_utf8_post.html"
100 :method :post
101 :external-format-out :utf-8
102 :parameters (list (cons "foo" (format nil "H~Chner" #.(code-char 252)))))
103 (http-assert-header :content-type "(?i)text/html; charset=UTF-8")
104 (http-assert-body "(72 252 104 110 101 114)")
105 (http-assert-body "(?i)"Hühner"")
107 (say "Test redirection")
108 (http-request "redir.html")
109 (http-assert 'uri (lambda (uri)
110 (matches (princ-to-string uri) "info.html\\?redirected=1")))
112 (say "Test authorization")
113 (http-request "authorization.html")
114 (http-assert 'status-code 401)
115 (http-request "authorization.html"
116 :basic-authorization '("nanook" "igloo"))
117 (http-assert 'status-code 200)
119 (say "Request the Zappa image")
120 (http-request "image.jpg")
121 (http-assert-header :content-length (file-length-string #P"fz.jpg"))
122 (http-assert-header :content-type "image/jpeg")
123 (http-assert 'body (complement #'mismatch) (file-contents #P"fz.jpg"))
125 (say "Request the Zappa image from RAM")
126 (http-request "image-ram.jpg")
127 (http-assert-header :content-length (file-length-string #P"fz.jpg"))
128 (http-assert-header :content-type "image/jpeg")
129 (http-assert 'body (complement #'mismatch) (file-contents #P"fz.jpg"))
131 (say "Upload a file")
132 (http-request "upload.html"
133 :method :post :parameters '(("clean" . "doit")))
134 (http-request "upload.html"
135 :method :post :parameters '(("file1" #P"fz.jpg")))
136 (http-request "upload.html")
137 (http-assert-body (format nil "fz.jpg.*>~A Bytes" (file-length-string #P"fz.jpg")))
139 (say "Range tests")
140 (say " Upload file")
141 (let* ((range-test-file-size (* 256 1024)) ; large enough to have hunchentoot use multiple buffers when reading back data, should be aligned to 1024
142 (range-test-buffer (make-array range-test-file-size :element-type '(unsigned-byte 8)))
143 (uploaded-file-url "files/?path=user-stream")) ; The uploaded file will appear under the name "user-stream" in hunchentoot
145 (dotimes (i range-test-file-size)
146 (setf (aref range-test-buffer i) (random 256)))
148 (flex:with-input-from-sequence (upload-stream range-test-buffer)
149 (http-request "upload.html"
150 :method :post :parameters `(("file1" ,upload-stream))))
152 (say " Request the uploaded file, verify contents")
153 (http-request uploaded-file-url)
154 (http-assert-header :content-length (princ-to-string range-test-file-size))
155 (http-assert 'body (complement #'mismatch) range-test-buffer)
157 (say " Verify responses to partial requests")
159 (say " Request just one byte")
160 (http-request uploaded-file-url :range '(0 0))
161 (http-assert 'status-code 206)
162 (http-assert 'body 'equalp (subseq range-test-buffer 0 1))
163 (http-assert-header :content-range (format nil "bytes 0-0/~D" range-test-file-size))
165 (say " End out of range")
166 (http-request uploaded-file-url :range (list 0 range-test-file-size))
167 (http-assert 'status-code 416)
168 (http-assert-header :content-range (format nil "bytes 0-~D/~A" (1- range-test-file-size) range-test-file-size))
170 (say " Request whole file as partial")
171 (http-request uploaded-file-url :range (list 0 (1- range-test-file-size)))
172 (http-assert 'status-code 206)
173 (http-assert 'body 'equalp range-test-buffer)
174 (http-assert-header :content-range (format nil "bytes 0-~D/~D" (1- range-test-file-size) range-test-file-size))
176 (say " Request something in the middle")
177 (let ((start-offset (/ range-test-file-size 4))
178 (length (/ range-test-file-size 2)))
179 (http-request uploaded-file-url :range (list start-offset (1- length)))
180 (http-assert 'status-code 206)
181 (http-assert 'body 'equalp (subseq range-test-buffer start-offset length))
182 (http-assert-header :content-range (format nil "bytes ~D-~D/~D" start-offset (1- length) range-test-file-size))))
185 (values)))