FAQ

On Tue, Dec 28, 2010 at 7:47 PM, James Russo wrote:
Hello,

? ? ? ?Does anyone there some examples of unit tests for a controller which is based on Catalyst::Controller::REST? There was some >discussion on the list a few years ago, and I think the conclusion was to use LWP directly and not Test::WWW::Mechanize::Catalyst. It >seems that using the methods do not give you access to passing actual body content and content-type headers? There are getting >overridden somewhere along the line. I'm going to continue to research it, but figured I'd ask here before I spend too much time on it.
Great discussion!

I use HTTP::Request::Common which I have found very convenient to test
my C::C::REST controllers, since GET, POST, PUT, etc. are exported
into your test file, and $rs->content works as expected. We have code
that returns both JSON and XML entities and we validate that against
the data in the DB, using the DBIC classes directly.

I have a small lib in my t/ directory which has a req sub that I have
pasted below. Then each test block look something like:

@auth = qw(
xxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyy
);
$rs = &req('PUT', '/xxxxx/idreq/0?test=1',
'text/xml', 'UTF-8',
@auth, "garbage", 'prettyxml');
$headers = $rs->headers;
ok($rs->code == 415, 'Validation Error on IDV')
or diag(&dump_rs($rs));

# get the resource from server
if($rs->header('location') =~ m[^http://.*/xxxx/idreq/(\d+)]){
$idreq_id = $1;
}
else{
BAIL_OUT("415 test did not return location for resource. Cannot continue.");
}
cmp_ok($idreq_id, '>', 0, "Returned correct url for resource $1");

$rs = &req('GET', "/xxxx/idreq/$idreq_id",
'text/xml', 'UTF-8',
@auth, undef, 'prettyxml');

[... retrieve test subject xml data...]

# see if it's really pretty xml
cmp_ok($rs->content, 'eq', $test_xml, 'Check for Pretty XML');


sub req {
my $method = shift;
my $uri = shift;
my $type = shift;
my $cenc = shift;
my $authu = shift;
my $authp = shift;
my $content = shift;
my $feature = shift;

my $r;
given($method){
when('POST'){
$r = POST $uri,
Content_Type => $type,
Content_Type_Charset => $cenc,
Accept => $type,
Accept_Charset => $cenc,
Accept_Feature => $feature,
Content => $content;
}
when('PUT'){
$r = PUT $uri,
Content_Type => $type,
Content_Type_Charset => $cenc,
Accept => $type,
Accept_Charset => $cenc,
Accept_Feature => $feature,
Content => $content;
}
when('GET'){
$r = GET $uri,
Accept => $type,
Accept_Charset => $cenc,
Accept_Feature => $feature,
}
}

$r->authorization_basic($authu,$authp);

my $rs = request($r);
return $rs;
}




Thanks,

-jr
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 8 | next ›
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedDec 29, '10 at 12:47a
activeJan 1, '11 at 4:16p
posts8
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase