root/lang/perl/HTTP-Engine/trunk/t/020_interface/test_upload.t @ 16873

Revision 16873, 2.5 kB (checked in by yappo, 5 years ago)

add copy_to and link_to method tests

Line 
1use strict;
2use warnings;
3use HTTP::Engine;
4use HTTP::Headers;
5use HTTP::Request;
6use Test::Base;
7use File::Temp qw( tempdir );
8use File::Spec;
9
10plan tests => 3*blocks;
11
12filters {
13    response => [qw/chop/],
14};
15
16run {
17    my $block = shift;
18    my $test;
19    my $body;
20
21    if ($block->request && exists $block->request->{method} && $block->request->{method} eq 'POST') {
22        delete $block->request->{method};
23        $body = delete $block->request->{body};
24        my $content = delete $block->request->{content};
25        $content =~ s/\r\n/\n/g;
26        $content =~ s/\n/\r\n/g;
27        $test = HTTP::Request->new( POST => 'http://localhost/', HTTP::Headers->new( %{ $block->request } ), $content );
28    } else {
29        $test = HTTP::Request->new( GET => 'http://localhost/');
30    }
31
32    my $upload;
33    my $response = HTTP::Engine->new(
34        interface => {
35            module => 'Test',
36            request_handler => sub {
37                my $c = shift;
38                $c->res->header( 'X-Req-Base' => $c->req->base );
39                $c->res->body('OK!');
40                return unless $body;
41
42                return unless $upload = $c->req->upload('test_upload_file');
43                my $upload_body = $upload->slurp;
44                unless ($body eq $upload_body) {
45                    $c->res->body('NG');
46                }
47            },
48        },
49    )->run($test);
50
51    $response->headers->remove_header('Date');
52    my $data = $response->headers->as_string."\n".$response->content;
53    is $data, $block->response;
54
55    unless ($upload) {
56        ok 1;
57        ok 1;
58        return;
59    };
60
61    my $tmpdir = tempdir( CLEANUP => 1 );
62    is slurp( copy => $tmpdir => $upload ), $body;
63    is slurp( link => $tmpdir => $upload ), $body;
64};
65
66sub slurp {
67    my($action, $tmpdir, $upload) = @_;
68    my $method = "${action}_to";
69    my $path = File::Spec->catfile( $tmpdir, $action );
70    $upload->$method($path);
71    open my $fh, '<', $path or die $!;
72    eval { local $/; <$fh> };
73}
74
75sub crlf {
76    my $in = shift;
77    $in =~ s/\n/\r\n/g;
78    $in;
79}
80
81__END__
82
83===
84--- request yaml
85method: POST
86content: |
87  ------BOUNDARY
88  Content-Disposition: form-data; name="test_upload_file"; filename="yappo.txt"
89  Content-Type: text/plain
90 
91  SHOGUN
92  ------BOUNDARY--
93Content-Type: multipart/form-data; boundary=----BOUNDARY
94Content-Length: 149
95body: SHOGUN
96--- response
97Content-Length: 3
98Content-Type: text/html
99Status: 200
100X-Req-Base: http://localhost/
101
102OK!
103===
104--- resquest
105--- response
106Content-Length: 3
107Content-Type: text/html
108Status: 200
109X-Req-Base: http://localhost/
110
111OK!
Note: See TracBrowser for help on using the browser.