我正在尝试使用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日志,我能够确认有效负载正在被压缩。此外,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响应以查看实际的头文件。
发布于 2021-01-19 09:50:03
您可以在代码中设置$ua->transactor->compressed(0);或在环境中设置MOJO_GZIP=0,以绕过自动解压缩。
如果您希望保持自动解压缩并在到达解压缩阶段之前检查头(这也会删除Content-Encoding头),可以在contents body事件上注册一个回调。此事件在分析标头之后但在处理正文之前激发。
use strict ;
use warnings;
use 5.30.0;
use Test::More tests => 3;
use Data::Dumper;
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 $res = $tx->res;
say 'register event listener';
$res->content->on(body=>sub{test_res_encoding($tx)});
}
);
$ua->transactor->compressed(0);
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" );
#say Dumper $tx->res->headers;
# 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?
sub test_res_encoding{
my $tx = shift;
is( $tx->res->headers->header('Content-Encoding'),
'gzip',
qq{Response Content-Encoding is "gzip"} );
}在您的环境中设置MOJO_EVENTEMITTER_DEBUG=1有助于查看发生了什么。
https://stackoverflow.com/questions/65782772
复制相似问题