Search in sources :

Example 6 with JsonServiceClient

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

the class TechStacksServiceTests method test_does_handle_auth_failure.

public void test_does_handle_auth_failure() {
    JsonServiceClient techStacksClient = new JsonServiceClient("http://techstacks.io/");
    int errorCode = 0;
    try {
        LockTechStack request = new LockTechStack();
        request.setTechnologyStackId((long) 6);
        LockStackResponse res = techStacksClient.post(request);
        fail("Should throw");
    } catch (WebServiceException ex) {
        // private StatusCode has correct code, response status is null due to empty response body.
        errorCode = ex.getStatusCode();
    }
    assertEquals(errorCode, 401);
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Example 7 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("https://servicestack.net/");
    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 8 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 9 with JsonServiceClient

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

the class TestServiceTests method test_does_fire_Request_and_Response_Filters.

public void test_does_fire_Request_and_Response_Filters() {
    JsonServiceClient client = new JsonServiceClient("http://test.servicestack.net");
    final ArrayList<String> events = new ArrayList<>();
    JsonServiceClient.GlobalRequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("GlobalRequestFilter");
        }
    };
    JsonServiceClient.GlobalResponseFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("GlobalResponseFilter");
        }
    };
    client.RequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("RequestFilter");
        }
    };
    client.ResponseFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("ResponseFilter");
        }
    };
    Hello request = new Hello().setName("World");
    HelloResponse response = client.get(request);
    assertEquals("Hello, World!", response.getResult());
    String results = strJoin(", ", events);
    assertEquals("RequestFilter, GlobalRequestFilter, ResponseFilter, GlobalResponseFilter", results);
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) ArrayList(java.util.ArrayList) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Example 10 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)

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