Skip to content

Commit 3e53a25

Browse files
committed
fix tests
1 parent 86b3ffb commit 3e53a25

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/ngx_http_apisix_module.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -849,20 +849,20 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
849849
static u_char*
850850
ngx_http_apisix_error_log_handler(ngx_http_request_t *r, u_char *buf, size_t len)
851851
{
852-
ngx_http_variable_value_t *request_id_var;
853-
ngx_http_apisix_loc_conf_t *loc_conf;
852+
ngx_http_variable_value_t *request_id_var;
853+
ngx_http_apisix_loc_conf_t *loc_conf;
854854

855-
loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
856-
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
857-
return buf;
858-
}
855+
loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
856+
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
857+
return buf;
858+
}
859859

860-
request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
861-
if (request_id_var == NULL || request_id_var->not_found) {
862-
return buf;
863-
}
864-
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
865-
return buf;
860+
request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
861+
if (request_id_var == NULL || request_id_var->not_found) {
862+
return buf;
863+
}
864+
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
865+
return buf;
866866
}
867867

868868

@@ -904,7 +904,7 @@ ngx_http_apisix_replace_error_log_handler(ngx_http_request_t *r)
904904

905905
ctx = ngx_http_apisix_get_module_ctx(r);
906906
if (ctx == NULL) {
907-
return NGX_OK;
907+
return NGX_ERROR;
908908
}
909909

910910
if (r->log_handler == NULL){
@@ -935,8 +935,8 @@ ngx_http_apisix_error_log_init(ngx_conf_t *cf)
935935
if (h == NULL) {
936936
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
937937
"failed setting error log handler");
938-
return NGX_CONF_ERROR;
939-
}
938+
return NGX_CONF_ERROR;
939+
}
940940

941941
*h = ngx_http_apisix_replace_error_log_handler;
942942

t/request-id-err-log.t

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,49 @@ qr/.*request_id: "1234".*$/
4242

4343

4444

45-
=== TEST 3: scoping: value is appended correctly to error logs
46-
based on the location where the directive is defined
45+
=== TEST 3: scoping: value is appended correctly to error logs based on the location where the directive is defined
4746
--- config
48-
location /append_req_id {
49-
set $req_id_a 123456;
50-
apisix_request_id_var $req_id_a;
47+
location = /append_method {
48+
set $req_id_b 654321;
49+
apisix_request_id_var $req_id_b;
50+
5151
content_by_lua_block {
5252
ngx.log(ngx.INFO, "log_msg")
5353
ngx.exit(200)
5454
}
5555
}
56-
location /append_method {
57-
set $req_id_b 654321;
58-
apisix_request_id_var $req_id_b;
59-
56+
location = /append_req_id {
57+
set $req_id_a 123456;
58+
apisix_request_id_var $req_id_a;
6059
content_by_lua_block {
6160
ngx.log(ngx.INFO, "log_msg")
6261
ngx.exit(200)
6362
}
6463
}
65-
--- pipelined_requests eval
66-
["GET /append_req_id", "GET /append_method"]
67-
--- error_code eval
68-
[200, 200, 200]
64+
--- request
65+
GET /append_method
6966
--- error_log eval
70-
[ 'request_id: "123456"', 'request_id: "654321"' ]
67+
qr/log_msg.*request_id: "654321"$/
68+
--- no_error_log
69+
[error]
70+
[crit]
71+
[alert]
72+
73+
74+
75+
=== TEST 4: Send request to different location
76+
--- request
77+
GET /append_req_id
78+
--- error_log eval
79+
qr/log_msg.*request_id: "123456"$/
7180
--- no_error_log
7281
[error]
7382
[crit]
7483
[alert]
7584

7685

7786

78-
=== TEST 4: scoping: value is NOT appended to error logs for the location where the directive is NOT defined
87+
=== TEST 5: scoping: value is NOT appended to error logs for the location where the directive is NOT defined
7988
--- config
8089
location /append {
8190
set $req_id 123456;
@@ -99,7 +108,7 @@ qr/log_msg.*request_id/
99108

100109

101110

102-
=== TEST 5: scoping: value is appended correctly to error logs when the directive is in the main configuration
111+
=== TEST 6: scoping: value is appended correctly to error logs when the directive is in the main configuration
103112
--- http_config
104113
apisix_request_id_var $req_id;
105114
--- config
@@ -122,7 +131,7 @@ qr/log_msg.*request_id: "123456"$/
122131

123132

124133

125-
=== TEST 6: scoping: value is appended correctly to error logs and the local directive overrides the global one
134+
=== TEST 7: scoping: value is appended correctly to error logs and the local directive overrides the global one
126135
--- http_config
127136
apisix_request_id_var $req_id_global;
128137
--- config
@@ -146,7 +155,7 @@ qr/log_msg.*request_id: "global"$/
146155

147156

148157

149-
=== TEST 7: Request ID variable changes are applied to the error log output
158+
=== TEST 8: Request ID variable changes are applied to the error log output
150159
--- config
151160
location = /test {
152161
set $my_var "";

0 commit comments

Comments
 (0)