"Fossies" - the Fresh Open Source Software Archive

Member "MIME-Types-2.22/t/22accbest.t" (9 Dec 2020, 1754 Bytes) of package /linux/privat/MIME-Types-2.22.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env perl
    2 #
    3 # Test httpAcceptBest and httpAcceptSelect()
    4 #
    5 
    6 use strict;
    7 use warnings;
    8 
    9 use Test::More tests => 25;
   10 
   11 use lib qw(lib t);
   12 
   13 use MIME::Types;
   14 
   15 my $mt = MIME::Types->new;
   16 ok(defined $mt);
   17 
   18 my @have = map $mt->type($_), qw[text/plain text/html application/pdf];
   19 cmp_ok(scalar @have, '==', 3, 'create offers');
   20 isa_ok($_, 'MIME::Type') for @have;
   21 
   22 ok($have[0]->equals('text/plain'), 'equal');
   23 ok(!$have[0]->equals('text/html'), 'not equal');
   24 
   25 # remember that order is important
   26 my $t0 = $mt->httpAcceptBest(['text/plain'], @have);
   27 is($t0, 'text/plain', 'single');
   28 
   29 my $t1 = $mt->httpAcceptBest(['text/plain', 'text/html'], @have);
   30 is($t1, 'text/plain', 'first');
   31 
   32 my $t2 = $mt->httpAcceptBest(['text/html', 'text/plain'], @have);
   33 is($t2, 'text/html', 'second');
   34 
   35 my $t3 = $mt->httpAcceptBest(['text/xyz', 'text/plain'], @have);
   36 is($t3, 'text/plain', 'second2');
   37 
   38 my $t4 = $mt->httpAcceptBest('text/html, application/pdf', @have);
   39 is($t4, 'text/html', 'string');
   40 
   41 
   42 
   43 ### Select()
   44 
   45 is($mt->mimeTypeOf('txt'), 'text/plain', 'test mimeTypeOf');
   46 is($mt->mimeTypeOf('a.txt'), 'text/plain');
   47 
   48 my ($f0, $m0) = $mt->httpAcceptSelect(undef, ['a.txt', 'a.html']);
   49 is($f0, 'a.txt', 'no Accept, take first');
   50 isa_ok($m0, 'MIME::Type');
   51 is("$m0", 'text/plain');
   52 
   53 my ($f1, $m1)
   54    = $mt->httpAcceptSelect('text/plain, text/html', ['a.txt', 'a.html']);
   55 is($f1, 'a.txt', 'select text');
   56 isa_ok($m1, 'MIME::Type');
   57 is("$m1", 'text/plain');
   58 
   59 my ($f2, $m2)
   60    = $mt->httpAcceptSelect('text/html, text/plain', ['a.txt', 'a.html']);
   61 is($f2, 'a.html', 'select html');
   62 isa_ok($m2, 'MIME::Type');
   63 is("$m2", 'text/html');
   64 
   65 my ($f3, $m3)
   66    = $mt->httpAcceptSelect('text/html, text/plain', ['a.pdf', 'a.docx']);
   67 ok(!defined $f3, 'not accepted');
   68 ok(!defined $m3);