-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Do not reuse HTTP client instance (bug fix for #37) * Update README and pom.xml * Update README and pom.xml
- Loading branch information
Showing
9 changed files
with
283 additions
and
18 deletions.
There are no files selected for viewing
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
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
107 changes: 107 additions & 0 deletions
107
src/test/java/com/github/dzieciou/testing/curl/Bug.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,107 @@ | ||
package com.github.dzieciou.testing.curl; | ||
|
||
import io.restassured.config.HttpClientConfig; | ||
import io.restassured.config.RestAssuredConfig; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.impl.client.AbstractHttpClient; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.mockserver.client.MockServerClient; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import static com.github.dzieciou.testing.curl.CommandExecutor.runCommand; | ||
import static io.restassured.RestAssured.config; | ||
import static io.restassured.RestAssured.given; | ||
import static io.restassured.config.HttpClientConfig.httpClientConfig; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockserver.integration.ClientAndServer.startClientAndServer; | ||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
|
||
public class Bug { | ||
|
||
private MockServerClient mockServer; | ||
|
||
String matchingBody = "secret=P@ssword"; | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
mockServer = startClientAndServer(9999); | ||
mockServer.when(request().withBody(matchingBody)).respond(response()); | ||
} | ||
|
||
@Test | ||
public void buggy() throws IOException { | ||
Options options = Options.builder().dontEscapeNonAscii().build(); | ||
final List<String> curls = new ArrayList<>(); | ||
CurlHandler handler = new CurlHandler() { | ||
@Override | ||
public void handle(String curl, Options options) { | ||
curls.add(curl); | ||
} | ||
}; | ||
List<CurlHandler> handlers = Arrays.asList(handler, new CurlLogger()); | ||
RestAssuredConfig restAssuredConfig = getRestAssuredConfig( | ||
new CurlGeneratingInterceptor(options, handlers)); | ||
|
||
|
||
//@formatter:off | ||
given() | ||
.baseUri("http://localhost") | ||
.port(9999) | ||
.config(restAssuredConfig) | ||
.body(matchingBody) | ||
.when() | ||
.post() | ||
.then() | ||
.statusCode(200); | ||
|
||
//@formatter:on | ||
|
||
String curl = curls.get(0); | ||
System.out.println(curl); | ||
String output = runCommand(curl).stdErr; | ||
assertThat(output, containsString("HTTP/1.1 200 OK")); | ||
|
||
} | ||
|
||
@AfterMethod | ||
public void after() { | ||
mockServer.stop(); | ||
} | ||
|
||
private static RestAssuredConfig getRestAssuredConfig( | ||
CurlGeneratingInterceptor curlGeneratingInterceptor) { | ||
return config() | ||
.httpClient(httpClientConfig() | ||
.reuseHttpClientInstance() | ||
.httpClientFactory(new MyHttpClientFactory(curlGeneratingInterceptor))); | ||
} | ||
|
||
private static class MyHttpClientFactory implements HttpClientConfig.HttpClientFactory { | ||
|
||
private final CurlGeneratingInterceptor curlGeneratingInterceptor; | ||
|
||
public MyHttpClientFactory(CurlGeneratingInterceptor curlGeneratingInterceptor) { | ||
this.curlGeneratingInterceptor = curlGeneratingInterceptor; | ||
} | ||
|
||
@Override | ||
public HttpClient createHttpClient() { | ||
@SuppressWarnings("deprecation") AbstractHttpClient client = new DefaultHttpClient(); | ||
client.addRequestInterceptor(curlGeneratingInterceptor); | ||
return client; | ||
} | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/com/github/dzieciou/testing/curl/CommandExecutor.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,50 @@ | ||
package com.github.dzieciou.testing.curl; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CommandExecutor { | ||
|
||
public static CommandOutput runCommand(String command) throws IOException { | ||
String[] commands = tokenize(command).toArray(new String[0]); | ||
Runtime rt = Runtime.getRuntime(); | ||
Process proc = rt.exec(commands); | ||
return new CommandOutput( | ||
readAll(proc.getInputStream()), readAll(proc.getErrorStream()) | ||
); | ||
} | ||
|
||
private static String readAll(InputStream in) throws IOException { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | ||
StringBuffer sb = new StringBuffer(); | ||
String s; | ||
while ((s = reader.readLine()) != null) { | ||
sb.append(s).append("\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static List<String> tokenize(String cmd) { | ||
List<String> tokens = new ArrayList<String>(); | ||
Matcher m = Pattern.compile("([^']\\S*|'.+?')\\s*").matcher(cmd); | ||
while (m.find()) | ||
tokens.add(m.group(1).replace("'", "")); | ||
return tokens; | ||
} | ||
|
||
public static class CommandOutput { | ||
final String stdIn; | ||
final String stdErr; | ||
|
||
public CommandOutput(String stdIn, String stdErr) { | ||
this.stdIn = stdIn; | ||
this.stdErr = stdErr; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.dzieciou.testing.curl; | ||
|
||
public class Try { | ||
|
||
|
||
public static void main(String[] args) { | ||
for(int i=0;i<1024;i++) { | ||
char c = (char)i; | ||
System.out.println(i + "," + escapeAsHex(c) + "," + escapeAsHexFix(c)); | ||
assert escapeAsHex(c).equals(escapeAsHexFix(c)); | ||
} | ||
} | ||
|
||
private static String escapeAsHex(char c) { | ||
int code = (int) c; | ||
String codeAsHex = Integer.toHexString(code); | ||
if (code < 256) { | ||
// Add leading zero when needed to not care about the next character. | ||
return code < 16 ? "\\x0" + codeAsHex : "\\x" + codeAsHex; | ||
} | ||
return String.format("\\u%04x", (int) c); | ||
} | ||
|
||
private static String escapeAsHexFix(char c) { | ||
int code = c; | ||
if (code < 256) { | ||
return String.format("\\x%02x", (int)c); | ||
} | ||
return String.format("\\u%04x", (int) c); | ||
} | ||
|
||
} |
Oops, something went wrong.