Catalyst-Runtime

 view release on metacpan or  search on metacpan

t/psgi_utils.t  view on Meta::CPAN

use warnings;
use strict;

# Make it easier to mount PSGI apps under catalyst

my $psgi_app = sub {
  my $req = Plack::Request->new(shift);
  return [200,[],[$req->path]];
};

{
  package MyApp::PSGIObject;

  sub as_psgi {
    return [200, ['Content-Type' => 'text/plain'], ['as_psgi']];
  };

  package MyApp::Controller::Docs;
  $INC{'MyApp/Controller/Docs.pm'} = __FILE__;

  use base 'Catalyst::Controller';
  use Plack::Request;
  use Catalyst::Utils;

  sub as_psgi :Local {
    my ($self, $c) = @_;
    my $as_psgi = bless +{}, 'MyApp::PSGIObject';
    $c->res->from_psgi_response($as_psgi);
  }

  sub name :Local {
    my ($self, $c) = @_;
    my $env = $c->Catalyst::Utils::env_at_action;
    $c->res->from_psgi_response(
      $psgi_app->($env));

  }

  sub name_args :Local Args(1) {
    my ($self, $c, $arg) = @_;
    my $env = $c->Catalyst::Utils::env_at_action;
    $c->res->from_psgi_response(
      $psgi_app->($env));
  }

  sub filehandle :Local {
    my ($self, $c, $arg) = @_;
    my $path = File::Spec->catfile('t', 'utf8.txt');
    open(my $fh, '<', $path) || die "trouble: $!";
    $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], $fh]);
  }

  sub direct :Local {
    my ($self, $c, $arg) = @_;
    $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], ["hello","world"]]);
  }

  sub streaming_body :Local {
    my ($self, $c) = @_;
    my $psgi_app = sub {
        my $respond = shift;
        my $writer = $respond->([200,["Content-Type" => "text/plain"]]);
        $writer->write("body");
        $writer->close;
    };
    $c->res->from_psgi_response($psgi_app);
  }
  sub streaming_body_with_charset :Local {
    my ($self, $c) = @_;
    my $psgi_app = sub {
        my $respond = shift;
        my $writer = $respond->([200,["Content-Type" => "text/plain; charset=utf-8"]]);
        $writer->write("body");
        $writer->close;
    };
    #$c->clear_encoding;
    $c->res->from_psgi_response($psgi_app);
  }

  package MyApp::Controller::User;
  $INC{'MyApp/Controller/User.pm'} = __FILE__;

  use base 'Catalyst::Controller';
  use Plack::Request;
  use Catalyst::Utils;

  sub local_example :Local {
    my ($self, $c) = @_;
    my $env = $self->get_env($c);
    $c->res->from_psgi_response(
      $psgi_app->($env));
  }

  sub local_example_args1 :Local Args(1) {
    my ($self, $c) = @_;
    my $env = $self->get_env($c);
    $c->res->from_psgi_response(
      $psgi_app->($env));
  }

  sub path_example :Path('path-example') {
    my ($self, $c) = @_;
    my $env = $self->get_env($c);
    $c->res->from_psgi_response(
      $psgi_app->($env));
  }

  sub path_example_args1 :Path('path-example-args1') {
    my ($self, $c) = @_;
    my $env = $self->get_env($c);
    $c->res->from_psgi_response(
      $psgi_app->($env));
  }

  sub chained :Chained(/) PathPrefix CaptureArgs(0) { }

    sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}

      sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
      {
        my ($self, $c) = @_;
        my $env = $self->get_env($c);
        $c->res->from_psgi_response(
          $psgi_app->($env));
      }

  sub mounted :Local Args(1) {
    my ($self, $c, $arg) = @_;

t/psgi_utils.t  view on Meta::CPAN

  is $c->action, 'user/end_chain';
  is $res->content, '/user/end_chain/444';
  is_deeply $c->req->args, [444];
}

{
  my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
  is $c->action, 'user/end_chain';
  is $res->content, '/end_chain/444';
  is_deeply $c->req->args, [444];
}

{
  my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
  is $c->action, 'user/end_chain';
  is $res->content, '/444';
  is_deeply $c->req->args, [444];
}

{
  my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
  is $c->action, 'user/end_chain';
  is $res->content, '/';
  is_deeply $c->req->args, [444];
}

{
  my ($res, $c) = ctx_request('/docs/name');
  is $c->action, 'docs/name';
  is $res->content, '/';
  is_deeply $c->req->args, [];
}

{
  my ($res, $c) = ctx_request('/docs/name/111/222');
  is $c->action, 'docs/name';
  is $res->content, '/111/222';
  is_deeply $c->req->args, [111,222];
}

{
  my ($res, $c) = ctx_request('/docs/name_args/111');
  is $c->action, 'docs/name_args';
  is $res->content, '/111';
  is_deeply $c->req->args, [111];
}

{
  use utf8;
  use Encode;
  my ($res, $c) = ctx_request('/docs/filehandle');
  is Encode::decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
}

{
  my ($res, $c) = ctx_request('/docs/direct');
  is $res->content, "helloworld";
}

{
  my ($res, $c) = ctx_request('/docs/streaming_body');
  is $res->content, "body";
}
{
  my ($res, $c) = ctx_request('/docs/streaming_body_with_charset');
  is $res->content, "body";
}

done_testing();



( run in 1.383 second using v1.01-cache-2.11-cpan-39bf76dae61 )