File: 85-attachments-rt127607.t

package info (click to toggle)
librt-client-rest-perl 1%3A0.56-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • sloc: perl: 4,205; makefile: 9
file content (136 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
#!perl
#
# This script tests whether submited data looks good

use strict;
use warnings;

use Test::More;

use IO::File;
use IO::Pipe;
use RT::Client::REST;
use File::Spec::Functions;
use Encode;
use HTTP::Response;
use HTTP::Server::Simple;

# this file, has more than one line but no endline on the last line
my $testfile = 'nonewline.txt';
my $testfile_path = catfile('t' => 'data' => $testfile);

my $testfile_content = do {
    my $fh = IO::File->new($testfile_path)
        or die "Couldn't open $testfile_path $!";
    local $/;
    <$fh>;
};

my ($reply_header, $reply_body) = do {
    my $binary_string = $testfile_content;
    my $length = length($binary_string);
    my $spaces = ' ' x length('Content: ');
    $binary_string =~ s/\n/\n$spaces/sg;
    my $body = <<"EOF";
id: 873
Subject: spaces.txt
Creator: 322136
Created: 2018-11-10 05:23:01
Transaction: 1818943
Parent: 130
MessageId: \nFilename: spaces.txt
ContentType: text/plain
ContentEncoding: none

Headers: MIME-Version: 1.0
         Subject: spaces.txt
         X-Mailer: MIME-tools 5.504 (Entity 5.504)
         Content-Type: text/plain; charset="utf-8"; name="spaces.txt"
         Content-Disposition: inline; filename="spaces.txt"
         Content-Transfer-Encoding: binary
         X-RT-Original-Encoding: utf-8
         Content-Length: $length

Content: $binary_string
EOF
    ('RT/4.0.7 200 Ok', $body);
};

my $http_payload =
    $reply_header                                       .
    "\n\n"                                              .
    $reply_body                                         .
    "\n\n"                                              ;

my $http_reply =
    "HTTP/1.1 200 OK\r\n"                               .
    "Content-Type: text/plain; charset=utf-8\r\n\r\n"   .
    $http_payload                                       ;

my $pipe = IO::Pipe->new;                           # Used to get port number
my $pid = fork;
die "cannot fork: $!" if not defined $pid;

if (0 == $pid) {                                    # Child
    $pipe->writer;
    {
        package My::Web::Server;
        use base qw(HTTP::Server::Simple::CGI);
        sub handle_request {
            print $http_reply;
        }
        # A hack to get HTTP::Server::Simple listen on ephemeral port.
        # See RT#72987
        sub after_setup_listener {
            use Socket;
            my $sock = getsockname HTTP::Server::Simple::HTTPDaemon;
            my ($port) = (sockaddr_in($sock))[0];
            $pipe->print("$port\n");
            $pipe->close;
        }
    }
    my $server = My::Web::Server->new('00');
    alarm 120;                                      # Just in case, don't hang people
    $server->run;                                   # Run until killed
    die 'unreachable code';
}

$pipe->reader;
chomp(my $port = <$pipe>);
#diag("set up web server on port $port");
$pipe->close;

unless ($port && $port =~ /^\d+$/) {
    kill 9, $pid;
    waitpid $pid, 0;
    plan skip_all => 'could not get port number from child, skipping all tests';
}

plan tests => 3;

{
    my $res = HTTP::Response->parse( $http_reply );
    ok($res->content eq $http_payload,
        'self-test: HTTP::Response gives back correct payload');
}

my $rt = RT::Client::REST->new(
    server => "http://127.0.0.1:$port",
    timeout => 2,
);

# avoid need to login
$rt->basic_auth_cb(sub { return });

{
    my $res = $rt->get_attachment(parent_id => 130, id => 873, undecoded => 1);
    ok($res->{Content} eq $testfile_content, 'files match with undecoded option');
}
{
    my $res = $rt->get_attachment(parent_id => 130, id => 873, undecoded => 0);
    ok($res->{Content} eq $testfile_content, 'files match w/o undecoded option');
}

kill 9, $pid;
waitpid $pid, 0;
exit;