Skip to content

Commit

Permalink
[plugin-rest-api] Post executed requests in a curl syntax in allure a…
Browse files Browse the repository at this point in the history
…ttachments
  • Loading branch information
abudevich committed Jan 15, 2024
1 parent e64b716 commit 277386e
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.http;

import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpRequest;

public final class CurlUtils
{
private static final Pattern NAME_FILE_NAME_PATTERN = Pattern.compile("name=\"(.+)\"; filename=\"(.+)\"");
private static final Pattern NAME_CONTENT_PATTERN = Pattern.compile("name=\"(.+)\"\nContent-Type:.+\n\n(.*)");
private static final String SINGLE_QUOTE_END_OF_LINE = "' \\\n";
private static final String DOUBLE_QUOTE_END_OF_LINE = "\" \\\n";

private CurlUtils()
{
}

public static String buildCurlCommand(HttpRequest request, String mimeType, byte[] body) throws URISyntaxException
{
StringBuilder curlCommand = new StringBuilder("curl ");
appendMethodAndUri(curlCommand, request);
appendHeaders(curlCommand, request.getHeaders());
if (body != null)
{
appendBody(curlCommand, mimeType, body);
}
return curlCommand.toString();
}

private static void appendMethodAndUri(StringBuilder curlCommand, HttpRequest request) throws URISyntaxException
{
curlCommand.append("-X ").append(request.getMethod()).append(" '")
.append(request.getUri()).append(SINGLE_QUOTE_END_OF_LINE);
}

private static void appendHeaders(StringBuilder curlCommand, Header[] headers)
{
Stream.of(headers).forEach(h -> curlCommand.append("-H '").append(h.getName()).append(": ")
.append(h.getValue()).append(SINGLE_QUOTE_END_OF_LINE));
}

private static void appendBody(StringBuilder curlCommand, String mimeType, byte[] body)
{
String bodyAsString = new String(body, StandardCharsets.UTF_8);
if (mimeType.contains("multipart"))
{
appendMultipartData(curlCommand, bodyAsString);
return;
}
curlCommand.append("-d '").append(bodyAsString).append(SINGLE_QUOTE_END_OF_LINE);
}

private static void appendMultipartData(StringBuilder curlCommand, String bodyAsString)
{
String regex = bodyAsString.split("\\R", 2)[0];
String[] formDataArray = bodyAsString.split(regex + ".*");

Stream.of(formDataArray).forEach(e ->
{
Matcher matcher = NAME_FILE_NAME_PATTERN.matcher(e);
String formStringStart = "-F \"";
if (matcher.find())
{
curlCommand.append(formStringStart).append(matcher.group(1)).append("=@<path-to-file>")
.append(matcher.group(2)).append(DOUBLE_QUOTE_END_OF_LINE);
}
else
{
matcher = NAME_CONTENT_PATTERN.matcher(e);
if (matcher.find())
{
curlCommand.append(formStringStart).append(matcher.group(1)).append("=")
.append(matcher.group(2)).append(DOUBLE_QUOTE_END_OF_LINE);
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -75,25 +76,37 @@ public void process(HttpRequest request, EntityDetails entityDetails, HttpContex
}
}
}
attachApiMessage("Request: " + request, request.getHeaders(), body, mimeType, -1);
String curlCommand = null;
try
{
curlCommand = CurlUtils.buildCurlCommand(request, mimeType, body);
}
catch (URISyntaxException e)
{
LOGGER.error("Error is occurred on building cURL command", e);
}
attachApiMessage("Request: " + request, request.getHeaders(), body, mimeType, -1, curlCommand);
}

@Override
public void handle(HttpResponse response) throws IOException
public void handle(HttpResponse response)
{
Header[] headers = response.getResponseHeaders();
String attachmentTitle = String.format("Response: %s %s", response.getMethod(), response.getFrom());
String mimeType = MimeTypeUtils.getMimeTypeFromHeadersWithDefault(headers);
attachApiMessage(attachmentTitle, headers, response.getResponseBody(), mimeType, response.getStatusCode());
attachApiMessage(attachmentTitle, headers, response.getResponseBody(), mimeType,
response.getStatusCode(), null);
}

private void attachApiMessage(String title, Header[] headers, byte[] body, String mimeType, int statusCode)
private void attachApiMessage(String title, Header[] headers, byte[] body, String mimeType,
int statusCode, String curlCommand)
{
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("headers", headers);
dataMap.put("body", body != null ? new String(body, StandardCharsets.UTF_8) : null);
dataMap.put("bodyContentType", mimeType);
dataMap.put("statusCode", statusCode);
dataMap.put("curlCommand", curlCommand);

attachmentPublisher.publishAttachment("/org/vividus/http/attachment/api-message.ftl", dataMap, title);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@
</div>
</div>
</#if>

<#if curlCommand??>
<div class="panel panel-info" onclick="copyCurlCommand()">
<div class="panel-heading">
<h4 class="panel-title toggleable">
<a data-toggle="collapse" data-target="#collapse-curl" href="#collapse-curl" class="collapsed">cURL command</a>
</h4>
</div>
<div id="collapse-curl" class="panel-collapse collapse">
<div class="container">
<pre><code class="language-shell">${curlCommand}</code></pre>
</div>
</div>
</div>
</#if>
</div>

<script src="../../webjars/jquery/3.6.4/jquery.min.js"></script>
Expand All @@ -110,6 +125,11 @@
hljs.highlightElement(e);
});
});
function copyCurlCommand() {
var text = document.querySelector('#collapse-curl code').textContent;
navigator.clipboard.writeText(text);
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.http;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.vividus.http.HttpMethod.GET;
import static org.vividus.http.HttpMethod.POST;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.junit.jupiter.api.Test;

class CurlUtilsTests
{
public static final String ANY = "any";
private static final Header[] EMPTY_HEADERS = {};

@Test
void testRequestWithHeadersAndWithoutBody() throws URISyntaxException
{
BasicHttpRequest request = mock();
when(request.getMethod()).thenReturn(GET.name());
when(request.getUri()).thenReturn(new URI("http://get.example.org/"));
Header[] requestHeaders =
{
new BasicHeader("Connection", "keep-alive"),
new BasicHeader("Host", "example.org")
};
when(request.getHeaders()).thenReturn(requestHeaders);

assertEquals(CurlUtils.buildCurlCommand(request, ANY, null),
"""
curl -X GET 'http://get.example.org/' \\
-H 'Connection: keep-alive' \\
-H 'Host: example.org' \\
""");
}

@Test
void testRequestWithHeadersAndWithBody() throws URISyntaxException
{
BasicHttpRequest request = mock();
when(request.getMethod()).thenReturn(POST.name());
when(request.getUri()).thenReturn(new URI("http://post.example.org/"));
when(request.getHeaders()).thenReturn(EMPTY_HEADERS);

assertEquals(CurlUtils.buildCurlCommand(request, ANY, "post body".getBytes(StandardCharsets.UTF_8)),
"""
curl -X POST 'http://post.example.org/' \\
-d 'post body' \\
""");
}

@Test
void testRequestWithFormBody() throws URISyntaxException
{
BasicHttpRequest request = mock();
when(request.getMethod()).thenReturn(POST.name());
when(request.getUri()).thenReturn(new URI("http://post.form.data.example.org/"));
when(request.getHeaders()).thenReturn(EMPTY_HEADERS);

// CHECKSTYLE:OFF
String formDataBody = """
--Bbg5_2qfo5RoGjrGzlpR2MFzlAqzj2ie49bp7
Content-Disposition: form-data; name="string-key"
Content-Type: text/plain
string1
--Bbg5_2qfo5RoGjrGzlpR2MFzlAqzj2ie49bp7
Content-Disposition: form-data; name="binary-key"; filename="raw.txt"
Content-Type: text/plain
raw
--Bbg5_2qfo5RoGjrGzlpR2MFzlAqzj2ie49bp7--
""";
// CHECKSTYLE:ON

assertEquals(CurlUtils.buildCurlCommand(request, "multipart/form-data",
formDataBody.getBytes(StandardCharsets.UTF_8)),
"""
curl -X POST 'http://post.form.data.example.org/' \\
-F "string-key=string1" \\
-F "binary-key=@<path-to-file>raw.txt" \\
""");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -75,6 +76,7 @@ class PublishingAttachmentInterceptorTests
private static final String CONTENT_TYPE = "Content-Type";
private static final String TEXT_PLAIN = "text/plain";
private static final byte[] DATA = "data".getBytes(StandardCharsets.UTF_8);
private static final String SPACE = " ";

@Mock private IAttachmentPublisher attachmentPublisher;
@InjectMocks private PublishingAttachmentInterceptor interceptor;
Expand Down Expand Up @@ -135,7 +137,7 @@ void testNoHttpRequestBodyIsAttached()
{
HttpRequest httpRequest = mock();
when(httpRequest.getHeaders()).thenReturn(new Header[] {});
when(httpRequest.toString()).thenReturn(METHOD + " " + ENDPOINT);
when(httpRequest.toString()).thenReturn(METHOD + SPACE + ENDPOINT);
testNoHttpRequestBodyIsAttached(httpRequest, empty());
}

Expand Down Expand Up @@ -177,6 +179,19 @@ void testNoHttpResponseBodyIsAttached() throws IOException
verifyPublishAttachment(RESPONSE);
}

@Test
void testCurlBuildIsFailed() throws URISyntaxException
{
HttpRequest httpRequest = mock();
when(httpRequest.getHeaders()).thenReturn(new Header[] {});
when(httpRequest.toString()).thenReturn(METHOD + SPACE + ENDPOINT);
String empty = "";
var exception = new URISyntaxException(empty, empty);
when(httpRequest.getUri()).thenThrow(exception);
testNoHttpRequestBodyIsAttached(httpRequest,
equalTo(List.of(error(exception, "Error is occurred on building cURL command"))));
}

private void testHttpRequestIsAttachedSuccessfully(Header contentTypeHeader, HttpEntity httpEntity)
{
var httpRequest = createClassicHttpRequest(new Header[] { contentTypeHeader }, httpEntity);
Expand Down

0 comments on commit 277386e

Please sign in to comment.