use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class HttpRequestMapperTests method stringByteNoContentType.
@Test
public void stringByteNoContentType() throws NoSuchMethodException, IOException {
Class<StringByteNoContentTypeMethods> clazz = StringByteNoContentTypeMethods.class;
Method byteBodyMethod = clazz.getDeclaredMethod("byteBody", (new byte[0]).getClass(), Callback.class);
Method stringBodyMethod = clazz.getDeclaredMethod("stringBody", String.class, Callback.class);
HttpRequestMapper requestMapper = new HttpRequestMapper("https://raw.host.com", byteBodyMethod, new JacksonSerder());
byte[] bytesBody = new byte[2];
bytesBody[0] = 'A';
bytesBody[1] = 'B';
HttpRequest httpRequest = requestMapper.map(toObjectArray(bytesBody));
assertEquals("application/octet-stream", httpRequest.getHeaders().getValue("Content-Type"));
assertArrayEquals(bytesBody, httpRequest.getBody());
requestMapper = new HttpRequestMapper("https://raw.host.com", stringBodyMethod, new JacksonSerder());
httpRequest = requestMapper.map(toObjectArray("hello"));
assertEquals("application/octet-stream", httpRequest.getHeaders().getValue("Content-Type"));
assertEquals("hello", new String(httpRequest.getBody()));
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class RestProxyXMLTests method canWriteXMLRequest.
@Test
public void canWriteXMLRequest() throws Exception {
URL url = getClass().getClassLoader().getResource("GetContainerACLs.xml");
byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
HttpRequest request = new HttpRequest(HttpMethod.PUT, "http://unused/SetContainerACLs");
request.setBody(bytes);
SignedIdentifierInner si = new SignedIdentifierInner();
si.withId("MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=");
AccessPolicy ap = new AccessPolicy();
ap.withStart(OffsetDateTime.parse("2009-09-28T08:49:37.0000000Z"));
ap.withExpiry(OffsetDateTime.parse("2009-09-29T08:49:37.0000000Z"));
ap.withPermission("rwd");
si.withAccessPolicy(ap);
List<SignedIdentifierInner> expectedAcls = Collections.singletonList(si);
JacksonSerder serderAdapter = new JacksonSerder();
MockXMLReceiverClient httpClient = new MockXMLReceiverClient();
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(httpClient).build();
MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, serderAdapter);
SignedIdentifiersWrapper wrapper = new SignedIdentifiersWrapper(expectedAcls);
CountDownLatch latch = new CountDownLatch(1);
myXMLService.setContainerACLs(wrapper, new Callback<Response<Void>>() {
@Override
public void onSuccess(Response<Void> response) {
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
latch.countDown();
}
});
awaitOnLatch(latch, "canWriteXMLRequest");
SignedIdentifiersWrapper actualAclsWrapped = serderAdapter.deserialize(new String(httpClient.receivedBytes, StandardCharsets.UTF_8), SignedIdentifiersWrapper.class, SerdeEncoding.XML);
List<SignedIdentifierInner> actualAcls = actualAclsWrapped.signedIdentifiers();
// Ideally we'd just check for "things that matter" about the XML-- e.g. the tag names, structure, and attributes needs to be the same,
// but it doesn't matter if one document has a trailing newline or has UTF-8 in the header instead of utf-8, or if comments are missing.
assertEquals(expectedAcls.size(), actualAcls.size());
assertEquals(expectedAcls.get(0).id(), actualAcls.get(0).id());
assertEquals(expectedAcls.get(0).accessPolicy().expiry(), actualAcls.get(0).accessPolicy().expiry());
assertEquals(expectedAcls.get(0).accessPolicy().start(), actualAcls.get(0).accessPolicy().start());
assertEquals(expectedAcls.get(0).accessPolicy().permission(), actualAcls.get(0).accessPolicy().permission());
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class HttpClientTests method sendRequest.
private String sendRequest(String requestPath, String method) {
CountDownLatch latch = new CountDownLatch(1);
final String[] content = new String[1];
final Throwable[] throwable = new Throwable[1];
content[0] = null;
throwable[0] = null;
createHttpClient().send(new HttpRequest(HttpMethod.GET, REQUEST_HOST + ":" + getWireMockPort() + "/" + requestPath), CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
try {
content[0] = response.getBodyAsString();
} finally {
latch.countDown();
}
}
@Override
public void onError(Throwable error) {
try {
throwable[0] = error;
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "method");
if (throwable[0] != null) {
throw new RuntimeException(throwable[0]);
} else {
return content[0];
}
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class HttpRequestMapperTests method jsonBodySubstitution.
@ParameterizedTest
@MethodSource("jsonBodySubstitutionSupplier")
public void jsonBodySubstitution(Method method, Object[] arguments, String expectedBodyContentType, Object expectedBody) throws IOException {
HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
HttpRequest httpRequest = mapper.map(arguments);
byte[] content = httpRequest.getBody();
if (expectedBody == null) {
Assertions.assertNull(content);
} else {
assertEquals(expectedBodyContentType, httpRequest.getHeaders().getValue("Content-Type"));
assertEquals(expectedBody, new String(content, StandardCharsets.UTF_8));
}
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class RestProxy method invoke.
@Override
public Object invoke(final Object restProxy, final Method swaggerMethod, final Object[] swaggerMethodArgs) {
final SwaggerMethodParser methodParser = this.interfaceParser.getMethodParser(swaggerMethod, this.logger);
final Callback<Response<?>> restCallback;
restCallback = (Callback<Response<?>>) swaggerMethodArgs[methodParser.callbackArgIndex];
Objects.requireNonNull(restCallback);
final CancellationToken cancellationToken;
if (methodParser.cancellationTokenArgIndex == -1) {
cancellationToken = CancellationToken.NONE;
} else {
cancellationToken = (CancellationToken) swaggerMethodArgs[methodParser.cancellationTokenArgIndex];
}
final HttpRequest httpRequest;
try {
httpRequest = methodParser.mapToHttpRequest(swaggerMethodArgs);
} catch (IOException e) {
restCallback.onFailure(e);
return null;
} catch (HttpResponseException e) {
restCallback.onFailure(e);
return null;
}
this.httpPipeline.send(httpRequest, RequestContext.NONE, cancellationToken, new HttpPipelineCallback(methodParser, restCallback));
return null;
}
Aggregations