IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 15, 2006, 12:02:55 PM (20 years ago)
Author:
jhoblitt
Message:

add ->request() tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStore/t/07_file.t

    r6541 r6594  
    33# Copyright (C) 2006  Joshua Hoblitt
    44#
    5 # $Id: 07_file.t,v 1.1 2006-03-08 01:14:14 jhoblitt Exp $
     5# $Id: 07_file.t,v 1.2 2006-03-15 22:02:53 jhoblitt Exp $
    66
    77use strict;
     
    1010use lib qw( ./lib ./t );
    1111
    12 use Test::More tests => 6;
     12use Test::More tests => 14;
    1313
    1414=head1 NAME
     
    2323
    2424use DataStore::File;
     25use File::Temp ();
    2526
    2627can_ok('DataStore::File', qw(
     
    6970
    7071eval {
    71     my $dsp = DataStore::File->new;
     72    my $dsf = DataStore::File->new;
    7273};
    7374like($@, qr/Mandatory parameters/,
     
    7677# ->request()
    7778
     79use Net::HTTPServer;
     80
     81sub bar
     82{
     83    my $req = shift;             # Net::HTTPServer::Request object
     84    my $res = $req->Response();  # Net::HTTPServer::Response object
     85
     86    my $string = "foobarbaz";
     87
     88    $res->Print($string);
     89
     90    return $res;
     91}
     92
     93$|++;
     94
     95$SIG{CHLD} = 'IGNORE';
     96my $pid = open(my $child, "-|");
     97
     98unless ($pid) {
     99    my $server = new Net::HTTPServer( port => 'scan' );
     100
     101    # send port number to parent
     102    print $server->Start(), "\n";
     103
     104    $server->RegisterURL('/somefile', \&bar);
     105    $server->Process();  # Run forever
     106
     107    exit -1;
     108}
     109
     110my $port = <$child>;
     111chomp $port;
     112
     113{
     114    my $dsf = DataStore::File->new(
     115        uri         => "http://localhost:$port/somefile",
     116        fileid      => '12buckelyourshoe',
     117        bytes       => 9,
     118        md5sum      => 'a0a6e1a375117c58d77221f10c5ce12e',
     119        type        => 'foo',
     120    );
     121
     122    my $fh = File::Temp->new( UNLINK => 1 );
     123    $fh->autoflush(1);
     124    isa_ok($dsf->request( filename => $fh->filename ), 'DataStore::Response');
     125}
     126
     127{
     128    my $dsf = DataStore::File->new(
     129        uri         => "http://localhost:$port/somefile",
     130        fileid      => '12buckelyourshoe',
     131        bytes       => 9,
     132        md5sum      => 'a0a6e1a375117c58d77221f10c5ce12e',
     133        type        => 'foo',
     134    );
     135
     136    my $fh = File::Temp->new( UNLINK => 1 );
     137    $fh->autoflush(1);
     138    my $dsr = $dsf->request( filename => $fh->filename );
     139
     140    ok($dsr->is_success, "response->is_success() is correct");
     141    is($dsr->code, 200, "response->code() is correct");
     142    is($dsr->status_line, "200 OK", "response->status_line() is correct");
     143    is($dsr->data, $fh->filename, "response->data is arrayref is correct");
     144    isa_ok($dsr->request, 'DataStore::Record');
     145}
     146
     147# stop HTTP server
     148kill 9, $pid;
     149
     150eval {
     151    my $dsf = DataStore::File->new(
     152        uri         => 'http://example.org/foo',
     153        fileid      => '12buckelyourshoe',
     154        bytes       => 12345,
     155        md5sum      => 'fe6a2b6564c0d4cfb3bbf1db813824ba',
     156        type        => 'foo',
     157    );
     158   
     159    $dsf->request;
     160};
     161like($@, qr/Mandatory parameter/,
     162    '->request() fails when passed no params');
     163
     164eval {
     165    my $dsf = DataStore::File->new(
     166        uri         => 'http://example.org/foo',
     167        fileid      => '12buckelyourshoe',
     168        bytes       => 12345,
     169        md5sum      => 'fe6a2b6564c0d4cfb3bbf1db813824ba',
     170        type        => 'foo',
     171    );
     172   
     173    my $fh = File::Temp->new( UNLINK => 1 );
     174    $dsf->request(
     175        filename => $fh->filename,
     176        foo => 1,
     177    );
     178};
     179like($@, qr/not listed in the validation options/,
     180    '->request() fails whe passed extra params');
Note: See TracChangeset for help on using the changeset viewer.