instaweb-test.ss
;;;
;;; Time-stamp: <2006-07-12 16:07:52 noel>
;;;
;;; Copyright (C) by Noel Welsh.
;;;

;;; This library is free software; you can redistribute it
;;; and/or modify it under the terms of the GNU Lesser
;;; General Public License as published by the Free Software
;;; Foundation; either version 2.1 of the License, or (at
;;; your option) any later version.

;;; This library is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the
;;; implied warranty of MERCHANTABILITY or FITNESS FOR A
;;; PARTICULAR PURPOSE.  See the GNU Lesser General Public
;;; License for more details.

;;; You should have received a copy of the GNU Lesser
;;; General Public License along with this library; if not,
;;; write to the Free Software Foundation, Inc., 59 Temple
;;; Place, Suite 330, Boston, MA 02111-1307 USA

;;; Author: Noel Welsh <noelwelsh@yahoo.com>
;;
;;
;; Commentary:

(module instaweb-test mzscheme

  (require (planet "test.ss" ("schematics" "schemeunit.plt" 2)))
  (require (lib "etc.ss")
           (lib "url.ss" "net")
           (planet "port.ss" ("schematics" "port.plt" 1))
           "instaweb.ss")

  (provide instaweb-tests)

  (define (make-dummy)
    (with-output-to-file "dummy.ss"
      (lambda ()
        (write
         '(module dummy mzscheme
            (require (lib "response-structs.ss" "web-server"))
            (provide interface-version timeout start)
            (define interface-version 'v1)
            (define timeout +inf.0)
            (define (start initial-request)
              (make-response/full
               200
               "OK"
               (current-seconds)
               #"text/plain"
               '()
               '("foo\r\n"))))))
      'replace))

  (define here (this-expression-source-directory))
  
  (define instaweb-tests
    (test-suite
     "All tests for instaweb"

     (test-case
      "Server listens on specified port and IP"
      (before
       (make-dummy)
       (let ((server
              (thread
               (lambda ()
                 (instaweb "dummy.ss" 4567 "127.0.0.1")))))
         (sleep 1)
         (let ((content
                (get-pure-port
                 (string->url "http://127.0.0.1:4567/servlets/dummy.ss"))))
           (check string=? (port->string content) "foo\r\n"))
         ;; Check the server is not listening on other ports
         (check-exn
          exn:fail:network?
          (lambda ()
            (get-pure-port
             (string->url "http://127.0.0.1:8123/servlets/dummy.ss"))))
         (kill-thread server))))

     (test-case
      "Instaweb stops reading if input port returns eof"
      (before
       (make-dummy)
       (let* ((op (open-output-string))
              (server
               (thread
                (lambda ()
                  (parameterize
                    ((current-input-port (open-input-string ""))
                     (current-output-port op))
                    (instaweb "dummy.ss" 4567 "127.0.0.1"))))))
         (sleep 1)
         (kill-thread server)
         (let ((content (get-output-string op)))
           (display content)
           (check <=
                  (string-length content)
                  218)))))

     (test-case
      "instaweb-here forms parameterize current-directory correctly"
      (parameterize
        ((current-directory "/"))
        (let/ec ec
          (instaweb-here
           (begin (check equal? (current-directory) here)
                  (ec #t))))
        (let/ec ec
          (instaweb-here
           (begin (check equal? (current-directory) here)
                  (ec #t))
           8000))
        (let/ec ec
          (instaweb-here
           (begin (check equal? (current-directory) here)
                  (ec #t))
           8000
           "127.0.0.1"))))
     ))
  )