如何将 Pact HTTP 请求交互转换为 Curl

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

How to turn Pact HTTP Request interactions into Curl

问题

我一直在寻找一种将我 Pact 测试的交互转化为 cUrl 的方式,以便于进行简便的调试,但不太确定如何做,请指教。

英文:

I've been looking for a way to turn interactions for my Pact tests into a cUrl to easy debugging but not sure how to do that please.

答案1

得分: 2

以下是翻译好的内容:

可能有更好的方法来实现这个,但是我能够使用我编写的 logCurlFromPact() 方法,并从 pactTestTemplate 中调用它来完成。

  1. @TestTemplate
  2. @ExtendWith(PactVerificationInvocationContextProvider.class)
  3. void pactTestTemplate(PactVerificationContext context, HttpRequest request) throws IOException {
  4. request.addHeader("Authorization", AUTHORIZATION_TOKEN);
  5. logCurlFromPact(context, request);
  6. context.verifyInteraction();
  7. }
  1. public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) throws IOException {
  2. String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
  3. String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
  4. String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
  5. String baseUrl = "";
  6. if (method.equals("POST")) {
  7. baseUrl = ((HttpPost) request).getURI().toString();
  8. } else if (method.equals("GET")) {
  9. baseUrl = ((HttpGet) request).getURI().toString();
  10. }
  11. Header[] headers = request.getAllHeaders();
  12. String headersString = "";
  13. for (Header s : headers) {
  14. headersString = headersString + "--header '" + s.getName() + ": " + s.getValue() + "' \\";
  15. }
  16. String curl = "curl '" + baseUrl + "' \\" +
  17. headersString +
  18. "--data-binary '" + bodyParam + "' \\" +
  19. "--compressed \\" +
  20. "--insecure \\" +
  21. "--verbose";
  22. System.out.println(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
  23. }

这里是一个 logback.xml 文件的示例,以防您想要使用日志而不是 System.out.print()

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
  4. <layout class="ch.qos.logback.classic.PatternLayout">
  5. <pattern>%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
  6. </layout>
  7. </appender>
  8. <appender name="File" class="ch.qos.logback.core.FileAppender">
  9. <file>logback.log</file>
  10. <append>true</append>
  11. <layout class="ch.qos.logback.classic.PatternLayout">
  12. <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
  13. </layout>
  14. </appender>
  15. <logger name="com.my.package.packagewiththelogmethod" level="DEBUG">
  16. <appender-ref ref="File"/>
  17. </logger>
  18. <logger name="curl" level="DEBUG">
  19. <appender-ref ref="File"/>
  20. </logger>
  21. <root level="info">
  22. <appender-ref ref="Console" />
  23. </root>
  24. </configuration>

更新

针对 Pact 4.3.2<http.client.version>5.1.2</http.client.version>,以下是 logCurlFromPact 方法的示例更新:

  1. public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) {
  2. String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
  3. String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
  4. String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
  5. String baseUrl = "";
  6. switch (method) {
  7. case "POST":
  8. baseUrl = ((HttpPost) request).getURI().toString();
  9. break;
  10. case "GET":
  11. baseUrl = ((HttpGet) request).getURI().toString();
  12. break;
  13. case "PUT":
  14. baseUrl = ((HttpPut) request).getURI().toString();
  15. break;
  16. case "PATCH":
  17. baseUrl = ((HttpPatch) request).getURI().toString();
  18. break;
  19. case "DELETE":
  20. baseUrl = ((HttpDelete) request).getURI().toString();
  21. break;
  22. }
  23. Header[] headers = request.getAllHeaders();
  24. String headersString = "";
  25. for (Header s : headers) {
  26. headersString = headersString + "--header '" + s.getName() + ": " + s.getValue() + "' \\";
  27. }
  28. String curl = "curl --request " + method + " '" + baseUrl + "' \\" +
  29. headersString +
  30. "--data-binary '" + bodyParam + "' \\" +
  31. "--compressed \\" +
  32. "--insecure \\" +
  33. "--verbose";
  34. log.debug(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
  35. }
英文:

There may be better ways to do this, but I was able to do it using this logCurlFromPact() method that I wrote and calling it from pactTestTemplate.

  1. @TestTemplate
  2. @ExtendWith(PactVerificationInvocationContextProvider.class)
  3. void pactTestTemplate(PactVerificationContext context, HttpRequest request) throws IOException {
  4. request.addHeader(&quot;Authorization&quot;, AUTHORIZATION_TOKEN);
  5. logCurlFromPact(context, request);
  6. context.verifyInteraction();
  7. }
  8. public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) throws IOException {
  9. String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
  10. String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
  11. String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
  12. String baseUrl = &quot;&quot;;
  13. if (method.equals(&quot;POST&quot;)) {
  14. baseUrl = ((HttpPost) request).getURI().toString();
  15. } else if (method.equals(&quot;GET&quot;)) {
  16. baseUrl = ((HttpGet) request).getURI().toString();
  17. }
  18. Header[] headers = request.getAllHeaders();
  19. String headersString = &quot;&quot;;
  20. for (Header s : headers) {
  21. headersString = headersString + &quot;--header &quot; + &quot;&#39;&quot; + s.getName() + &quot;: &quot; + s.getValue() + &quot;&#39;&quot; + &quot;\\&quot; + &quot;\n&quot;;
  22. }
  23. String curl = &quot;&quot; +
  24. &quot;curl &quot; +
  25. &quot;&#39;&quot; + baseUrl + &quot;&#39; \\&quot; + &quot;\n&quot; +
  26. headersString +
  27. &quot;--data-binary &quot; + &quot;&#39;&quot; + bodyParam + &quot;&#39; \\&quot; + &quot;\n&quot; +
  28. &quot;--compressed \\&quot; + &quot;\n&quot; +
  29. &quot;--insecure \\&quot; + &quot;\n&quot; +
  30. &quot;--verbose&quot; +
  31. &quot;&quot;;
  32. System.out.println(curl + &quot;\n\n &quot; + bodyResponse + &quot;\n ---- \n\n&quot;);
  33. }

