英文:
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("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() + "'" + "\\" + "\n";
}
String curl = "" +
"curl " +
"'" + baseUrl + "' \\" + "\n" +
headersString +
"--data-binary " + "'" + bodyParam + "' \\" + "\n" +
"--compressed \\" + "\n" +
"--insecure \\" + "\n" +
"--verbose" +
"";
System.out.println(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
}
And here a sample for a logback.xml file in case you may want to use logs instead of 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>
UPDATE
For Pact 4.3.2
and <http.client.version>5.1.2</http.client.version>
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() + "'" + "\\" + "\n";
}
String curl = "" +
"curl " +
"--request " + method + " " +
"'" + baseUrl + "' \\" + "\n" +
headersString +
"--data-binary " + "'" + bodyParam + "' \\" + "\n" +
"--compressed \\" + "\n" +
"--insecure \\" + "\n" +
"--verbose" +
"";
log.debug(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论