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

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

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 中调用它来完成。

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) throws IOException {
    request.addHeader("Authorization", AUTHORIZATION_TOKEN);

    logCurlFromPact(context, request);

    context.verifyInteraction();
}
public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) throws IOException {
    String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
    String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
    String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
    
    String baseUrl = "";
    if (method.equals("POST")) {
        baseUrl = ((HttpPost) request).getURI().toString();
    } else if (method.equals("GET")) {
        baseUrl = ((HttpGet) request).getURI().toString();
    }
    
    Header[] headers = request.getAllHeaders();
    String headersString = "";
    
    for (Header s : headers) {
        headersString = headersString + "--header '" + s.getName() + ": " + s.getValue() + "' \\";
    }
    
    String curl = "curl '" + baseUrl + "' \\" +
            headersString +
            "--data-binary '" + bodyParam + "' \\" +
            "--compressed \\" +
            "--insecure \\" +
            "--verbose";
    
    System.out.println(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
}

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

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </layout>
    </appender>

    <appender name="File" class="ch.qos.logback.core.FileAppender">
        <file>logback.log</file>
        <append>true</append>

        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
        </layout>
    </appender>

    <logger name="com.my.package.packagewiththelogmethod" level="DEBUG">
        <appender-ref ref="File"/>
    </logger>

    <logger name="curl" level="DEBUG">
        <appender-ref ref="File"/>
    </logger>

    <root level="info">
        <appender-ref ref="Console" />
    </root>
</configuration>

更新

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

public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) {
    String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
    String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
    String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();

    String baseUrl = "";
    switch (method) {
        case "POST":
            baseUrl = ((HttpPost) request).getURI().toString();
            break;
        case "GET":
            baseUrl = ((HttpGet) request).getURI().toString();
            break;
        case "PUT":
            baseUrl = ((HttpPut) request).getURI().toString();
            break;
        case "PATCH":
            baseUrl = ((HttpPatch) request).getURI().toString();
            break;
        case "DELETE":
            baseUrl = ((HttpDelete) request).getURI().toString();
            break;
    }

    Header[] headers = request.getAllHeaders();
    String headersString = "";

    for (Header s : headers) {
        headersString = headersString + "--header '" + s.getName() + ": " + s.getValue() + "' \\";
    }

    String curl = "curl --request " + method + " '" + baseUrl + "' \\" +
            headersString +
            "--data-binary '" + bodyParam + "' \\" +
            "--compressed \\" +
            "--insecure \\" +
            "--verbose";

    log.debug(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
}
英文:

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.

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) throws IOException {
request.addHeader(&quot;Authorization&quot;, AUTHORIZATION_TOKEN);
logCurlFromPact(context, request);
context.verifyInteraction();
}
public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) throws IOException {
String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
String baseUrl = &quot;&quot;;
if (method.equals(&quot;POST&quot;)) {
baseUrl = ((HttpPost) request).getURI().toString();
} else if (method.equals(&quot;GET&quot;)) {
baseUrl = ((HttpGet) request).getURI().toString();
}
Header[] headers = request.getAllHeaders();
String headersString = &quot;&quot;;
for (Header s : headers) {
headersString = headersString + &quot;--header &quot; + &quot;&#39;&quot; + s.getName() + &quot;: &quot; + s.getValue() + &quot;&#39;&quot; + &quot;\\&quot; + &quot;\n&quot;;
}
String curl = &quot;&quot; +
&quot;curl &quot; +
&quot;&#39;&quot; + baseUrl + &quot;&#39; \\&quot; + &quot;\n&quot; +
headersString +
&quot;--data-binary &quot; + &quot;&#39;&quot; + bodyParam + &quot;&#39; \\&quot; + &quot;\n&quot; +
&quot;--compressed \\&quot; + &quot;\n&quot; +
&quot;--insecure \\&quot; + &quot;\n&quot; +
&quot;--verbose&quot; +
&quot;&quot;;
System.out.println(curl + &quot;\n\n &quot; + bodyResponse + &quot;\n ---- \n\n&quot;);
}

如何将 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()

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
&lt;appender name=&quot;Console&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;
&lt;pattern&gt;%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg %n&lt;/pattern&gt;
&lt;/layout&gt;
&lt;/appender&gt;
&lt;appender name=&quot;File&quot; class=&quot;ch.qos.logback.core.FileAppender&quot;&gt;
&lt;file&gt;logback.log&lt;/file&gt;
&lt;append&gt;true&lt;/append&gt;
&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;
&lt;pattern&gt;%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx&lt;/pattern&gt;
&lt;/layout&gt;
&lt;/appender&gt;
&lt;logger name=&quot;com.my.package.packagewiththelogmethod&quot; level=&quot;DEBUG&quot;&gt;
&lt;appender-ref ref=&quot;File&quot;/&gt;
&lt;/logger&gt;
&lt;logger name=&quot;curl&quot; level=&quot;DEBUG&quot;&gt;
&lt;appender-ref ref=&quot;File&quot;/&gt;
&lt;/logger&gt;
&lt;root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;Console&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;

UPDATE

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

public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) {
String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();
String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();
String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();
String baseUrl = &quot;&quot;;
switch (method) {
case &quot;POST&quot;:
baseUrl = ((HttpPost) request).getURI().toString();
break;
case &quot;GET&quot;:
baseUrl = ((HttpGet) request).getURI().toString();
break;
case &quot;PUT&quot;:
baseUrl = ((HttpPut) request).getURI().toString();
break;
case &quot;PATCH&quot;:
baseUrl = ((HttpPatch) request).getURI().toString();
break;
case &quot;DELETE&quot;:
baseUrl = ((HttpDelete) request).getURI().toString();
break;
}
Header[] headers = request.getAllHeaders();
String headersString = &quot;&quot;;
for (Header s : headers) {
headersString = headersString + &quot;--header &quot; + &quot;&#39;&quot; + s.getName() + &quot;: &quot; + s.getValue() + &quot;&#39;&quot; + &quot;\\&quot; + &quot;\n&quot;;
}
String curl = &quot;&quot; +
&quot;curl &quot; +
&quot;--request &quot; + method + &quot; &quot; +
&quot;&#39;&quot; + baseUrl + &quot;&#39; \\&quot; + &quot;\n&quot; +
headersString +
&quot;--data-binary &quot; + &quot;&#39;&quot; + bodyParam + &quot;&#39; \\&quot; + &quot;\n&quot; +
&quot;--compressed \\&quot; + &quot;\n&quot; +
&quot;--insecure \\&quot; + &quot;\n&quot; +
&quot;--verbose&quot; +
&quot;&quot;;
log.debug(curl + &quot;\n\n &quot; + bodyResponse + &quot;\n ---- \n\n&quot;);
}

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:

确定