错误场景测试
测试 Nginx 启动失败、配置错误、超时、畸形响应等异常场景。
启动失败测试
使用 --- must_die 段来测试预期的 Nginx 启动失败。这对于验证模块的配置校验逻辑至关重要:
perl
1=== TEST 1: reject invalid config2--- http_config3 my_module_invalid_directive on;4--- must_die5--- error_log6unknown directive "my_module_invalid_directive"超时测试
测试连接超时和读取超时场景:
perl
1=== TEST 2: upstream timeout2--- http_config3 upstream backend {4 server 127.0.0.1:$TEST_NGINX_RAND_PORT_1;5 }6--- config7 location /t {8 proxy_connect_timeout 100ms;9 proxy_pass http://backend;10 }11--- tcp_listen: $TEST_NGINX_RAND_PORT_112--- tcp_reply_delay: 200ms13--- tcp_reply14slow response15--- request16GET /t17--- error_code: 504忽略畸形响应
有些场景(如 mock 服务器返回非法 HTTP 响应)需要忽略响应检查:
perl
1=== TEST 3: malformed upstream response2--- config3 location /t {4 proxy_pass http://127.0.0.1:$TEST_NGINX_RAND_PORT_1;5 }6--- tcp_listen: $TEST_NGINX_RAND_PORT_17--- tcp_reply8NOT A VALID HTTP RESPONSE9--- request10GET /t11--- ignore_response12--- error_log13upstream sent no valid HTTP/1.0 header