X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;ds=sidebyside;f=t%2Ffilter.t;h=609a19297f3b4094db0326754f998442a57e2d77;hb=c1abb946e53e4179666ebb290e31c2d9ddc40711;hp=c3cd39f426f9c3a1b4c8de17e7c1b3d73aa82120;hpb=e022d3377fd2c50fd9931bf96394728958a90bf3;p=public-inbox.git diff --git a/t/filter.t b/t/filter.t index c3cd39f4..609a1929 100644 --- a/t/filter.t +++ b/t/filter.t @@ -1,4 +1,4 @@ -# Copyright (C) 2013, Eric Wong and all contributors +# Copyright (C) 2013-2015 all contributors # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) use strict; use warnings; @@ -15,6 +15,53 @@ sub count_body_parts { $bodies->{$body}++; } +# multipart/alternative: HTML and quoted-printable, keep the plain-text +{ + my $html_body = "hi"; + my $parts = [ + Email::MIME->create( + attributes => { + content_type => 'text/html; charset=UTF-8', + encoding => 'base64', + }, + body => $html_body, + ), + Email::MIME->create( + attributes => { + content_type => 'text/plain', + encoding => 'quoted-printable', + }, + body => 'hi = "bye"', + ) + ]; + my $email = Email::MIME->create( + header_str => [ + From => 'a@example.com', + Subject => 'blah', + 'Content-Type' => 'multipart/alternative' + ], + parts => $parts, + ); + is(1, PublicInbox::Filter->run($email), "run was a success"); + my $parsed = Email::MIME->new($email->as_string); + is("text/plain", $parsed->header("Content-Type")); + is(scalar $parsed->parts, 1, "HTML part removed"); + my %bodies; + $parsed->walk_parts(sub { + my ($part) = @_; + return if $part->subparts; # walk_parts already recurses + count_body_parts(\%bodies, $part); + }); + is(scalar keys %bodies, 1, "one bodies"); + is($bodies{"hi =3D \"bye\"="}, 1, "QP text part unchanged"); + $parsed->walk_parts(sub { + my ($part) = @_; + my $b = $part->body; + $b =~ s/\s*\z//; + is($b, "hi = \"bye\"", "decoded body matches"); + }); +} + # plain-text email is passed through unchanged { my $s = Email::MIME->create( @@ -38,13 +85,14 @@ sub count_body_parts { 'Content-Type' => 'text/html', Subject => 'HTML only badness', ], - body => "bad body\n", + body => "bad body\r\n\n", ); is(1, PublicInbox::Filter->run($s), "run was a success"); unlike($s->as_string, qr//, "HTML removed"); is("text/plain", $s->header("Content-Type"), "content-type changed"); like($s->body, qr/\A\s*bad body\s*\z/, "body"); + unlike($s->body, qr/\r/, "body has no cr"); like($s->header("X-Content-Filtered-By"), qr/PublicInbox::Filter/, "XCFB header added"); } @@ -249,24 +297,6 @@ sub count_body_parts { like($s->as_string, qr/scrubbed/, "scrubbed message"); } -{ - my $s = Email::MIME->create( - header => [ - From => 'a@example.com', - To => 'b@example.com', - 'Content-Type' => 'text/plain', - 'Mail-Followup-To' => 'c@example.com', - Subject => 'mfttest', - ], - body => "mft\n", - ); - - is('c@example.com', $s->header("Mail-Followup-To"), - "mft set correctly"); - is(1, PublicInbox::Filter->run($s), "run succeeded for mft"); - is(undef, $s->header("Mail-Followup-To"), "mft stripped"); -} - # multi-part with application/octet-stream { my $os = 'application/octet-stream';