Add PerlIO methods to Catalyst::Response, so that it more properly
[catagits/Catalyst-Runtime.git] / t / lib / TestApp / Controller / Engine / Response / PerlIO.pm
CommitLineData
c9373e69 1package TestApp::Controller::Engine::Response::PerlIO;
2
3use strict;
4use base 'Catalyst::Controller';
5
6sub zip : Relative {
7 my ( $self, $c ) = @_;
8
9 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
10
11 my $data1 = 'x' x (100 * 1024);
12 #my $data2 = join '', map { chr($_) } (0..65535);
13 my $data2 = join('', map { chr($_) } (0..255)) x 256;
14
15 my $zip = new Archive::Zip;
16 $zip->addString(\$data1, 'x.txt', COMPRESSION_LEVEL_BEST_COMPRESSION);
17 #$zip->addString(\$data2, 'utf16.txt', COMPRESSION_LEVEL_BEST_COMPRESSION); ### Needs better support in Archive::Zip first... ###
18 $zip->addString(\$data1, 'ASCII.txt', COMPRESSION_LEVEL_BEST_COMPRESSION);
19
20 unless ($zip->writeToFileHandle($c->response, 0) == AZ_OK) {
21 Catalyst::Exception->throw("ZIP Write Error!");
22 }
23}
24
25sub csv : Relative {
26 my ( $self, $c ) = @_;
27
28 use Text::CSV;
29
30 my $csv = Text::CSV->new({ eol => "\n" });
31 my $csv_doc = [
32 [qw/Cnt Item Price/],
33 [1, "Box of Ritz Crackers", '$2.55'],
34 [1, "Cheese Whiz", '$1.22'],
35 [5, "Banana (single)", '$ .40'],
36 ];
37
38 while (my $row = shift @$csv_doc) {
39 $csv->print($c->response, $row) || Catalyst::Exception->throw("CSV Write Error!");
40 }
41}
42
43sub xml : Relative {
44 my ( $self, $c ) = @_;
45
46 use XML::Simple;
47
48 my $xs = XML::Simple->new(
49 XMLDecl => 1,
50 KeepRoot => 1,
51 OutputFile => $c->response,
52 );
53 $xs->xml_out({
54 geocode => {
55 results => [
56 {
57 address_components => [
58 {
59 long_name => "1600",
60 short_name => "1600",
61 types => [ "street_number" ]
62 },
63 {
64 long_name => "Amphitheatre Pkwy",
65 short_name => "Amphitheatre Pkwy",
66 types => [ "route" ]
67 },
68 {
69 long_name => "Mountain View",
70 short_name => "Mountain View",
71 types => [ "locality", "political" ]
72 },
73 {
74 long_name => "Santa Clara",
75 short_name => "Santa Clara",
76 types => [ "administrative_area_level_2", "political" ]
77 },
78 {
79 long_name => "California",
80 short_name => "CA",
81 types => [ "administrative_area_level_1", "political" ]
82 },
83 {
84 long_name => "United States",
85 short_name => "US",
86 types => [ "country", "political" ]
87 },
88 {
89 long_name => "94043",
90 short_name => "94043",
91 types => [ "postal_code" ]
92 }
93 ],
94 formatted_address => "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
95 geometry => {
96 location => {
97 lat => 37.42109430,
98 lng => -122.08525150
99 },
100 location_type => "ROOFTOP",
101 viewport => {
102 northeast => {
103 lat => 37.42244328029150,
104 lng => -122.0839025197085
105 },
106 southwest => {
107 lat => 37.41974531970850,
108 lng => -122.0866004802915
109 }
110 }
111 },
112 types => [ "street_address" ]
113 }
114 ],
115 status => "OK",
116 source => 'http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'
117 },
118 }) || Catalyst::Exception->throw("XML Write Error!");
119}
120
1211;