Search in sources :

Example 31 with HttpHeaders

use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.

the class RestProxyTests method getRequestWithHeaderParametersAndAnythingReturn.

@Test
public void getRequestWithHeaderParametersAndAnythingReturn() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    createService(Service7.class).getAnything("A", 15, 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, "getRequestWithHeaderParametersAndAnythingReturn");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        Assertions.assertNotNull(response);
        HttpBinJSON json = response.getValue();
        assertNotNull(json);
        assertMatchWithHttpOrHttps("localhost/anything", json.url());
        assertNotNull(json.headers());
        final HttpHeaders headers = new HttpHeaders(json.headers());
        assertEquals("A", headers.getValue("A"));
        assertArrayEquals(new String[] { "A" }, headers.getValues("A"));
        assertEquals("15", headers.getValue("B"));
        assertArrayEquals(new String[] { "15" }, headers.getValues("B"));
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 32 with HttpHeaders

use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.

the class RestProxyTests method headersRequest.

@Test
public void headersRequest() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    createService(Service13.class).get(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, "headersRequest");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        Assertions.assertNotNull(response);
        Assertions.assertNotNull(response.getValue());
        HttpBinJSON json = response.getValue();
        assertNotNull(json);
        assertMatchWithHttpOrHttps("localhost/anything", json.url());
        assertNotNull(json.headers());
        final HttpHeaders headers = new HttpHeaders(json.headers());
        assertEquals("MyHeaderValue", headers.getValue("MyHeader"));
        assertArrayEquals(new String[] { "MyHeaderValue" }, headers.getValues("MyHeader"));
        assertEquals("My,Header,Value", headers.getValue("MyOtherHeader"));
        assertArrayEquals(new String[] { "My", "Header", "Value" }, headers.getValues("MyOtherHeader"));
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 33 with HttpHeaders

use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.

the class RestProxyTests method service20GetResponseBody.

@Test
public void service20GetResponseBody() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    createService(Service20.class).putBody("body string", 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, "service20GetResponseBody");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        assertNotNull(response);
        assertEquals(200, response.getStatusCode());
        final HttpBinJSON body = response.getValue();
        assertNotNull(body);
        assertMatchWithHttpOrHttps("localhost/put", body.url());
        assertEquals("body string", body.data());
        final HttpHeaders headers = response.getHeaders();
        assertNotNull(headers);
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 34 with HttpHeaders

use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method knownError.

@Test
public void knownError() throws Throwable {
    Class<KnownErrorMethods> clazz = KnownErrorMethods.class;
    Method getError409Method = clazz.getDeclaredMethod("getError409", Callback.class);
    HttpResponseMapper mapper = new HttpResponseMapper(getError409Method, extractCallbackType(getError409Method), logger);
    ErrorData409 errorData409 = new ErrorData409(677, "retry after 10 sec");
    JacksonSerder jacksonSerder = new JacksonSerder();
    String wireErrorData409 = jacksonSerder.serialize(errorData409, SerdeEncoding.JSON);
    MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 409, new HttpHeaders(), wireErrorData409.getBytes());
    Retry409Exception ex = null;
    try {
        mapper.map(httpResponse, jacksonSerder);
    } catch (Retry409Exception e) {
        ex = e;
    }
    assertNotNull(ex);
    assertNotNull(ex.getValue());
    ErrorData409 receivedErrorData409 = ex.getValue();
    assertEquals(errorData409.getCode(), receivedErrorData409.getCode());
    assertEquals(errorData409.getMessage(), receivedErrorData409.getMessage());
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 35 with HttpHeaders

use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method unixTimeEncodedContent.

@Test
public void unixTimeEncodedContent() throws Throwable {
    Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
    Method unixTimeMethod = clazz.getDeclaredMethod("unixTime", Callback.class);
    HttpResponseMapper mapperUnixTime = new HttpResponseMapper(unixTimeMethod, extractCallbackType(unixTimeMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    UnixTime unixTime = new UnixTime(offsetDateTime);
    String wireUnixTimeJsonNumber = unixTime.toString();
    MockHttpResponse httpResponseUnixTime = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireUnixTimeJsonNumber.getBytes());
    Response<OffsetDateTime> restResponseUnixTime = (Response<OffsetDateTime>) mapperUnixTime.map(httpResponseUnixTime, jacksonSerder);
    OffsetDateTime dateTimeReceived = restResponseUnixTime.getValue();
    assertNotNull(dateTimeReceived);
    assertEquals(0, dateTimeReceived.compareTo(offsetDateTime));
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) UnixTime(com.azure.android.core.util.UnixTime) OffsetDateTime(org.threeten.bp.OffsetDateTime) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpHeaders (com.azure.android.core.http.HttpHeaders)37 Test (org.junit.jupiter.api.Test)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 HttpResponse (com.azure.android.core.http.HttpResponse)20 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)17 HttpMethod (com.azure.android.core.http.HttpMethod)16 Method (java.lang.reflect.Method)16 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 HashMap (java.util.HashMap)7 HttpRequest (com.azure.android.core.http.HttpRequest)6 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)6 Map (java.util.Map)6 OffsetDateTime (org.threeten.bp.OffsetDateTime)6 Response (com.azure.android.core.rest.Response)5 StreamResponse (com.azure.android.core.rest.StreamResponse)5 List (java.util.List)5 HttpCallback (com.azure.android.core.http.HttpCallback)4 HttpHeader (com.azure.android.core.http.HttpHeader)4 HttpPipeline (com.azure.android.core.http.HttpPipeline)4