Hello.
use warnings;
use strict;
use Test::Nginx::Socket 'no_plan';
use HTTP::Request;
use LWP::UserAgent;
no_shuffle();
run_tests();
__DATA__
=== TEST 1: xxx
--- config
location = /code {
content_by_lua_block {
ngx.print("happycoding")
}
}
location = /abcd {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local code = args['code']
if code == "happycoding" then
ngx.print("success")
else
ngx.print("failed")
end
}
}
--- request_eval
sub getcode($) {
my $fake = shift;
if ($fake == 1) {
return "deadbeef";
}
my $addr = $ENV{server_addr_for_client} || "127.0.0.1";
my $port = $ENV{TEST_NGINX_SERVER_PORT} || $ENV{TEST_NGINX_PORT} || 1984;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => "http://$addr:$port/code");
my $resp = $ua->request($req);
my $code = $resp->decoded_content;
return $code;
}
[
"GET /abcd?code=${\getcode(0)}",
"GET /abcd?code=${\getcode(1)}"
]
--- response_body eval
["success", "failed"]
注意这里需要用 request_eval 而不是 eval,否则通过 eval 执行 perl 的代码时,nginx 还没运行:(
在 2016年11月28日星期一 UTC+8下午4:43:13,Celebi写道:
比如要测一个业务流程
第一个请求login得到login_token,可能是json中的一个key的值,
然后将这个login_token作为参数传给第二个请求,
Test::Nginx可以做到吗?或者有其他办法能达到目的吗?