如何在test::base/test::nginx/perl测试用例中添加换行符?

huangapple go评论77阅读模式
英文:

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 &quot;got&quot; part in comparison to the &quot;expected&quot; 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(&quot;apisix.secret.vault&quot;)
            local conf = {
                prefix = &quot;kv/apisix&quot;,
                token = &quot;$ENV://VALT_TOKEN&quot;,
                uri = &quot;http://127.0.0.1:8200&quot;
            }
            local value, err = vault.get(conf, &quot;/apisix-key/jack/key&quot;)
            if err then
                return ngx.say(err)
            end

            ngx.print(&quot;value&quot;)
        }
    }
--- request
GET /t
--- response_body
failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}



=== TEST 9: get value from vault: token env var contains wrong token
--- config
    location /t {
        content_by_lua_block {
            local vault = require(&quot;apisix.secret.vault&quot;)
            local conf = {
                prefix = &quot;kv/apisix&quot;,
                token = &quot;$ENV://WRONG_VAULT_TOKEN&quot;,
                uri = &quot;http://127.0.0.1:8200&quot;
            }
            local value, err = vault.get(conf, &quot;/apisix-key/jack/key&quot;)
            if err then
                return ngx.say(err)
            end

            ngx.print(&quot;value&quot;)
        }
    }
--- request
GET /t
--- response_body
failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}

I tried adding "\n" at the end of the expected part like so:

--- response_body
failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}\n

But that didn't work. So I tried to surround the "expected" part in quotes &quot;...&quot; 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: {&quot;errors&quot;:[&quot;permission denied&quot;]}

改成了这个:

--- response_body_like
failed to decode result, res: {\&quot;errors\&quot;:\[\&quot;permission denied\&quot;\]}\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: {&quot;errors&quot;:[&quot;permission denied&quot;]}

With this:

--- response_body_like
failed to decode result, res: {\&quot;errors\&quot;:\[\&quot;permission denied\&quot;\]}\n

The idea was to use the response_body_like construct to be able to use regex for expression matching. Thanks to this blog.

huangapple
  • 本文由 发表于 2023年2月18日 14:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定