[racket] Testing Servlets

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Sun Jul 24 17:02:24 EDT 2011

At RacketCon today, someone asked about good ways to test servlets. I
said that I had something I used for the Web server test suite, but
that it wasn't suited for public consumption. After making that
statement, I got it ready for the public. I just pushed the new
libraries: web-server/test and xml/path

Here's a little example:

(define (test-add-two-numbers -s>)
  (define x (random 500))
  (define xs (string->bytes/utf-8 (number->string x)))
  (define y (random 500))
  (define ys (string->bytes/utf-8 (number->string y)))

  (define r0 (-s>))
  (define k0 (se-path* '(form #:action) r0))
  (define i0 (se-path* '(form input #:name) r0))
  (define r1
    (-s> (format "~a?~a=~a" k0 i0 xs)
         (list (make-binding:form (string->bytes/utf-8 i0) xs))))
  (define k1 (se-path* '(form #:action) r1))
  (define i1 (se-path* '(form input #:name) r1))
  (define r2
    (-s> (format "~a?~a=~a" k1 i1 ys)
         (list (make-binding:form (string->bytes/utf-8 i1) ys))))
  (define n (se-path* '(p) r2))
  (check-equal? n
                (format "The answer is ~a" (+ x y))))

(require
 (prefix-in ex:add1: web-server/default-web-root/htdocs/servlets/examples/add)
 (prefix-in ex:add2:
web-server/default-web-root/htdocs/servlets/examples/add-v2))

(test-add-two-numbers
 (make-servlet-tester ex:add1:start))
(test-add-two-numbers
 (make-servlet-tester ex:add2:start))

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.