Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed flaky test-case in Test Suite: BasicHttpClientTest #693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.RequestBuilder;
Expand All @@ -24,6 +28,7 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;

public class BasicHttpClientTest {
Expand All @@ -44,27 +49,30 @@ public void setUp() {
public void createRequestBuilder() throws IOException {
header.put("Content-Type", "application/x-www-form-urlencoded");
String reqBodyAsString = "{\"Company\":\"Amazon\",\"age\":30,\"worthInBillion\":999.999}";
Set<String> expected = new HashSet<>(Arrays.asList("Company=Amazon", "worthInBillion=999.999", "age=30"));
RequestBuilder requestBuilder = basicHttpClient.createRequestBuilder("/api/v1/founder", "POST", header, reqBodyAsString);
String nameValuePairString = EntityUtils.toString(requestBuilder.getEntity(), "UTF-8");
Set<String> actualNameValuePairSet = getActualSetOfNameValuePairsFromString(EntityUtils.toString(requestBuilder.getEntity(), "UTF-8"));
assertThat(requestBuilder.getMethod(), is("POST"));
assertThat(nameValuePairString, is("Company=Amazon&worthInBillion=999.999&age=30"));
assertThat(actualNameValuePairSet, equalTo(expected));
}

@Test
public void createRequestBuilder_spaceInKeyValue() throws IOException {
header.put("Content-Type", "application/x-www-form-urlencoded");
String reqBodyAsString = "{\"Name\":\"Larry Pg\",\"Company\":\"Amazon\",\"Title\":\"CEO\"}";
Set<String> expected = new HashSet<>(Arrays.asList("Company=Amazon", "Title=CEO", "Name=Larry+Pg"));
RequestBuilder requestBuilder = basicHttpClient.createRequestBuilder("/api/v1/founder", "POST", header, reqBodyAsString);
String nameValuePairString = EntityUtils.toString(requestBuilder.getEntity(), "UTF-8");
assertThat(nameValuePairString, is("Company=Amazon&Title=CEO&Name=Larry+Pg"));
Set<String> actualNameValuePairSet = getActualSetOfNameValuePairsFromString(EntityUtils.toString(requestBuilder.getEntity(), "UTF-8"));
assertThat(actualNameValuePairSet, equalTo(expected));
}

@Test
public void createRequestBuilder_frontSlash() throws IOException {
Set<String> expected = new HashSet<>(Arrays.asList("Company=Amazon", "Title=CEO", "state%2Fregion=singapore+north"));
String reqBodyAsString = "{\"state/region\":\"singapore north\",\"Company\":\"Amazon\",\"Title\":\"CEO\"}";
RequestBuilder requestBuilder = basicHttpClient.createRequestBuilder("/api/v1/founder", "POST", header, reqBodyAsString);
String nameValuePairString = EntityUtils.toString(requestBuilder.getEntity(), "UTF-8");
assertThat(nameValuePairString, is("Company=Amazon&Title=CEO&state%2Fregion=singapore+north"));
Set<String> actualNameValuePairSet = getActualSetOfNameValuePairsFromString(EntityUtils.toString(requestBuilder.getEntity(), "UTF-8"));
assertThat(actualNameValuePairSet, equalTo(expected));
}

@Test
Expand All @@ -88,11 +96,12 @@ public void createRequestBuilder_jsonValue() throws IOException {
" \"type\": \"HeadOffice\"\n" +
" }\n" +
"}";
Set<String> expected = new HashSet<>(Arrays.asList("Company=Amazon", "addresses=%7Bcity%3DNewYork%2C+type%3DHeadOffice%7D"));
RequestBuilder requestBuilder = basicHttpClient.createRequestBuilder("/api/v1/founder", "POST", header, reqBodyAsString);
String nameValuePairString = EntityUtils.toString(requestBuilder.getEntity(), "UTF-8");
Set<String> actualNameValuePairSet = getActualSetOfNameValuePairsFromString(EntityUtils.toString(requestBuilder.getEntity(), "UTF-8"));
assertThat(requestBuilder.getMethod(), is("POST"));
//On the server side: address={city=NewYork, type=HeadOffice}
assertThat(nameValuePairString, is("Company=Amazon&addresses=%7Bcity%3DNewYork%2C+type%3DHeadOffice%7D"));
assertThat(actualNameValuePairSet, equalTo(expected));
}

@Test
Expand Down Expand Up @@ -191,4 +200,8 @@ public void willMockDefaultResponseEncoding() throws Exception {
final String responseBodyActual = (String) basicHttpClient.handleResponse(closeableHttpResponse).getEntity();
assertThat(responseBodyActual, CoreMatchers.is(response));
}

private Set<String> getActualSetOfNameValuePairsFromString(String actual) {
return new HashSet<>(Arrays.asList(actual.split("&")));
}
}
Loading