| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | use HTTP::Engine; |
|---|
| 4 | use HTTP::Headers; |
|---|
| 5 | use HTTP::Request; |
|---|
| 6 | use Test::Base; |
|---|
| 7 | use File::Temp qw( tempdir ); |
|---|
| 8 | use File::Spec; |
|---|
| 9 | |
|---|
| 10 | plan tests => 3*blocks; |
|---|
| 11 | |
|---|
| 12 | filters { |
|---|
| 13 | response => [qw/chop/], |
|---|
| 14 | }; |
|---|
| 15 | |
|---|
| 16 | run { |
|---|
| 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 | |
|---|
| 66 | sub 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 | |
|---|
| 75 | sub crlf { |
|---|
| 76 | my $in = shift; |
|---|
| 77 | $in =~ s/\n/\r\n/g; |
|---|
| 78 | $in; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | __END__ |
|---|
| 82 | |
|---|
| 83 | === |
|---|
| 84 | --- request yaml |
|---|
| 85 | method: POST |
|---|
| 86 | content: | |
|---|
| 87 | ------BOUNDARY |
|---|
| 88 | Content-Disposition: form-data; name="test_upload_file"; filename="yappo.txt" |
|---|
| 89 | Content-Type: text/plain |
|---|
| 90 | |
|---|
| 91 | SHOGUN |
|---|
| 92 | ------BOUNDARY-- |
|---|
| 93 | Content-Type: multipart/form-data; boundary=----BOUNDARY |
|---|
| 94 | Content-Length: 149 |
|---|
| 95 | body: SHOGUN |
|---|
| 96 | --- response |
|---|
| 97 | Content-Length: 3 |
|---|
| 98 | Content-Type: text/html |
|---|
| 99 | Status: 200 |
|---|
| 100 | X-Req-Base: http://localhost/ |
|---|
| 101 | |
|---|
| 102 | OK! |
|---|
| 103 | === |
|---|
| 104 | --- resquest |
|---|
| 105 | --- response |
|---|
| 106 | Content-Length: 3 |
|---|
| 107 | Content-Type: text/html |
|---|
| 108 | Status: 200 |
|---|
| 109 | X-Req-Base: http://localhost/ |
|---|
| 110 | |
|---|
| 111 | OK! |
|---|