Skip to content

Commit 39842db

Browse files
gg4racehrishileanplum
authored andcommitted
E2-1790 android socket fix (#348)
* check https instead of wss * add tests * isSecure public for tests * try importing WebSocketClient bc test failing * try removing test for build * comment out test methods * try commenting out robolectric * does adding config work? * add more configs * change config * copy over another test calss * add one passing test * add real tests (cherry picked from commit 005b4c0)
1 parent 83d97da commit 39842db

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/Socket.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ private void createSocketClient() {
8181
@Override
8282
public void onError(Exception error) {
8383
Log.e("Development socket error", error);
84+
85+
// if error happens during connecting, reset flag
86+
connecting = false;
8487
}
8588

8689
@Override

AndroidSDKCore/src/main/java/com/leanplum/internal/WebSocketClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,23 @@ public void connect() {
9999
@Override
100100
public void run() {
101101
try {
102-
int port = (mURI.getPort() != -1) ? mURI.getPort() : (mURI.getScheme().equals("wss") ? 443 : 80);
102+
int port = (mURI.getPort() != -1) ? mURI.getPort() : (isSecure() ? 443 : 80);
103103

104104
String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath();
105105
if (!TextUtils.isEmpty(mURI.getQuery())) {
106106
path += "?" + mURI.getQuery();
107107
}
108108

109-
String originScheme = mURI.getScheme().equals("wss") ? "https" : "http";
110109
URI origin = null;
111110
try {
112-
origin = new URI(originScheme, "//" + mURI.getHost(), null);
111+
origin = new URI(mURI.getScheme(), "//" + mURI.getHost(), null);
113112
} catch (URISyntaxException e) {
114113
Util.handleException(e);
115114
}
116115

117116
SocketFactory factory;
118117
try {
119-
factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault();
118+
factory = isSecure() ? getSSLSocketFactory() : SocketFactory.getDefault();
120119
} catch (GeneralSecurityException e) {
121120
Util.handleException(e);
122121
return;
@@ -186,6 +185,10 @@ public void run() {
186185
mThread.start();
187186
}
188187

188+
public boolean isSecure() {
189+
return mURI.getScheme().equals("https");
190+
}
191+
189192
public void disconnect() {
190193
if (mSocket != null) {
191194
mHandler.post(new Runnable() {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2019, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
package com.leanplum.internal;
22+
23+
import com.leanplum.Leanplum;
24+
import com.leanplum.__setup.LeanplumTestApp;
25+
26+
import org.junit.Before;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
31+
import org.powermock.core.classloader.annotations.PrepareForTest;
32+
import org.powermock.modules.junit4.rule.PowerMockRule;
33+
import org.robolectric.RobolectricTestRunner;
34+
import org.robolectric.annotation.Config;
35+
36+
import java.net.URI;
37+
import java.net.URISyntaxException;
38+
39+
import static org.junit.Assert.assertFalse;
40+
import static org.junit.Assert.assertTrue;
41+
import static org.powermock.api.mockito.PowerMockito.spy;
42+
43+
/**
44+
* Tests for {@link WebSocketClient} class.
45+
*
46+
* @author Grace Gu
47+
*/
48+
@RunWith(RobolectricTestRunner.class)
49+
@Config(
50+
sdk = 16,
51+
application = LeanplumTestApp.class
52+
)
53+
@PowerMockIgnore({
54+
"org.mockito.*",
55+
"org.robolectric.*",
56+
"org.json.*",
57+
"org.powermock.*",
58+
"android.*"
59+
})
60+
@PrepareForTest({Leanplum.class, Util.class, WebSocketClient.class, RequestOld.class})
61+
public class WebSocketClientTest {
62+
@Rule
63+
public PowerMockRule rule = new PowerMockRule();
64+
65+
/**
66+
* Runs before every test case.
67+
*/
68+
@Before
69+
public void setUp() {
70+
spy(WebSocketClient.class);
71+
}
72+
73+
// java doesn't support wss so we expect WebSocketClient to respect http vs https
74+
@Test
75+
public void testIsSecure() throws URISyntaxException {
76+
WebSocketClient webSocketClient = new WebSocketClient(new URI("https://dev.leanplum.com"), null, null);
77+
assertTrue(webSocketClient.isSecure());
78+
assertTrue(true);
79+
}
80+
81+
@Test
82+
public void testIsNotSecure() throws URISyntaxException {
83+
WebSocketClient webSocketClient = new WebSocketClient(new URI("http://dev.leanplum.com"), null, null);
84+
assertFalse(webSocketClient.isSecure());
85+
}
86+
}

0 commit comments

Comments
 (0)