cz.msebera.android.httpclient.entity.StringEntity Java Examples

The following examples show how to use cz.msebera.android.httpclient.entity.StringEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TokenRegistryTest.java    From pusher-websocket-android with MIT License 6 votes vote down vote up
@Test
public void uploadsOnInvalidClientId() throws Exception {
    JSONObject json = new JSONObject();
    json.put("platform_type", PlatformType.GCM.toString());
    json.put("token", "token-woot");
    json.put("app_key", "app-key-lala");
    StringEntity params = new StringEntity(json.toString(), "UTF-8");
    tokenRegistry.onInvalidClientId(params);
    verify(client).post(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients"),
            eq(params),
            eq("application/json"),
            eq(tokenUploadHandler)
    );
}
 
Example #2
Source File: TokenRegistry.java    From pusher-websocket-android with MIT License 6 votes vote down vote up
private void update(String token, String cachedId) throws JSONException {
    String url = options.buildNotificationURL("/clients/" + cachedId + "/token");
    AsyncHttpClient client = factory.newHttpClient();
    StringEntity params = createRegistrationJSON(token);

    AsyncHttpResponseHandler handler = factory.newTokenUpdateHandler(
            cachedId, params, context, listenerStack, this
    );
    client.put(context, url, params, "application/json", handler);
}
 
Example #3
Source File: TokenRegistryTest.java    From pusher-websocket-android with MIT License 6 votes vote down vote up
@Test
public void receiveUploadsWhenNoCachedId() throws Exception {
    tokenRegistry.receive("mysuperspecialfcmtoken");
    verify(factory).newTokenUploadHandler(context, stack);

    ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class);

    verify(client).post(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients"),
            (HttpEntity) paramsCaptor.capture(),
            eq("application/json"),
            eq(tokenUploadHandler)
    );

    // test proper params sent
    HttpEntity params = (HttpEntity) paramsCaptor.getValue();
    JSONAssert.assertEquals(
            EntityUtils.toString(params),
            "{\"platform_type\":\"fcm\",\"token\":\"mysuperspecialfcmtoken\",\"app_key\":\"superkey\"}",
            true
    );
}
 
Example #4
Source File: SubscriptionManager.java    From pusher-websocket-android with MIT License 6 votes vote down vote up
public void sendSubscription(Subscription subscription) {
    String interest =  subscription.getInterest();
    InterestSubscriptionChange change = subscription.getChange();

    JSONObject json = new JSONObject();
    try {
        json.put("app_key", appKey);
    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
    }
    StringEntity entity = new StringEntity(json.toString(), "UTF-8");

    String url = options.buildNotificationURL("/clients/" + clientId + "/interests/" + interest);
    ResponseHandlerInterface handler = factory.newSubscriptionChangeHandler(subscription);
    AsyncHttpClient client = factory.newHttpClient();
    switch (change) {
        case SUBSCRIBE:
            client.post(context, url, entity, "application/json", handler);
            break;
        case UNSUBSCRIBE:
            client.delete(context, url, entity, "application/json", handler);
            break;
    }
}
 
Example #5
Source File: SubscriptionManagerTest.java    From pusher-websocket-android with MIT License 6 votes vote down vote up
@Test
public void testSubscribe() throws IOException {
    Subscription sub = new Subscription("donuts",
            InterestSubscriptionChange.SUBSCRIBE,
            listener);
    subscriptionManager.sendSubscription(sub);

    ArgumentCaptor<Subscription> subCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(factory).newSubscriptionChangeHandler(sub);

    ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class);
    verify(client).post(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/123-456/interests/donuts"),
            (HttpEntity) paramsCaptor.capture(),
            eq("application/json"),
            eq(subscriptionChangeHandler)
    );
    StringEntity params = (StringEntity) paramsCaptor.getValue();
    assertEquals(
            "{\"app_key\":\"super-cool-key\"}",
            EntityUtils.toString(params)
    );
}
 
Example #6
Source File: TokenRegistry.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
private StringEntity createRegistrationJSON(String token) throws JSONException {
    JSONObject params = new JSONObject();
    params.put("platform_type", platformType.toString());
    params.put("token", token);
    params.put("app_key", appKey);
    return new StringEntity(params.toString(), "UTF-8");
}
 
Example #7
Source File: TokenRegistryTest.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
@Test
public void receivesUpdatesWhenCachedId() throws Exception {
    PreferenceManager.
            getDefaultSharedPreferences(context).
            edit().
            putString("__pusher__client__key__fcm__superkey", "this-is-the-client-id")
            .apply();

    tokenRegistry.receive("woot-token-woot");
    ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class);

    verify(factory).newTokenUpdateHandler(
            eq("this-is-the-client-id"),
            (StringEntity) paramsCaptor.capture(),
            eq(context),
            eq(stack),
            eq(tokenRegistry)
    );


    StringEntity sentParams = (StringEntity) paramsCaptor.getValue();

    verify(client).put(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/this-is-the-client-id/token"),
            eq(sentParams),
            eq("application/json"),
            eq(tokenUpdateHandler)
    );

    JSONAssert.assertEquals(EntityUtils.toString((HttpEntity) paramsCaptor.getValue()),
            "{\"platform_type\":\"fcm\",\"token\":\"woot-token-woot\",\"app_key\":\"superkey\"}",
            true
    );
}
 
Example #8
Source File: TokenUpdateHandler.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
public TokenUpdateHandler(String cachedId, StringEntity retryParams, Context context, RegistrationListenerStack listenerStack, InvalidClientIdHandler invalidClientIdHandler) {
    this.cachedId = cachedId;
    this.retryParams = retryParams;
    this.context = context;
    this.listenerStack = listenerStack;
    this.invalidClientIdHandler = invalidClientIdHandler;
}
 
Example #9
Source File: TokenRegistryTest.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply();

    when(factory.newHttpClient()).thenReturn(client);
    when(factory.newTokenUploadHandler(any(Context.class), any(RegistrationListenerStack.class))).thenReturn(tokenUploadHandler);
    when(factory.newTokenUpdateHandler(any(String.class), any(StringEntity.class), any(Context.class), any(RegistrationListenerStack.class), any(InvalidClientIdHandler.class))).thenReturn(tokenUpdateHandler);

    stack = new RegistrationListenerStack();
    tokenRegistry = new TokenRegistry("superkey", stack, context, PlatformType.FCM, options, factory);
}
 
Example #10
Source File: TokenUpdateHandlerTest.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    JSONObject json = new JSONObject();
    json.put("platform_type", PlatformType.GCM.toString());
    json.put("token", "token-woot");
    json.put("app_key", "app-key-lala");
    params = new StringEntity(json.toString(), "UTF-8");

    tokenUpdateHandler = new TokenUpdateHandler(
            "cached-id", params, context,listenerStack, invalidClientIdHandler
    );
}
 
Example #11
Source File: SubscriptionManagerTest.java    From pusher-websocket-android with MIT License 5 votes vote down vote up
@Test
public void testUnsubscribe() throws IOException {
    Subscription subscription = new Subscription(
            "donuts",
            InterestSubscriptionChange.UNSUBSCRIBE,
            listener);
    subscriptionManager.sendSubscription(subscription);

    ArgumentCaptor<Subscription> subCaptor = ArgumentCaptor.forClass(Subscription.class);

    verify(factory).newSubscriptionChangeHandler(
            subscription
    );

    ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class);
    verify(client).delete(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/123-456/interests/donuts"),
            (HttpEntity) paramsCaptor.capture(),
            eq("application/json"),
            eq(subscriptionChangeHandler)
    );
    StringEntity params = (StringEntity) paramsCaptor.getValue();
    assertEquals(
            "{\"app_key\":\"super-cool-key\"}",
            EntityUtils.toString(params)
    );
}
 
Example #12
Source File: PusherAndroidFactory.java    From pusher-websocket-android with MIT License 4 votes vote down vote up
public TokenUpdateHandler newTokenUpdateHandler(String cachedId, StringEntity retryParams, Context context, RegistrationListenerStack listenerStack, InvalidClientIdHandler invalidClientIdHandler) {
    return new TokenUpdateHandler(cachedId, retryParams, context, listenerStack, invalidClientIdHandler);
}
 
Example #13
Source File: SubscriptionManagerTest.java    From pusher-websocket-android with MIT License 4 votes vote down vote up
@Test
public void testSendingListOfSubscriptions() throws IOException {
    Subscription sub1 = new Subscription("kittens", InterestSubscriptionChange.SUBSCRIBE, listener);
    Subscription sub2 = new Subscription("donuts", InterestSubscriptionChange.UNSUBSCRIBE, listener);
    List<Subscription> list = new ArrayList<>(Arrays.asList(sub1, sub2));
    subscriptionManager.sendSubscriptions(list);

    ArgumentCaptor<Subscription> subCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(factory).newSubscriptionChangeHandler(sub1);

    ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class);

    InOrder inOrder = inOrder(client);
    inOrder.verify(client).post(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/123-456/interests/kittens"),
            (HttpEntity) paramsCaptor.capture(),
            eq("application/json"),
            eq(subscriptionChangeHandler)
    );
    StringEntity params = (StringEntity) paramsCaptor.getValue();
    assertEquals(
            "{\"app_key\":\"super-cool-key\"}",
            EntityUtils.toString(params)
    );

    ArgumentCaptor<Subscription> subCaptor2 = ArgumentCaptor.forClass(Subscription.class);
    verify(factory).newSubscriptionChangeHandler(sub2);

    ArgumentCaptor deleteCaptor = ArgumentCaptor.forClass(StringEntity.class);

    inOrder.verify(client).delete(
            eq(context),
            eq("https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/123-456/interests/donuts"),
            (HttpEntity) deleteCaptor.capture(),
            eq("application/json"),
            eq(subscriptionChangeHandler)
    );

    StringEntity deleteParams = (StringEntity) deleteCaptor.getValue();
    assertEquals(
            "{\"app_key\":\"super-cool-key\"}",
            EntityUtils.toString(deleteParams)
    );
}
 
Example #14
Source File: TokenRegistry.java    From pusher-websocket-android with MIT License 4 votes vote down vote up
private void upload(String token) throws JSONException {
    StringEntity params = createRegistrationJSON(token);
    upload(params);
}
 
Example #15
Source File: TokenRegistry.java    From pusher-websocket-android with MIT License 4 votes vote down vote up
@Override
public void onInvalidClientId(StringEntity params) {
    upload(params);
}
 
Example #16
Source File: TokenRegistry.java    From pusher-websocket-android with MIT License 4 votes vote down vote up
private void upload(StringEntity params) {
    String url = options.buildNotificationURL("/clients");
    AsyncHttpClient client = factory.newHttpClient();
    JsonHttpResponseHandler handler = factory.newTokenUploadHandler(context, listenerStack);
    client.post(context, url, params, "application/json", handler);
}
 
Example #17
Source File: InvalidClientIdHandler.java    From pusher-websocket-android with MIT License votes vote down vote up
void onInvalidClientId(StringEntity params);