Search in sources :

Example 1 with ConnectionFilter

use of net.servicestack.client.ConnectionFilter 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 2 with ConnectionFilter

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

the class TestServiceTestsAsync method test_Can_send_ReturnVoid_Async.

public void test_Can_send_ReturnVoid_Async() {
    final CountDownLatch signal = new CountDownLatch(1);
    final List<String> sentMethods = new ArrayList<>();
    client.RequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            sentMethods.add(conn.getRequestMethod());
        }
    };
    client.sendAsync(new HelloReturnVoid().setId(1), new AsyncResultVoid() {

        @Override
        public void success() {
            assertEquals(HttpMethods.Post, sentMethods.get(sentMethods.size() - 1));
            signal.countDown();
        }
    });
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) AsyncResultVoid(net.servicestack.client.AsyncResultVoid) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with ConnectionFilter

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

the class TestServiceTests method test_Can_send_ReturnVoid.

public void test_Can_send_ReturnVoid() {
    final List<String> sentMethods = new ArrayList<>();
    client.RequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            sentMethods.add(conn.getRequestMethod());
        }
    };
    client.send(new HelloReturnVoid().setId(1));
    assertEquals(HttpMethods.Post, sentMethods.get(sentMethods.size() - 1));
    client.get(new HelloReturnVoid().setId(2));
    assertEquals(HttpMethods.Get, sentMethods.get(sentMethods.size() - 1));
    client.post(new HelloReturnVoid().setId(3));
    assertEquals(HttpMethods.Post, sentMethods.get(sentMethods.size() - 1));
    client.put(new HelloReturnVoid().setId(4));
    assertEquals(HttpMethods.Put, sentMethods.get(sentMethods.size() - 1));
    client.delete(new HelloReturnVoid().setId(5));
    assertEquals(HttpMethods.Delete, sentMethods.get(sentMethods.size() - 1));
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) ArrayList(java.util.ArrayList)

Example 4 with ConnectionFilter

use of net.servicestack.client.ConnectionFilter 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 5 with ConnectionFilter

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

the class TestServiceTestsAsync method test_does_fire_Request_and_Response_Filters_Async.

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

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("GlobalRequestFilter");
        }
    };
    AndroidServiceClient.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");
    final CountDownLatch signal = new CountDownLatch(1);
    client.getAsync(request, new AsyncResult<HelloResponse>() {

        @Override
        public void success(HelloResponse response) {
            assertEquals("Hello, World!", response.getResult());
            String results = TextUtils.join(", ", events);
            assertEquals("RequestFilter, GlobalRequestFilter, ResponseFilter, GlobalResponseFilter", results);
            AndroidServiceClient.GlobalRequestFilter = null;
            AndroidServiceClient.GlobalResponseFilter = null;
        }

        @Override
        public void complete() {
            signal.countDown();
        }
    });
    assertTrue(signal.await(5, TimeUnit.SECONDS));
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) AndroidServiceClient(net.servicestack.android.AndroidServiceClient) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)7 ConnectionFilter (net.servicestack.client.ConnectionFilter)7 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 JsonServiceClient (net.servicestack.client.JsonServiceClient)3 AsyncResultVoid (net.servicestack.client.AsyncResultVoid)2 AndroidServiceClient (net.servicestack.android.AndroidServiceClient)1