Copilot commented on code in PR #13234:
URL: https://github.com/apache/apisix/pull/13234#discussion_r3094181494
##########
t/plugin/ai-request-rewrite2.t:
##########
@@ -112,7 +53,7 @@ __DATA__
},
"provider": "openai",
"override": {
- "endpoint":
"http://localhost:6724/check_extra_options"
+ "endpoint":
"http://127.0.0.1:1980/check_extra_options"
},
Review Comment:
These tests now point to the shared mock server at
`127.0.0.1:1980/check_extra_options`, but `t/lib/server.lua` does not implement
a `check_extra_options` handler (URI gets mapped to `check_extra_options` in
`server.go()`), so the upstream will 404. Either add a `check_extra_options`
endpoint to `t/lib/server.lua` (replicating the previous validation logic) or
keep an inline mock for this test.
##########
t/plugin/ai-request-rewrite2.t:
##########
@@ -200,7 +141,7 @@ override.endpoint is required for openai-compatible provider
"model": "gpt-35-turbo-instruct"
},
"override": {
- "endpoint":
"http://localhost:6724/test/params/in/overridden/endpoint?some_query=yes"
+ "endpoint":
"http://127.0.0.1:1980/test/params/in/overridden/endpoint?some_query=yes"
},
Review Comment:
This override endpoint uses `/test/params/in/overridden/endpoint`, but the
shared test server maps this URI to `test_params_in_overridden_endpoint` and no
such handler exists in `t/lib/server.lua`. As-is, the upstream will return 404
and the test won’t validate query param forwarding. Add a corresponding handler
in `t/lib/server.lua` or change this to hit an existing fixture-backed endpoint
and validate query params another way.
##########
t/lib/fixture_loader.lua:
##########
@@ -0,0 +1,142 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- Test fixture loader for AI proxy mock responses.
+-- Loads fixture files from t/fixtures/ and serves them based on the
+-- X-AI-Fixture request header.
+--
+-- Supported headers:
+-- X-AI-Fixture: <path> -- fixture file path relative to t/fixtures/
+-- X-AI-Fixture-Status: <code> -- optional HTTP status code (default 200)
+
+local _M = {}
+local io = io
+local ngx = ngx
+
+local fixture_base_dir
+
+
+local function get_fixture_dir()
+ if fixture_base_dir then
+ return fixture_base_dir
+ end
+
+ local constants = require("apisix.constants")
+ local lua_home = constants and constants.apisix_lua_home
+ if type(lua_home) ~= "string" or lua_home == "" then
+ return nil, "apisix_lua_home is not initialized"
+ end
+ fixture_base_dir = lua_home .. "/t/fixtures/"
+ return fixture_base_dir
+end
+
+
+function _M.load(name)
+ if type(name) ~= "string" or #name == 0 or #name > 256 then
+ return nil, "invalid fixture name"
+ end
+ if name:sub(1, 1) == "/" or name:find("..", 1, true) then
+ return nil, "invalid fixture name"
+ end
+ if not name:match("^[%w%._/-]+$") then
+ return nil, "invalid fixture name"
+ end
+
+ local dir, dir_err = get_fixture_dir()
+ if not dir then
+ return nil, dir_err
+ end
+ local path = dir .. name
+ local f = io.open(path, "r")
+ if not f then
+ return nil, "fixture not found"
+ end
+ local content, read_err = f:read("*a")
+ local ok, close_err = f:close()
+ if not content then
+ return nil, "failed to read fixture: " .. (read_err or "unknown error")
+ end
+ if not ok then
+ return nil, "failed to close fixture: " .. (close_err or "unknown
error")
+ end
+ return content
+end
+
+
+-- Replace {{model}} placeholder with the model from request body.
+local function apply_template(content)
+ if not content:find("{{model}}", 1, true) then
+ return content
+ end
+
+ ngx.req.read_body()
+ local body_data = ngx.req.get_body_data()
+ if not body_data then
+ return content:gsub("{{model}}", "unknown")
+ end
+
+ local json = require("cjson.safe")
+ local body = json.decode(body_data)
+ if not body or not body.model then
+ return content:gsub("{{model}}", "unknown")
+ end
+
+ return content:gsub("{{model}}", function() return body.model end)
+end
Review Comment:
`apply_template` uses `content:gsub("{{model}}", body.model)`, but
`string.gsub` treats `%` sequences in the replacement string specially. A model
value containing `%` (or other magic sequences) will be mangled. Use a function
replacement (or escape `%` in `body.model`) to ensure literal substitution.
##########
t/plugin/ai-request-rewrite2.t:
##########
@@ -244,7 +185,7 @@ passed
},
"provider": "openai",
"override": {
- "endpoint":
"http://localhost:6724/check_extra_options"
+ "endpoint":
"http://127.0.0.1:1980/check_extra_options"
},
"ssl_verify": false,
Review Comment:
Same issue as earlier: the shared mock server doesn’t define a
`check_extra_options` action, so this override endpoint will 404. Add the
missing handler in `t/lib/server.lua` or keep an inline mock for this test.
##########
t/plugin/ai-request-rewrite.t:
##########
@@ -604,7 +528,7 @@ passed
},
"provider": "openai",
"override": {
- "endpoint": "http://localhost:6724/bad_request"
+ "endpoint": "http://127.0.0.1:1980/bad_request"
},
"ssl_verify": false,
Review Comment:
This test now depends on `127.0.0.1:1980/bad_request`, but there is no
`bad_request` handler in `t/lib/server.lua` for the shared mock server. Add
`bad_request` (and `internalservererror` below) endpoints to
`t/lib/server.lua`, or keep these inline mocks so the expected error paths are
still exercised.
##########
t/plugin/ai-request-rewrite.t:
##########
@@ -668,7 +592,7 @@ passed
},
"provider": "openai",
"override": {
- "endpoint":
"http://localhost:6724/internalservererror"
+ "endpoint":
"http://127.0.0.1:1980/internalservererror"
},
"ssl_verify": false,
Review Comment:
This override endpoint depends on an `internalservererror` action on the
shared mock server, but `t/lib/server.lua` doesn’t define it. Without adding
it, requests to this endpoint will 404 instead of returning the intended 500
for the error-path assertions.
##########
t/plugin/ai-rate-limiting.t:
##########
@@ -423,6 +316,7 @@ X-AI-RateLimit-Reset-ai-proxy-openai: 60
]
--- more_header
Authorization: Bearer token
+X-AI-Fixture: openai/chat-model-echo.json
--- error_code eval
Review Comment:
`--- more_header` is not a valid Test::Nginx section name (should be `---
more_headers`). With the singular form, the Authorization / fixture headers
likely won’t be applied to the pipelined requests.
##########
t/plugin/ai-proxy-multi.t:
##########
@@ -250,9 +164,11 @@ passed
--- request
POST /anything
{ "messages": [ { "role": "system", "content": "You are a mathematician" }, {
"role": "user", "content": "What is 1+1?"} ] }
---- error_code: 401
---- response_body
-Unauthorized
+--- more_headers
+X-AI-Fixture: openai/chat-basic.json
+--- error_code: 200
+--- response_body eval
+qr/\{ "content": "1 \+ 1 = 2\.", "role": "assistant" \}/
Review Comment:
This scenario is labeled as “wrong auth header”, but the request now forces
a successful upstream response via `X-AI-Fixture` and expects HTTP 200. That
stops exercising the unauthorized path that the inline mock previously covered.
Consider setting `X-AI-Fixture-Status: 401` (and asserting 401) so the test
still validates behavior when the provider rejects the request.
##########
t/plugin/ai-rate-limiting.t:
##########
@@ -528,6 +424,7 @@ X-AI-RateLimit-Reset-ai-proxy-openai: 45
]
--- more_header
Authorization: Bearer token
+X-AI-Fixture: openai/chat-model-echo.json
--- error_code eval
Review Comment:
`--- more_header` should be `--- more_headers` here as well; otherwise the
headers won’t be added to the pipelined requests and the fixture-based upstream
will respond with the default missing-fixture error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]