Skip to content

Instantly share code, notes, and snippets.

@moznion
Created November 3, 2013 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moznion/7291445 to your computer and use it in GitHub Desktop.
Save moznion/7291445 to your computer and use it in GitHub Desktop.
#!perl
use strict;
use warnings;
use utf8;
use HTTP::Request::Common;
use Plack::Request;
use Plack::Test;
use Test::More;
use Test::JsonAPI::Autodoc;
BEGIN {
$ENV{TEST_JSONAPI_AUTODOC} = 1;
}
# Plack App
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
if ($req->path eq '/') {
return [ 200, [ 'Content-Type' => 'application/json' ], ['{ "message" : "Hello" }'] ];
}
elsif ($req->path eq '/foo') {
return [ 200, [ 'Content-Type' => 'application/json' ], ['{ "message" : "Goodbye" }'] ];
}
return [ 404, [ 'Content-Type' => 'text/plain' ], [ "Not found" ] ];
};
# Not use `test_psgi`
my $test_app = Plack::Test->create($app);
describe 'POST /' => sub {
my $req = POST '/';
$req->header('Content-Type' => 'application/json');
$req->content(q{
{
"id": 1,
"message": "blah blah"
}
});
plack_ok($test_app, $req, 200, "get message ok");
};
# Use `test_psgi`
test_psgi $app, sub {
my $cb = shift;
describe 'POST /foo' => sub {
my $req = POST '/foo';
$req->header('Content-Type' => 'application/json');
$req->content(q{
{
"id": 1,
"message": "blah blah"
}
});
plack_ok($cb, $req, 200, "get message ok");
};
};
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment