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

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

How to add a linebreak in test::base/test::nginx/perl test case?

问题

  1. 我在实现[Apache APISIX][1]的功能时编写测试用例遇到了一个顽固的错误got部分相比于expected部分多了一个换行符
  2. [![测试结果][2]][2]
  3. 以下是测试代码
  4. === 测试 8从保险库获取值token环境变量错误/丢失
  5. --- 配置
  6. 位置 /t {
  7. content_by_lua_block {
  8. local vault = require("apisix.secret.vault")
  9. local conf = {
  10. prefix = "kv/apisix",
  11. token = "$ENV://VALT_TOKEN",
  12. uri = "http://127.0.0.1:8200"
  13. }
  14. local value, err = vault.get(conf, "/apisix-key/jack/key")
  15. if err then
  16. return ngx.say(err)
  17. end
  18. ngx.print("value")
  19. }
  20. }
  21. --- 请求
  22. GET /t
  23. --- 响应正文
  24. 无法解码结果结果{"errors":["权限被拒绝"]}
  25. === 测试 9从保险库获取值token环境变量包含错误的令牌
  26. --- 配置
  27. 位置 /t {
  28. content_by_lua_block {
  29. local vault = require("apisix.secret.vault")
  30. local conf = {
  31. prefix = "kv/apisix",
  32. token = "$ENV://WRONG_VAULT_TOKEN",
  33. uri = "http://127.0.0.1:8200"
  34. }
  35. local value, err = vault.get(conf, "/apisix-key/jack/key")
  36. if err then
  37. return ngx.say(err)
  38. end
  39. ngx.print("value")
  40. }
  41. }
  42. --- 请求
  43. GET /t
  44. --- 响应正文
  45. 无法解码结果结果{"errors":["权限被拒绝"]}

我尝试在预期部分的末尾添加 "\n",如下所示:

  1. --- 响应正文
  2. 无法解码结果结果{"errors":["权限被拒绝"]}\n

但是这并不起作用。所以我尝试用引号 "..." 包围“expected”部分,以便包含换行符,但这也不起作用。

另一种方法是通过编写一些代码从“got”部分中去除换行符,但我认为这不是理想的做法(修改响应)。

提前感谢!这是工作流程操作运行的链接

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. [![Test result][2]][2]
  5. Here is the code for the test:
  6. ```perl
  7. === TEST 8: get value from vault: token env var wrong/missing
  8. --- config
  9. location /t {
  10. content_by_lua_block {
  11. local vault = require(&quot;apisix.secret.vault&quot;)
  12. local conf = {
  13. prefix = &quot;kv/apisix&quot;,
  14. token = &quot;$ENV://VALT_TOKEN&quot;,
  15. uri = &quot;http://127.0.0.1:8200&quot;
  16. }
  17. local value, err = vault.get(conf, &quot;/apisix-key/jack/key&quot;)
  18. if err then
  19. return ngx.say(err)
  20. end
  21. ngx.print(&quot;value&quot;)
  22. }
  23. }
  24. --- request
  25. GET /t
  26. --- response_body
  27. failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}
  28. === TEST 9: get value from vault: token env var contains wrong token
  29. --- config
  30. location /t {
  31. content_by_lua_block {
  32. local vault = require(&quot;apisix.secret.vault&quot;)
  33. local conf = {
  34. prefix = &quot;kv/apisix&quot;,
  35. token = &quot;$ENV://WRONG_VAULT_TOKEN&quot;,
  36. uri = &quot;http://127.0.0.1:8200&quot;
  37. }
  38. local value, err = vault.get(conf, &quot;/apisix-key/jack/key&quot;)
  39. if err then
  40. return ngx.say(err)
  41. end
  42. ngx.print(&quot;value&quot;)
  43. }
  44. }
  45. --- request
  46. GET /t
  47. --- response_body
  48. 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:

  1. --- response_body
  2. 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”。 这就是我的做法。

我用这个替换了:

  1. --- response_body
  2. failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}

改成了这个:

  1. --- response_body_like
  2. 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:

  1. --- response_body
  2. failed to decode result, res: {&quot;errors&quot;:[&quot;permission denied&quot;]}

With this:

  1. --- response_body_like
  2. 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:

确定