如何将 Pact HTTP 请求交互转换为 Curl

And here a sample for a logback.xml file in case you may want to use logs instead of System.out.print()

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;configuration&gt;
  3. &lt;appender name=&quot;Console&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
  4. &lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;
  5. &lt;pattern&gt;%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg %n&lt;/pattern&gt;
  6. &lt;/layout&gt;
  7. &lt;/appender&gt;
  8. &lt;appender name=&quot;File&quot; class=&quot;ch.qos.logback.core.FileAppender&quot;&gt;
  9. &lt;file&gt;logback.log&lt;/file&gt;
  10. &lt;append&gt;true&lt;/append&gt;
  11. &lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;
  12. &lt;pattern&gt;%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx&lt;/pattern&gt;
  13. &lt;/layout&gt;
  14. &lt;/appender&gt;
  15. &lt;logger name=&quot;com.my.package.packagewiththelogmethod&quot; level=&quot;DEBUG&quot;&gt;
  16. &lt;appender-ref ref=&quot;File&quot;/&gt;
  17. &lt;/logger&gt;
  18. &lt;logger name=&quot;curl&quot; level=&quot;DEBUG&quot;&gt;
  19. &lt;appender-ref ref=&quot;File&quot;/&gt;
  20. &lt;/logger&gt;
  21. &lt;root level=&quot;info&quot;&gt;
  22. &lt;appender-ref ref=&quot;Console&quot; /&gt;
  23. &lt;/root&gt;
  24. &lt;/configuration&gt;

UPDATE

For Pact 4.3.2 and &lt;http.client.version&gt;5.1.2&lt;/http.client.version&gt;

  1. public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) {
  2. String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
  3. String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
  4. String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
  5. String baseUrl = &quot;&quot;;
  6. switch (method) {
  7. case &quot;POST&quot;:
  8. baseUrl = ((HttpPost) request).getURI().toString();
  9. break;
  10. case &quot;GET&quot;:
  11. baseUrl = ((HttpGet) request).getURI().toString();
  12. break;
  13. case &quot;PUT&quot;:
  14. baseUrl = ((HttpPut) request).getURI().toString();
  15. break;
  16. case &quot;PATCH&quot;:
  17. baseUrl = ((HttpPatch) request).getURI().toString();
  18. break;
  19. case &quot;DELETE&quot;:
  20. baseUrl = ((HttpDelete) request).getURI().toString();
  21. break;
  22. }
  23. Header[] headers = request.getAllHeaders();
  24. String headersString = &quot;&quot;;
  25. for (Header s : headers) {
  26. headersString = headersString + &quot;--header &quot; + &quot;&#39;&quot; + s.getName() + &quot;: &quot; + s.getValue() + &quot;&#39;&quot; + &quot;\\&quot; + &quot;\n&quot;;
  27. }
  28. String curl = &quot;&quot; +
  29. &quot;curl &quot; +
  30. &quot;--request &quot; + method + &quot; &quot; +
  31. &quot;&#39;&quot; + baseUrl + &quot;&#39; \\&quot; + &quot;\n&quot; +
  32. headersString +
  33. &quot;--data-binary &quot; + &quot;&#39;&quot; + bodyParam + &quot;&#39; \\&quot; + &quot;\n&quot; +
  34. &quot;--compressed \\&quot; + &quot;\n&quot; +
  35. &quot;--insecure \\&quot; + &quot;\n&quot; +
  36. &quot;--verbose&quot; +
  37. &quot;&quot;;
  38. log.debug(curl + &quot;\n\n &quot; + bodyResponse + &quot;\n ---- \n\n&quot;);
  39. }

huangapple
  • 本文由 发表于 2020年9月22日 19:16:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64008576.html
匿名

发表评论

匿名网友

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

确定