From 539ef08bada07082139de6a0235128b37e2aa845 Mon Sep 17 00:00:00 2001 From: Steven Jenkin Date: Tue, 1 Nov 2022 11:46:40 +0000 Subject: [PATCH] Add tests to boost coverage --- t/mailform.t | 74 ++++++++++++++++++++++++++++++++++++++++++++++++- t/run_as_psgi.t | 34 +++++++++++++++++------ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/t/mailform.t b/t/mailform.t index 3312854..c70c333 100644 --- a/t/mailform.t +++ b/t/mailform.t @@ -1,4 +1,9 @@ -use Test::More tests => 4; +use Test::More tests => 11; + +# Need CGI.pm for tests +use CGI; + + SKIP: { # Check for Net::SMTP @@ -20,3 +25,70 @@ SKIP: { # Did it inherit the run method? can_ok($mf, qw/run/); } + +# Instantiate CGI::Application::Mailform +# run() CGI::Application::Mailform object. +# Expect redirect header + body +{ + my $app = CGI::Application::Mailform->new(); + isa_ok($app, 'CGI::Application::Mailform'); + + $app->query(CGI->new("")); + $app->param(MAIL_FROM=>'a'); + $app->param(MAIL_TO => 'b'); + $app->param(HTMLFORM_REDIRECT_URL => 'redirect_url'); + $app->param(SUCCESS_REDIRECT_URL => 'd'); + $app->param(FORM_FIELDS => []); + + + response_like( + $app, + qr/^Status: 302/, + qr/Continue: redirect_url<\/a>/, + 'TestApp, redirect_test' + ); +} + +# Instantiate CGI::Application::Mailform with missing params +# run() CGI::Application::Mailform object. +# Expect error +{ + my $app = CGI::Application::Mailform->new(); + isa_ok($app, 'CGI::Application::Mailform'); + + $app->query(CGI->new("")); + + my $output; + eval { $output = $app->run; }; + + like( $@, qr/Missing or invalid required parameters/, "Missing paramters"); +} + +# Instantiate CGI::Application::Mailform with invalid params +# run() CGI::Application::Mailform object. +# Expect error +{ + my $app = CGI::Application::Mailform->new(); + isa_ok($app, 'CGI::Application::Mailform'); + + $app->query(CGI->new("")); + $app->param(MAIL_FROM=>''); + $app->param(FORM_FIELDS => 'not an arrayref'); + + + my $output; + eval { $output = $app->run; }; + + like( $@, qr/Missing or invalid required parameters/, "Missing paramters"); +} + + +sub response_like { + my ($app, $header_re, $body_re, $comment) = @_; + + local $ENV{CGI_APP_RETURN_ONLY} = 1; + my $output = $app->run; + my ($header, $body) = split /\r\n\r\n/m, $output; + like($header, $header_re, "$comment (header match)"); + like($body, $body_re, "$comment (body match)"); +} diff --git a/t/run_as_psgi.t b/t/run_as_psgi.t index 9477511..c5eda9c 100644 --- a/t/run_as_psgi.t +++ b/t/run_as_psgi.t @@ -35,14 +35,32 @@ sub setup { } package main; +# Run as PSGI Application directly +{ + my $app = TestApp->new( QUERY => CGI::PSGI->new($env) ); -my $app = TestApp->new( QUERY => CGI::PSGI->new($env) ); + my $response = $app->run_as_psgi; -my $response = $app->run_as_psgi; + is_deeply $response, [ + '200', + [ 'Content-Type' => 'text/html; charset=ISO-8859-1' ], + [ 'Hello World' ], + ], + "run_as_psgi: reality check basic response"; +} + +# Retrieve the PGSI app as a subroutine +{ + my $app = TestApp->new( QUERY => CGI::PSGI->new($env) ); + + my $psgi_app = $app->psgi_app; -is_deeply $response, [ - '200', - [ 'Content-Type' => 'text/html; charset=ISO-8859-1' ], - [ 'Hello World' ], -], -"run_as_psgi: reality check basic response"; + my $response = $psgi_app->(); + + is_deeply $response, [ + '200', + [ 'Content-Type' => 'text/html; charset=ISO-8859-1' ], + [ 'Hello World' ], + ], + "run_as_psgi: reality check basic response"; +}