use of com.azure.android.core.test.implementation.entities.HttpBinFormDataJSON in project azure-sdk-for-android by Azure.
the class MockHttpClient method send.
@Override
public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
HttpResponse response = null;
try {
final URL requestUrl = request.getUrl();
final String requestHost = requestUrl.getHost();
final String contentType = request.getHeaders().getValue("Content-Type");
if ("localhost".equalsIgnoreCase(requestHost)) {
final String requestPath = requestUrl.getPath();
final String requestPathLower = requestPath.toLowerCase();
if (requestPathLower.startsWith("/anything")) {
if ("HEAD".equals(request.getHttpMethod().name())) {
response = new MockHttpResponse(request, 200, new byte[0]);
} else {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
}
} else if (requestPathLower.startsWith("/bytes/")) {
final String byteCountString = requestPath.substring("/bytes/".length());
final int byteCount = Integer.parseInt(byteCountString);
HttpHeaders newHeaders = new HttpHeaders(RESPONSE_HEADERS).put("Content-Type", "application/octet-stream").put("Content-Length", Integer.toString(byteCount));
response = new MockHttpResponse(request, 200, newHeaders, byteCount == 0 ? null : new byte[byteCount]);
} else if (requestPathLower.startsWith("/base64urlbytes/")) {
final String byteCountString = requestPath.substring("/base64urlbytes/".length());
final int byteCount = Integer.parseInt(byteCountString);
final byte[] bytes = new byte[byteCount];
for (int i = 0; i < byteCount; ++i) {
bytes[i] = (byte) i;
}
final Base64Url base64EncodedBytes = bytes.length == 0 ? null : Base64Url.encode(bytes);
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytes);
} else if (requestPathLower.equals("/base64urllistofbytes")) {
final List<String> base64EncodedBytesList = new ArrayList<>();
for (int i = 0; i < 3; ++i) {
final int byteCount = (i + 1) * 10;
final byte[] bytes = new byte[byteCount];
for (int j = 0; j < byteCount; ++j) {
bytes[j] = (byte) j;
}
base64EncodedBytesList.add(Base64Url.encode(bytes).toString());
}
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytesList);
} else if (requestPathLower.equals("/base64urllistoflistofbytes")) {
final List<List<String>> result = new ArrayList<>();
for (int i = 0; i < 2; ++i) {
final List<String> innerList = new ArrayList<>();
for (int j = 0; j < (i + 1) * 2; ++j) {
final int byteCount = (j + 1) * 5;
final byte[] bytes = new byte[byteCount];
for (int k = 0; k < byteCount; ++k) {
bytes[k] = (byte) k;
}
innerList.add(Base64Url.encode(bytes).toString());
}
result.add(innerList);
}
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
} else if (requestPathLower.equals("/base64urlmapofbytes")) {
final Map<String, String> result = new HashMap<>();
for (int i = 0; i < 2; ++i) {
final String key = Integer.toString(i);
final int byteCount = (i + 1) * 10;
final byte[] bytes = new byte[byteCount];
for (int j = 0; j < byteCount; ++j) {
bytes[j] = (byte) j;
}
result.put(key, Base64Url.encode(bytes).toString());
}
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
} else if (requestPathLower.equals("/datetimerfc1123")) {
final DateTimeRfc1123 now = new DateTimeRfc1123(OffsetDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC));
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, now.toString());
} else if (requestPathLower.equals("/unixtime")) {
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, 0);
} else if (requestPathLower.equals("/delete")) {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
response = new MockHttpResponse(request, 200, json);
} else if (requestPathLower.equals("/get")) {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
} else if (requestPathLower.equals("/patch")) {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
response = new MockHttpResponse(request, 200, json);
} else if (requestPathLower.equals("/post")) {
if (contentType != null && contentType.contains("x-www-form-urlencoded")) {
Map<String, String> parsed = bodyToMap(request);
final HttpBinFormDataJSON json = new HttpBinFormDataJSON();
HttpBinFormDataJSON.Form form = new HttpBinFormDataJSON.Form();
form.customerName(parsed.get("custname"));
form.customerEmail(parsed.get("custemail"));
form.customerTelephone(parsed.get("custtel"));
form.pizzaSize(HttpBinFormDataJSON.PizzaSize.valueOf(parsed.get("size")));
form.toppings(Arrays.asList(parsed.get("toppings").split(",")));
json.form(form);
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
} else {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
}
} else if (requestPathLower.equals("/put")) {
final HttpBinJSON json = new HttpBinJSON();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
} else if (requestPathLower.startsWith("/status/")) {
final String statusCodeString = requestPathLower.substring("/status/".length());
final int statusCode = Integer.parseInt(statusCodeString);
response = new MockHttpResponse(request, statusCode);
}
} else if ("echo.org".equalsIgnoreCase(requestHost)) {
response = new MockHttpResponse(request, 200, new HttpHeaders(request.getHeaders()), request.getBody());
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
if (response == null) {
response = new MockHttpResponse(request, 500);
}
httpCallback.onSuccess(response);
}
use of com.azure.android.core.test.implementation.entities.HttpBinFormDataJSON in project azure-sdk-for-android by Azure.
the class RestProxyTests method postUrlForm.
@Test
public void postUrlForm() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinFormDataJSON> cbResult = new CallbackResult<>();
Service26 service = createService(Service26.class);
service.postForm("Foo", "123", "foo@bar.com", HttpBinFormDataJSON.PizzaSize.LARGE, Arrays.asList("Bacon", "Onion"), new Callback<Response<HttpBinFormDataJSON>>() {
@Override
public void onSuccess(Response<HttpBinFormDataJSON> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "postUrlForm");
if (cbResult.error != null) {
Assertions.fail(cbResult.error);
} else {
final Response<HttpBinFormDataJSON> response = cbResult.response;
assertNotNull(response);
assertEquals(200, response.getStatusCode());
HttpBinFormDataJSON result = response.getValue();
assertNotNull(result);
assertNotNull(result.form());
assertEquals("Foo", result.form().customerName());
assertEquals("123", result.form().customerTelephone());
assertEquals("foo%40bar.com", result.form().customerEmail());
assertEquals(HttpBinFormDataJSON.PizzaSize.LARGE, result.form().pizzaSize());
assertEquals(2, result.form().toppings().size());
assertEquals("Bacon", result.form().toppings().get(0));
assertEquals("Onion", result.form().toppings().get(1));
}
}
use of com.azure.android.core.test.implementation.entities.HttpBinFormDataJSON in project azure-sdk-for-android by Azure.
the class RestProxyTests method postUrlFormEncoded.
@Test
public void postUrlFormEncoded() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinFormDataJSON> cbResult = new CallbackResult<>();
Service26 service = createService(Service26.class);
service.postEncodedForm("Foo", "123", "foo@bar.com", HttpBinFormDataJSON.PizzaSize.LARGE, Arrays.asList("Bacon", "Onion"), new Callback<Response<HttpBinFormDataJSON>>() {
@Override
public void onSuccess(Response<HttpBinFormDataJSON> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "postUrlFormEncoded");
if (cbResult.error != null) {
Assertions.fail(cbResult.error);
} else {
final Response<HttpBinFormDataJSON> response = cbResult.response;
assertNotNull(response);
assertEquals(200, response.getStatusCode());
HttpBinFormDataJSON result = response.getValue();
assertNotNull(result);
assertNotNull(result.form());
assertEquals("Foo", result.form().customerName());
assertEquals("123", result.form().customerTelephone());
assertEquals("foo@bar.com", result.form().customerEmail());
assertEquals(HttpBinFormDataJSON.PizzaSize.LARGE, result.form().pizzaSize());
assertEquals(2, result.form().toppings().size());
assertEquals("Bacon", result.form().toppings().get(0));
assertEquals("Onion", result.form().toppings().get(1));
}
}
Aggregations