-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-rest-api] Post executed requests in a curl syntax in allure a…
…ttachments
- Loading branch information
Showing
5 changed files
with
280 additions
and
7 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
vividus-plugin-rest-api/src/main/java/org/vividus/http/CurlUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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_STRING = "' \\\n"; | ||
private static final String DOUBLE_QUOTE_END_OF_STRING = "\" \\\n"; | ||
|
||
private CurlUtils() | ||
{ | ||
} | ||
|
||
public static String buildCurlCommand(HttpRequest request, String mimeType, byte[] body) throws URISyntaxException | ||
{ | ||
return new StringBuilder(getRequestAsString(request)) | ||
.append(getHeadersAsString(request.getHeaders())) | ||
.append(getContentAsString(mimeType, body)) | ||
.toString(); | ||
} | ||
|
||
private static StringBuilder getRequestAsString(HttpRequest request) throws URISyntaxException | ||
{ | ||
return new StringBuilder("curl -X ").append(request.getMethod()).append(" '") | ||
.append(request.getUri()).append(SINGLE_QUOTE_END_OF_STRING); | ||
} | ||
|
||
private static StringBuilder getHeadersAsString(Header[] headers) | ||
{ | ||
StringBuilder headersString = new StringBuilder(); | ||
Stream.of(headers).forEach(h -> headersString.append("-H '").append(h.getName()).append(": ") | ||
.append(h.getValue()).append(SINGLE_QUOTE_END_OF_STRING)); | ||
return headersString; | ||
} | ||
|
||
private static StringBuilder getContentAsString(String mimeType, byte[] body) | ||
{ | ||
StringBuilder bodyString = new StringBuilder(); | ||
if (body != null) | ||
{ | ||
String bodyAsString = new String(body, StandardCharsets.UTF_8); | ||
if (mimeType.contains("multipart")) | ||
{ | ||
return getMultipartDataAsString(bodyAsString); | ||
} | ||
bodyString.append("-d '").append(bodyAsString).append("'"); | ||
} | ||
return bodyString; | ||
} | ||
|
||
private static StringBuilder getMultipartDataAsString(String bodyAsString) | ||
{ | ||
String regex = bodyAsString.split("\\R", 2)[0]; | ||
String[] formDataArray = bodyAsString.split(regex + ".*"); | ||
|
||
StringBuilder multipartDataString = new StringBuilder(); | ||
Stream.of(formDataArray).forEach(e -> | ||
{ | ||
Matcher matcher = NAME_FILE_NAME_PATTERN.matcher(e); | ||
String formStringStart = "-F \""; | ||
if (matcher.find()) | ||
{ | ||
multipartDataString.append(formStringStart).append(matcher.group(1)).append("=@<path-to-file>") | ||
.append(matcher.group(2)).append(DOUBLE_QUOTE_END_OF_STRING); | ||
} | ||
else | ||
{ | ||
matcher = NAME_CONTENT_PATTERN.matcher(e); | ||
if (matcher.find()) | ||
{ | ||
multipartDataString.append(formStringStart).append(matcher.group(1)).append("=") | ||
.append(matcher.group(2)).append(DOUBLE_QUOTE_END_OF_STRING); | ||
} | ||
} | ||
}); | ||
return multipartDataString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
vividus-plugin-rest-api/src/test/java/org/vividus/http/CurlUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* 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.junit.jupiter.api.Assertions.assertThrows; | ||
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 testRequestWithExceptionOnUri() throws URISyntaxException | ||
{ | ||
BasicHttpRequest request = mock(); | ||
when(request.getUri()).thenThrow(URISyntaxException.class); | ||
|
||
assertThrows(URISyntaxException.class, () -> CurlUtils.buildCurlCommand(request, ANY, null)); | ||
} | ||
|
||
@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/' \\\n" | ||
+ "-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" \\ | ||
"""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters