File: body_fh.t

package info (click to toggle)
libcatalyst-perl 5.90124-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,992 kB
  • sloc: perl: 11,123; makefile: 7
file content (128 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use warnings;
use strict;
use Test::More;
use HTTP::Request::Common;
use HTTP::Message::PSGI;
use Plack::Util;

# Test case to check that we now send scalar and filehandle like
# bodys directly to the PSGI engine, rather than call $writer->write
# or unroll the filehandle ourselves.

{
  package MyApp::Controller::Root;

  use base 'Catalyst::Controller';

  sub flat_response :Local {
    my $response = 'Hello flat_response';
    pop->res->body($response);
  }

  sub memory_stream :Local {
    my $response = 'Hello memory_stream';
    open my $fh, '<', \$response || die "$!";

    pop->res->body($fh);
  }

  sub manual_write_fh :Local {
    my ($self, $c) = @_;
    my $response = 'Hello manual_write_fh';
    my $writer = $c->res->write_fh;
    $writer->write($response);
    $writer->close;
  }

  sub manual_write :Local {
    my ($self, $c) = @_;
    $c->res->write('Hello');
    $c->res->body('manual_write');
  }

  $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...

  package MyApp;
  use Catalyst;

}



ok(MyApp->setup);
ok(my $psgi = MyApp->psgi_app);

{
  ok(my $env = req_to_psgi(GET '/root/flat_response'));
  ok(my $psgi_response = $psgi->($env));

  $psgi_response->(sub {
    my $response_tuple = shift;
    my ($status, $headers, $body) = @$response_tuple;

    ok $status;
    ok $headers;
    is $body->[0], 'Hello flat_response';

   });
}

{
  ok(my $env = req_to_psgi(GET '/root/memory_stream'));
  ok(my $psgi_response = $psgi->($env));

  $psgi_response->(sub {
    my $response_tuple = shift;
    my ($status, $headers, $body) = @$response_tuple;

    ok $status;
    ok $headers;
    is ref($body), 'GLOB';

  });
}

{
  ok(my $env = req_to_psgi(GET '/root/manual_write_fh'));
  ok(my $psgi_response = $psgi->($env));

  $psgi_response->(sub {
    my $response_tuple = shift;
    my ($status, $headers, $body) = @$response_tuple;

    ok $status;
    ok $headers;
    ok !$body;

    return Plack::Util::inline_object(
        write => sub { is shift, 'Hello manual_write_fh' },
        close => sub { ok 1, 'closed' },
      );
  });
}

{
  ok(my $env = req_to_psgi(GET '/root/manual_write'));
  ok(my $psgi_response = $psgi->($env));

  $psgi_response->(sub {
    my $response_tuple = shift;
    my ($status, $headers, $body) = @$response_tuple;

    ok $status;
    ok $headers;
    ok !$body;

    my @expected = (qw/Hello manual_write/);
    return Plack::Util::inline_object(
        close => sub { ok 1, 'closed'; is scalar(@expected), 0; },
        write => sub { is shift, shift(@expected) },
      );
  });
}

## We need to specify the number of expected tests because tests that live
## in the callbacks might never get run (thus all ran tests pass but not all
## required tests run).

done_testing(28);