Search in sources :

Example 1 with JsonServiceClient

use of net.servicestack.client.JsonServiceClient in project ServiceStack.Java by ServiceStack.

the class JsonServiceClientTests method test_can_serialize_dates_correctly_via_get_request.

public void test_can_serialize_dates_correctly_via_get_request() {
    JsonServiceClient client = new JsonServiceClient("http://test.servicestack.net/");
    EchoTypes request = new EchoTypes();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2015);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    Date dateRepresentation = cal.getTime();
    Date date = dateRepresentation;
    request.setDateTime(date);
    EchoTypes response = client.get(request);
    assertTrue(response != null);
    assertEquals(request.getDateTime(), response.getDateTime());
}
Also used : Calendar(java.util.Calendar) JsonServiceClient(net.servicestack.client.JsonServiceClient) Date(java.util.Date)

Example 2 with JsonServiceClient

use of net.servicestack.client.JsonServiceClient in project ServiceStack.Java by ServiceStack.

the class JsonServiceClientTests method test_does_process_missing_service_correctly.

public void test_does_process_missing_service_correctly() {
    JsonServiceClient localTestClient = new JsonServiceClient("http://techstacks.io/");
    try {
        localTestClient.get(new EchoTypes());
        fail("Should throw");
    } catch (WebServiceException ex) {
        assertEquals(ex.getStatusCode(), 405);
    }
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Example 3 with JsonServiceClient

use of net.servicestack.client.JsonServiceClient in project ServiceStack.Java by ServiceStack.

the class JsonServiceClientTests method test_can_use_request_filter.

public void test_can_use_request_filter() {
    final Boolean[] passTest = { false };
    JsonServiceClient localTestClient = new JsonServiceClient("http://test.servicestack.net/");
    localTestClient.RequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            passTest[0] = true;
        }
    };
    Hello request = new Hello().setName("World");
    HelloResponse response = localTestClient.get(request);
    assertEquals("Hello, World!", response.getResult());
    assertTrue(passTest[0]);
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Example 4 with JsonServiceClient

use of net.servicestack.client.JsonServiceClient in project ServiceStack.Java by ServiceStack.

the class TestServiceTests method test_Does_handle_401_Error_with_empty_ResponseBody.

public void test_Does_handle_401_Error_with_empty_ResponseBody() {
    JsonServiceClient testClient = new JsonServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    WebServiceException thrownError = null;
    JsonServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    TestAuth request = new TestAuth();
    try {
        TestAuthResponse response = testClient.send(request);
    } catch (WebServiceException webEx) {
        thrownError = webEx;
    }
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError);
    ResponseStatus status = thrownError.getResponseStatus();
    assertNull(status);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebServiceException(net.servicestack.client.WebServiceException) ResponseStatus(net.servicestack.client.ResponseStatus) ExceptionFilter(net.servicestack.client.ExceptionFilter) JsonServiceClient(net.servicestack.client.JsonServiceClient) WebServiceException(net.servicestack.client.WebServiceException)

Example 5 with JsonServiceClient

use of net.servicestack.client.JsonServiceClient in project ServiceStack.Java by ServiceStack.

the class TestServiceTests method test_Does_handle_404_Error.

public void test_Does_handle_404_Error() {
    JsonServiceClient testClient = new JsonServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    WebServiceException thrownError = null;
    JsonServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    ThrowType request = new ThrowType().setType("NotFound").setMessage("not here");
    try {
        ThrowTypeResponse response = testClient.put(request);
    } catch (WebServiceException webEx) {
        thrownError = webEx;
    }
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError);
    ResponseStatus status = thrownError.getResponseStatus();
    assertEquals("NotFound", status.getErrorCode());
    assertEquals("not here", status.getMessage());
    assertNotNull(status.getStackTrace());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebServiceException(net.servicestack.client.WebServiceException) ResponseStatus(net.servicestack.client.ResponseStatus) ExceptionFilter(net.servicestack.client.ExceptionFilter) JsonServiceClient(net.servicestack.client.JsonServiceClient) WebServiceException(net.servicestack.client.WebServiceException)

Aggregations

JsonServiceClient (net.servicestack.client.JsonServiceClient)10 HttpURLConnection (java.net.HttpURLConnection)5 WebServiceException (net.servicestack.client.WebServiceException)5 ConnectionFilter (net.servicestack.client.ConnectionFilter)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 ExceptionFilter (net.servicestack.client.ExceptionFilter)2 ResponseStatus (net.servicestack.client.ResponseStatus)2 ArrayList (java.util.ArrayList)1