Fork me on GitHub Swatpm.org - Web Test Automation with Perl
SWAT - Web Test Automation With Perl. 5 Minutes Tutorial
Given some web app ... (see the example app below)

# Start the App
    $ plackup
    
# Define tested application URL
    $ echo '127.0.0.1:5000' > host
    
# Define tests

    # GET   /hello
    $ mkdir hello
    $ touch hello/get.txt             

    # GET   /foo/bar?greeting=hello
    $ mkdir -p foo/bar
    $ touch foo/bar/get.txt
    $ echo curl_params="'-G -d greeting=hello'" > foo/bar/swat.ini

    # The example app will return the wrong thing for this test
    # Resulting in a failure as seen below

    # POST  /bar/baz  ( with params: greeting=hello )
    $ mkdir -p bar/baz
    $ touch bar/baz/post.txt          
    $ echo curl_params="'-d greeting=hello'" > bar/baz/swat.ini

    # POST  /send_cookie ( with cookie: name=swat )
    $ mkdir send_cookie
    $ touch send_cookie/post.txt      
    $ echo curl_params="'-b name=swat'" > send_cookie/swat.ini
    
# Define expected response

    # GET  /hello should succeed and return 'Hi there!'
    $ echo 200 OK > hello/get.txt
    $ echo 'Hi there!' >> hello/get.txt

    # GET  /foo/bar should succeed and return my greeting
    $ echo 200 OK > foo/bar/get.txt
    $ echo 'I am get to /foo/bar. you just told me: hello' >> foo/bar/get.txt

    # POST /bar/baz should succeed and return my greeting
    $ echo 200 OK > bar/baz/post.txt
    $ cat bar/baz/post.txt
        200 OK
        within: I am post to \/bar\/baz
        you just told me: hello

    # POST /send_cookie  should succeed and return my cookie
    $ echo 200 OK > send_cookie/post.txt
    $ echo 'swat' >> send_cookie/post.txt
    
# Run swat tests
$ swat
/home/vagrant/.swat/.cache/13587/prove/send_cookie/POST/request.t ..                                               
ok 1 - 200 / 1 of 1 curl -X POST -k --connect-timeout 20 -m 20 -b name=swat -L -D - '127.0.0.1:5000/send_cookie'   
ok 2 - output match '200 OK'                                                                                       
ok 3 - output match 'swat'                                                                                         
1..3                                                                                                               
ok                                                                                                                 
/home/vagrant/.swat/.cache/13587/prove/foo/bar/GET/request.t .......                                               
ok 1 - 200 / 1 of 1 curl -X GET -k --connect-timeout 20 -m 20 -G -d greeting=hello -L -D - '127.0.0.1:5000/foo/bar'
ok 2 - output match '200 OK'                                                                                       
ok 3 - output match 'I am get to /foo/bar. you just told me: hello'                                                
1..3                                                                                                               
ok                                                                                                                 
/home/vagrant/.swat/.cache/13587/prove/hello/GET/request.t .........                                               
ok 1 - 200 / 1 of 1 curl -X GET -k --connect-timeout 20 -m 20 -L -D - '127.0.0.1:5000/hello'                       
ok 2 - output match 'Hi there!'                                                                                    
1..2                                                                                                               
ok                                                                                                                 
/home/vagrant/.swat/.cache/13587/prove/bar/baz/POST/request.t ......                                               
ok 1 - 200 / 1 of 1 curl -X POST -k --connect-timeout 20 -m 20 -d greeting=hello -L -D - '127.0.0.1:5000/bar/baz'  
ok 2 - output match '200 OK'                                                                                       
ok 3 - output match /I am post to \/bar\/baz/                                                                      
not ok 4 - 'I am post to /bar/baz. you just told me: goodbye' match 'you just told me: hello'                      
                                                                                                                   
#   Failed test ''I am post to /bar/baz. you just told me: goodbye' match 'you just told me: hello''               
#   at /usr/local/share/perl/5.20.2/swat.pm line 238.                                                              
1..4                                                                                                               
# Looks like you failed 1 test of 4.                                                                               
Dubious, test returned 1 (wstat 256, 0x100)                                                                        
Failed 1/4 subtests                                                                                                
                                                                                                                   
Test Summary Report                                                                                                
-------------------                                                                                                
/home/vagrant/.swat/.cache/13587/prove/bar/baz/POST/request.t    (Wstat: 256 Tests: 4 Failed: 1)                   
  Failed test:  4                                                                                                  
  Non-zero exit status: 1                                                                                          
Files=4, Tests=12,  0 wallclock secs ( 0.03 usr  0.00 sys +  0.21 cusr  0.01 csys =  0.25 CPU)                     
Result: FAIL                                                                                                       
# Application example

    $ cat app.psgi 

    #!/usr/bin/perl
    
    use Dancer2;
    
    get '/hello' => sub {
        return 'Hi there!'
    };

    get '/foo/bar' => sub {
        return "I am get to /foo/bar. you just told me: ".query_parameters->get('greeting')
    };

    # swat expects something else here, see the contents of bar/baz/post.txt
    
    post '/bar/baz' => sub {
        return "I am post to /bar/baz. you just told me: goodbye"
    };
    
    post '/send_cookie' => sub {
    
        my $cookie = cookies->{name};
        return $cookie->value;
    };
    
    dance;