<div class="gmail_quote">On Tue, Dec 28, 2010 at 8:32 PM, James Russo <span dir="ltr"><<a href="mailto:jr@halo3.net">jr@halo3.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"># create</div>
ok($response = request(<br>
POST('/appointment','content-type' => 'application/json', 'content' => to_json($foo)<br>
),<br>
"create new appointment"));<br>
<br>
Just wanted to put it out there in the archives for the next person.<br></blockquote><div><br></div><div>Again, my suggestion is you put that into a test class -- you probably don't want to type that dozens (if not hundreds) of times. And if your application grows you will likely want some kind of custom test framework that all your test files use. And If you work with other developers it's helpful to have a simple test framework that's easy for everyone to use and understand.</div>
<div><br></div><div>I use a generic test class that provides methods for making different types of request and decoding responses, provides a $schema object (as you need to modify data during a test), building and tearing down test data, and provides access to the app configuration. I can enable a debug option to dump full request and response and the class allows single-stepping through the tests. And with your REST-like methods you probably want to know more than than $response->is_success (which is what post_ok() checks). I then sub-class that for each application so I can add specific helper methods unique to that app. </div>
<div><br></div><div>So, tests end up something like:</div><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div class="gmail_quote"><div>my $test = Test::MyApp->new( debug_response => 0, dbic_trace => 0 );</div>
<div><br></div><div>my $user = $test->create_and_login_test_user;</div></div><div class="gmail_quote"><div><br></div></div><div class="gmail_quote"><div>$test->json_request( 201, POST => $path, \%data ); # OK if status == 201, and print message about request.</div>
</div><div class="gmail_quote"><div><br></div></div><div class="gmail_quote"><div>my $response = $test->request( 200, GET => $path ); # $response is HTTP::Response sub-class.</div><div><br></div></div><div class="gmail_quote">
<div>my $data = $response->decoded_content;</div></div><div class="gmail_quote"><div>$data->{foo} = 'bar'; # modify response data before PUT</div><div><br></div></div><div class="gmail_quote"><div>$test->json_request( 204, PUT => $path, $data ); </div>
</div></blockquote><div class="gmail_quote"><div><br></div><div>The *request methods allow passing an HTTP::Request object, too, so it's easy to customize a request for the rare times I need a custom header.</div><div>
<br></div><div><br></div><div><br></div></div>-- <br>Bill Moseley<br><a href="mailto:moseley@hank.org" target="_blank">moseley@hank.org</a><br>