Search in sources :

Example 6 with Request

use of net.morimekta.test.providence.client.Request in project providence by morimekta.

the class HttpClientHandlerTest method testBadResponse.

@Test
@SuppressWarnings("unchecked")
public void testBadResponse() throws IOException, Failure {
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::html));
    try {
        client.test(new Request("request"));
        fail("no exception");
    } catch (PApplicationException e) {
        assertThat(e.getMessage(), is("Unknown content-type in response: text/html;charset=utf-8"));
        assertThat(e.getId(), is(PApplicationExceptionType.INVALID_PROTOCOL));
    }
    client = new TestService.Client(new HttpClientHandler(this::response));
    reply.set(new PServiceCall("foo", PServiceCallType.REPLY, 1, new TestServiceBypass().testResponse("foo")));
    try {
        client.test(new Request("request"));
        fail("no exception");
    } catch (SerializerException e) {
        assertThat(e.getMessage(), is("No such method foo on client.TestService"));
        assertThat(e.getExceptionType(), is(PApplicationExceptionType.UNKNOWN_METHOD));
        assertThat(e.getMethodName(), is("foo"));
        assertThat(e.getSequenceNo(), is(1));
    }
    reply.set(new PServiceCall("test", PServiceCallType.CALL, 2, new TestServiceBypass().testResponse("bar")));
    try {
        client.test(new Request("request"));
        fail("no exception");
    } catch (PApplicationException e) {
        assertThat(e.getMessage(), is("Reply with invalid call type: CALL"));
        assertThat(e.getId(), is(PApplicationExceptionType.INVALID_MESSAGE_TYPE));
    }
    reply.set(new PServiceCall("test", PServiceCallType.REPLY, 100, new TestServiceBypass().testResponse("baz")));
    try {
        client.test(new Request("request"));
        fail("no exception");
    } catch (PApplicationException e) {
        assertThat(e.getMessage(), is("Reply sequence out of order: call = 2, reply = 100"));
        assertThat(e.getId(), is(PApplicationExceptionType.BAD_SEQUENCE_ID));
    }
    reply.set(new PServiceCall("test", PServiceCallType.REPLY, 4, new PApplicationException("foo", PApplicationExceptionType.INTERNAL_ERROR)));
    try {
        client.test(new Request("request"));
        fail("no exception");
    } catch (SerializerException e) {
        assertThat(e.getMessage(), is("Wrong type string(11) for client.TestService.test.response.fail, should be struct(12)"));
        assertThat(e.getExceptionType(), is(PApplicationExceptionType.PROTOCOL_ERROR));
        assertThat(e.getMethodName(), is("test"));
        assertThat(e.getSequenceNo(), is(4));
    }
}
Also used : TestService(net.morimekta.test.providence.client.TestService) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) SerializerException(net.morimekta.providence.serializer.SerializerException) Test(org.junit.Test)

Example 7 with Request

use of net.morimekta.test.providence.client.Request in project providence by morimekta.

the class HttpClientHandlerTest method testSimpleRequest.

@Test
public void testSimpleRequest() throws IOException, TException, Failure {
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider, instrumentation));
    when(impl.test(any(net.morimekta.test.thrift.client.Request.class))).thenReturn(new net.morimekta.test.thrift.client.Response("response"));
    Response response = client.test(new Request("request"));
    waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> contentTypes.size() > 0);
    verify(impl).test(any(net.morimekta.test.thrift.client.Request.class));
    verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
    verifyNoMoreInteractions(impl, instrumentation);
    assertThat(response, is(equalToMessage(new Response("response"))));
    assertThat(contentTypes, is(equalTo(ImmutableList.of("application/vnd.apache.thrift.binary"))));
}
Also used : TestService(net.morimekta.test.providence.client.TestService) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) Response(net.morimekta.test.providence.client.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) PServiceCall(net.morimekta.providence.PServiceCall) Test(org.junit.Test)

Example 8 with Request

use of net.morimekta.test.providence.client.Request in project providence by morimekta.

the class HttpClientHandlerTest method testSimpleRequest_exception.

@Test
public void testSimpleRequest_exception() throws IOException, Failure, TException {
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider, instrumentation));
    when(impl.test(any(net.morimekta.test.thrift.client.Request.class))).thenThrow(new net.morimekta.test.thrift.client.Failure("failure"));
    try {
        client.test(new Request("request"));
        fail("No exception");
    } catch (Failure ex) {
        assertThat(ex.getText(), is("failure"));
    }
    verify(impl).test(any(net.morimekta.test.thrift.client.Request.class));
    verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
    verifyNoMoreInteractions(impl, instrumentation);
}
Also used : TestService(net.morimekta.test.providence.client.TestService) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) PServiceCall(net.morimekta.providence.PServiceCall) Failure(net.morimekta.test.providence.client.Failure) Test(org.junit.Test)

Example 9 with Request

use of net.morimekta.test.providence.client.Request in project providence by morimekta.

the class HttpClientHandlerNetworkTest method testSimpleRequest_connectionRefused_apacheHttpTransport.

@Test
public void testSimpleRequest_connectionRefused_apacheHttpTransport() throws IOException, Failure {
    HttpRequestFactory factory = new ApacheHttpTransport().createRequestFactory();
    GenericUrl url = new GenericUrl("http://localhost:" + (port - 10) + "/" + ENDPOINT);
    TestService.Iface client = new TestService.Client(new HttpClientHandler(() -> url, factory, provider));
    try {
        client.test(new Request("request"));
        fail("No exception");
    } catch (HttpHostConnectException ex) {
        assertThat(ex.getMessage(), allOf(startsWith("Connect to localhost:" + (port - 10) + " failed: "), containsString("Connection refused")));
    }
}
Also used : HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) TestService(net.morimekta.test.providence.client.TestService) Request(net.morimekta.test.providence.client.Request) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) GenericUrl(com.google.api.client.http.GenericUrl) ApacheHttpTransport(com.google.api.client.http.apache.ApacheHttpTransport) Test(org.junit.Test)

Example 10 with Request

use of net.morimekta.test.providence.client.Request in project providence by morimekta.

the class HttpClientHandlerTest method testSimple.

@Test
public void testSimple() throws IOException, TException, Failure {
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint));
    when(impl.test(any(net.morimekta.test.thrift.client.Request.class))).thenReturn(new net.morimekta.test.thrift.client.Response("response"));
    Response response = client.test(new Request("request"));
    waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> contentTypes.size() > 0);
    verify(impl).test(any(net.morimekta.test.thrift.client.Request.class));
    verifyNoMoreInteractions(impl);
    assertThat(response, is(equalToMessage(new Response("response"))));
    assertThat(contentTypes, is(equalTo(ImmutableList.of("application/vnd.apache.thrift.binary"))));
}
Also used : Response(net.morimekta.test.providence.client.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestService(net.morimekta.test.providence.client.TestService) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) Test(org.junit.Test)

Aggregations

Request (net.morimekta.test.providence.client.Request)10 TestService (net.morimekta.test.providence.client.TestService)10 Test (org.junit.Test)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 PServiceCall (net.morimekta.providence.PServiceCall)5 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)4 GenericUrl (com.google.api.client.http.GenericUrl)3 HttpResponseException (com.google.api.client.http.HttpResponseException)3 ApacheHttpTransport (com.google.api.client.http.apache.ApacheHttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Response (net.morimekta.test.providence.client.Response)2 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)2 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 PApplicationException (net.morimekta.providence.PApplicationException)1 SerializerException (net.morimekta.providence.serializer.SerializerException)1 Failure (net.morimekta.test.providence.client.Failure)1