use of com.azure.android.core.http.HttpPipeline 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.HttpPipeline in project azure-sdk-for-android by Azure.
the class RestProxyXMLTests method canReadXMLResponse.
@Test
public void canReadXMLResponse() {
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new MockXMLHTTPClient()).build();
MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, new JacksonSerder());
final SignedIdentifiersWrapperOrError wrapperOrError = new SignedIdentifiersWrapperOrError();
CountDownLatch latch = new CountDownLatch(1);
myXMLService.getContainerACLs(new Callback<Response<SignedIdentifiersWrapper>>() {
@Override
public void onSuccess(Response<SignedIdentifiersWrapper> response) {
try {
wrapperOrError.wrapper = response.getValue();
} finally {
latch.countDown();
}
}
@Override
public void onFailure(Throwable error) {
try {
wrapperOrError.error = error;
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "canReadXMLResponse");
if (wrapperOrError.error != null) {
fail(wrapperOrError.error);
} else {
assertNotNull(wrapperOrError.wrapper);
assertNotNull(wrapperOrError.wrapper.signedIdentifiers());
assertNotEquals(0, wrapperOrError.wrapper.signedIdentifiers().size());
}
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class RestProxyTests method bytesUploadTest.
@Test
public void bytesUploadTest() throws Exception {
byte[] reqContent = "The quick brown fox jumps over the lazy dog".getBytes();
final HttpClient httpClient = createHttpClient();
// Scenario: Log the body so that body buffering/replay behavior is exercised.
//
// Order in which policies applied will be the order in which they added to builder
//
final HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(httpClient).policies(new PortPolicy(getWireMockPort(), true), new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))).build();
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
RestProxy.create(BytesUploadService.class, httpPipeline, SERIALIZER).put(reqContent, reqContent.length, new Callback<Response<HttpBinJSON>>() {
@Override
public void onSuccess(Response<HttpBinJSON> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "bytesUploadTest");
if (cbResult.error != null) {
Assertions.fail(cbResult.error);
} else {
final Response<HttpBinJSON> response = cbResult.response;
assertEquals("The quick brown fox jumps over the lazy dog", response.getValue().data());
}
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class ProtocolPolicyTests method withNoOverwrite.
@Test
public void withNoOverwrite() {
final HttpPipeline pipeline = createPipeline("ftp", false, "https://www.bing.com");
CountDownLatch latch = new CountDownLatch(1);
pipeline.send(createHttpRequest("https://www.bing.com"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "withNoOverwrite");
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class RequestIdPolicyTests method newRequestIdForEachCall.
@Test
public void newRequestIdForEachCall() {
HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
String firstRequestId = null;
@Override
public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
if (firstRequestId != null) {
String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
Assertions.assertNotNull(newRequestId, "newRequestId should not be null");
Assertions.assertNotEquals(newRequestId, firstRequestId);
}
firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
if (firstRequestId == null) {
Assertions.fail("The firstRequestId should not be null.");
}
httpCallback.onSuccess(mockResponse);
}
}).policies(new RequestIdPolicy()).build();
CountDownLatch latch1 = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch1.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch1.countDown();
}
}
});
awaitOnLatch(latch1, "newRequestIdForEachCall");
CountDownLatch latch2 = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch2.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch2.countDown();
}
}
});
awaitOnLatch(latch2, "newRequestIdForEachCall");
}
Aggregations