英文:
How to add a linebreak in test::base/test::nginx/perl test case?
问题
我在实现[Apache APISIX][1]的功能时编写测试用例,遇到了一个顽固的错误,即“got”部分相比于“expected”部分多了一个换行符。
[![测试结果][2]][2]
以下是测试代码:
=== 测试 8:从保险库获取值:token环境变量错误/丢失
--- 配置
位置 /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://VALT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- 请求
GET /t
--- 响应正文
无法解码结果,结果:{"errors":["权限被拒绝"]}
=== 测试 9:从保险库获取值:token环境变量包含错误的令牌
--- 配置
位置 /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://WRONG_VAULT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- 请求
GET /t
--- 响应正文
无法解码结果,结果:{"errors":["权限被拒绝"]}
我尝试在预期部分的末尾添加 "\n"
,如下所示:
--- 响应正文
无法解码结果,结果:{"errors":["权限被拒绝"]}\n
但是这并不起作用。所以我尝试用引号 "..."
包围“expected”部分,以便包含换行符,但这也不起作用。
另一种方法是通过编写一些代码从“got”部分中去除换行符,但我认为这不是理想的做法(修改响应)。
提前感谢!这是工作流程操作运行的链接。
<details>
<summary>英文:</summary>
I was writing test cases while implementing a feature in [Apache APISIX][1] where I came across a stubborn error where there is an extra linebreak in the "got" part in comparison to the "expected" part.
[![Test result][2]][2]
Here is the code for the test:
```perl
=== TEST 8: get value from vault: token env var wrong/missing
--- config
location /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://VALT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- request
GET /t
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
=== TEST 9: get value from vault: token env var contains wrong token
--- config
location /t {
content_by_lua_block {
local vault = require("apisix.secret.vault")
local conf = {
prefix = "kv/apisix",
token = "$ENV://WRONG_VAULT_TOKEN",
uri = "http://127.0.0.1:8200"
}
local value, err = vault.get(conf, "/apisix-key/jack/key")
if err then
return ngx.say(err)
end
ngx.print("value")
}
}
--- request
GET /t
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
I tried adding "\n" at the end of the expected part like so:
--- response_body
failed to decode result, res: {"errors":["permission denied"]}\n
But that didn't work. So I tried to surround the "expected" part in quotes "..."
so that the linebreak gets included that didn't work either.
Another approach would be to remove the linebreak from the "got" part by writing some code but I think that wouldn't be an ideal thing to do (modifying the response).
Thanks in advance!!
Here's the link to the workflow action run.
答案1
得分: 1
曾经很简单!我不知道我们可以使用正则表达式来匹配“got”和“expected”。 这就是我的做法。
我用这个替换了:
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
改成了这个:
--- response_body_like
failed to decode result, res: {\"errors\":\[\"permission denied\"\]}\n
这个想法是使用 response_body_like
结构来能够使用正则表达式进行表达式匹配。感谢 这篇博文。
英文:
It was simple! I didn't know we could use regex to match "got" with "expected". This is how I did it.
I replaced this:
--- response_body
failed to decode result, res: {"errors":["permission denied"]}
With this:
--- response_body_like
failed to decode result, res: {\"errors\":\[\"permission denied\"\]}\n
The idea was to use the response_body_like
construct to be able to use regex for expression matching. Thanks to this blog.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论