集成测试(端到端):如何使用NodeJS检查Azure事件网格上的消息?

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

Integration Test (E2E): How to check message on Azure Event Grid with NodeJS?

问题

我有一个.NET HttpTrigger Azure函数,它接收请求主体并将其发送到Azure事件网格主题。

我需要为此编写一个NodeJS/TS的E2E或集成测试。我的想法是模仿向Azure函数发送HTTP Post请求,然后检查/观察消息是否存在于EventGrid主题中作为检查条件,但这比说起来容易得多。

我找到了一个非常酷的.NET项目,但不幸的是,我无法使用它,因为我的测试项目使用了PlayWright和NodeJS。

这篇Microsoft 文章 讨论了如何创建一个使用Azure Relay连接的Node客户端/服务器。但是,我不确定如何将它整合到我的测试中,甚至不确定是否是正确的做法?

  1. import { test as base, APIResponse } from '@playwright/test';
  2. let _respStatus: number;
  3. let _response: APIResponse;
  4. // 触发 Azure 函数
  5. base.step('Send POST to "admin/functions/FunctionName"', async () => {
  6. _response = await request.post('admin/functions/FunctionName', { data: null });
  7. _respStatus = _response.status();
  8. });
  9. // 通过 Azure Relay 监听 Event Grid 主题上的消息
  10. base.step("Listen for Event Grid message", async () => {
  11. const https = require("hyco-https");
  12. const ns = "{RelayNamespace}";
  13. const path = "{HybridConnectionName}";
  14. const keyrule = "{SASKeyName}";
  15. const key = "{SASKeyValue}";
  16. var uri = https.createRelayListenUri(ns, path);
  17. var server = https.createRelayedServer(
  18. {
  19. server: uri,
  20. token: () => https.createRelayToken(uri, keyrule, key),
  21. },
  22. (req, res) => {
  23. res.setHeader("Content-Type", "text/html");
  24. res.end("<html><head><title>Hey!</title></head><body>Relayed Node.js Server!</body></html>");
  25. }).listen(() => {});
  26. });
  1. <details>
  2. <summary>英文:</summary>
  3. I have a .NET HttpTrigger Azure Function which takes the request body and emit it to an Azure Event Grid topic.
  4. I need to write an E2E or integration test for this in NodeJS/TS. My thought is to mimic a HTTP Post request to the Azure function and then check/observe the message exists within the EventGrid topic as a check condition but that is easier said than done.
  5. I found a [very cool project in .NET][2] but unfortunately I cannot use it because my test project is using PlayWright and NodeJS.
  6. This Microsoft [article][1] talks about create a Node client/server using Azure Relay Connection. But, I am not sure how to incorporate it into my test, or even if it is the right thing to do?
  7. import { test as base, APIResponse } from &#39;@playwright/test&#39;;
  8. let _respStatus: number;
  9. let _response: APIResponse;
  10. //Trigger the Azure Function
  11. base.step(&#39;Send POST to \&quot;admin/functions/FunctionName\&quot;&#39;, async () =&gt; {
  12. _response = await request.post(&#39;admin/functions/FunctionName&#39;,{ data: null });
  13. _respStatus = _response.status();
  14. });
  15. //Check for the message on Event Grid Topic via Azure Relay
  16. base.step(&quot;Listen for Event Grid message&quot;, async () =&gt; {
  17. const https = require(&quot;hyco-https&quot;);
  18. const ns = &quot;{RelayNamespace}&quot;;
  19. const path = &quot;{HybridConnectionName}&quot;;
  20. const keyrule = &quot;{SASKeyName}&quot;;
  21. const key = &quot;{SASKeyValue}&quot;;
  22. var uri = https.createRelayListenUri(ns, path);
  23. var server = https.createRelayedServer(
  24. {
  25. server: uri,
  26. token: () =&gt; https.createRelayToken(uri, keyrule, key),
  27. },
  28. (req, res) =&gt; {
  29. res.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;);
  30. res.end(&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hey!&lt;/title&gt;&lt;/head&gt;&lt;body&gt;Relayed Node.js Server!&lt;/body&gt;&lt;/html&gt;&quot;);
  31. }).listen(() =&gt; {});
  32. [1]: https://learn.microsoft.com/en-us/azure/azure-relay/relay-hybrid-connections-http-requests-node-get-started
  33. [2]: https://eventgrid.arcus-azure.net/Features/running-integration-tests
  34. </details>
  35. # 答案1
  36. **得分**: 0
  37. I managed to get it working. Here is the general idea behind my solution.
  38. - 在Azure云上创建一个Azure Relay,创建一个混合连接并让它订阅Event Grid以接收我的测试消息。
  39. - 使用Node.js编写一个'服务器'以连接到Azure Relay以接收数据。在这种情况下,Azure Relay充当客户端,向服务器代码发送请求消息。
  40. - 将此服务器代码包装在我的Playwright测试用例的test.beforeAll()中,以便每次运行测试时,此HTTP服务器都会开始侦听。
  41. - 当此'服务器'从Azure Relay接收到消息时,它会解析消息并设置任何变量,例如消息ID,以供稍后在我的测试验证中使用。
  42. 我已经为您翻译了所需的部分,不包括代码部分。有关更多详细信息,请访问https://learn.microsoft.com/en-us/azure/azure-relay/relay-hybrid-connections-http-requests-node-get-started。
  43. <details>
  44. <summary>英文:</summary>
  45. I managed to get it working. Here is the general idea behind my solution.
  46. - Create an Azure Relay on Azure cloud, create a hybrid connection and have it subscribed to the
  47. Event Grid to receive my test messages.
  48. - Write a &#39;server&#39; using nodeJs
  49. to hook into the Azure Relay in order to receive data. In this case
  50. the Azure Relay acts as a client which sends request message to the
  51. server code.
  52. - Wrap this server code inside the test.beforeAll() in my
  53. Playwrite test case so that each time the test run, this http server
  54. will start listening.
  55. - When this &#39;server&#39; receives message from Azure Relay, it parses
  56. the message and set any variable such as the message Id
  57. which I can use later in my test validation.
  58. test.beforeAll(async ({ getFunctionAppApiContext, a0UserCredentials }) =&gt;
  59. {
  60. let _response: APIResponse;
  61. let _respBody: any;
  62. let respId: string;
  63. let server: any;
  64. var qs = require(&#39;querystring&#39;);
  65. let eventGridMessage: string;
  66. const https = require(&quot;hyco-https&quot;);
  67. const ns = &#39;&lt;relay_name&gt;.servicebus.windows.net&#39;;
  68. const path = &#39;&lt;hybrid_connection_name&gt;&#39;;
  69. const keyrule = &#39;&lt;hybrid_connection_policy_name&gt;&#39;;
  70. const key = &#39;&lt;hybrid_connection_key&gt;&#39;;
  71. //setup the listener to listen events from Azure Relay
  72. var uri = https.createRelayListenUri(ns, path);
  73. server = https.createRelayedServer(
  74. {
  75. server: uri,
  76. token: () =&gt; https.createRelayToken(uri, keyrule, key),
  77. },
  78. //process request from Azure relay originated from event grid
  79. (req, res) =&gt; {
  80. var bodyStream = &#39;&#39;;
  81. req.on(&#39;data&#39;, function(incomingData) {
  82. bodyStream += incomingData;
  83. });
  84. req.on(&#39;end&#39;, () =&gt; {
  85. eventGridMessage = qs.parse(bodyStream);
  86. });
  87. }).listen();
  88. });
  89. The details that I based my solution on can be found at https://learn.microsoft.com/en-us/azure/azure-relay/relay-hybrid-connections-http-requests-node-get-started
  90. </details>

huangapple
  • 本文由 发表于 2023年6月9日 04:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435480.html
匿名

发表评论

匿名网友

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

确定