如何对CGI.pm的文件上传进行单元测试?

时间:2016-08-02 14:46:50

标签: perl cgi mime

我有一个CGI,它上传了一个文件并用它做了一些事情。

我想要一个用于文件上传功能的Unittest

阅读http://www.perlmonks.org/?node_id=249917

我尝试在CGI.pm的t/upload.t

中调整代码
{
%ENV = (
%ENV, 
      'SCRIPT_NAME' => '/data.cgi',
      'SERVER_NAME' => 'localhost',
      'HTTP_CONNECTION' => 'TE, close',
      'REQUEST_METHOD' => 'POST',
      'SCRIPT_URI' => 'http://localhost/data.cgi',
      'CONTENT_LENGTH' => '160',
      'SCRIPT_FILENAME' => '/var/www/cgi-bin/data.cgi',
      'SERVER_SOFTWARE' => 'Apache/1.3.27 (Unix) ',
      'HTTP_TE' => 'deflate,gzip;q=0.3',
      'QUERY_STRING' => '',
      'REMOTE_PORT' => '1855',
      'HTTP_USER_AGENT' => 'libwww-perl/5.69',
      'SERVER_PORT' => '80',
      'REMOTE_ADDR' => '127.0.0.1',
      'CONTENT_TYPE' => 'multipart/form-data; boundary=xYzZY',
      'SERVER_PROTOCOL' => 'HTTP/1.1',
      'PATH' => '/usr/local/bin:/usr/bin:/bin',
      'REQUEST_URI' => '/data.cgi',
      'GATEWAY_INTERFACE' => 'CGI/1.1',
      'SCRIPT_URL' => '/data.cgi',
      'SERVER_ADDR' => '127.0.0.1',
      'DOCUMENT_ROOT' => '/var/www/html',
      'HTTP_HOST' => 'localhost'
    );

local *STDIN;
open STDIN, '<test.txt' or die 'argh';
binmode STDIN;

$cgi = new CGI;

run()
}

的test.txt:

--xYzZY
Content-Disposition: form-data; name="hello_world"; filename="goodbye_world.txt"
Content-Length: 13
Content-Type: text/plain

Goodbye World!
--xYzZY--

当我尝试使用

访问文件句柄时,我在cgi_error中收到“400 Bad request(格式错误的多部分POST)”
  our $cgi;

  sub run() {
  $cgi = new CGI unless $cgi;
  my $upload_fh =  $cgi->upload('upload');
  }

0 个答案:

没有答案