我正在尝试使用Mojo::UserAgent
来验证应用程序的 gzip 压缩(内容编码)。
不幸的是,这个 UA 似乎默默地解码了内容并删除了 Content-Encoding 标头的后缀。
以下是我的最小示例
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 3;
use Mojo::UserAgent; # Version 8.26
my $ua = Mojo::UserAgent->new();
# As documented: https://docs.mojolicious.org/Mojolicious/Guides/Cookbook#Decorating-follow-up-requests
$ua->once(
start => sub {
my ( $ua, $tx ) = @_;
$tx->req->headers->header( 'Accept-Encoding' => 'gzip' );
}
);
my $tx = $ua->get('https://www.mojolicious.org');
is( $tx->req->headers->header('Accept-Encoding'), 'gzip', qq{Request Accept-Encoding is "gzip"} );
ok( $tx->res->is_success, "Response is success" );
# The following assertion fails.
# My theory is that Mojo::UserAgent is silently decoding the content, and changing
# the Content-Encoding and Content-Length to reflect the new values. However, how
# do we inspect what the original response headers were?
is( $tx->res->headers->header('Content-Encoding'), 'gzip', qq{Response Content-Encoding is "gzip"} );
结果
$ perl mojo_useragent_content_encoding.pl
1..3
ok 1 - Request Accept-Encoding is "gzip"
ok 2 - Response is success
not ok 3 - Response Content-Encoding is "gzip"
# Failed test 'Response Content-Encoding is "gzip"'
# at mojo_useragent_content_encoding.pl line 30.
# got: undef
# expected: 'gzip'
# Looks like you failed 1 test of 3.
通过分析 Apache 日志,我能够确认有效负载正在被 gzip 压缩。此外,此 curl 还确认此示例网站正在使用 gzip 编码来处理请求
$ curl -i -H "Accept-Encoding: gzip" https://www.mojolicious.org
HTTP/1.1 200 OK
Date: Mon, 18 Jan 2021 21:28:14 GMT
Content-Type: text/html;charset=UTF-8
...
Content-Encoding: gzip
...
我可以用来LWP::UserAgent
确认响应的正确内容编码。
但是,在执行任何理论上的后期处理之前,我无法确定如何检查 Mojo::UserAgent 响应以查看真实的标头。