Skip to content

Commit acc1745

Browse files
author
Javen
committed
Refine - add push param checking, and add tests.
1 parent c752fa2 commit acc1745

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

src/cn/jpush/api/common/HttpProxy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ public class HttpProxy {
1919
public HttpProxy(String host, int port) {
2020
this.host = host;
2121
this.port = port;
22-
23-
LOG.info("Http Proxy - host:" + host + ", port:" + port);
2422
}
2523

2624
public HttpProxy(String host, int port, String username, String password) {
2725
this(host, port);
26+
2827
this.username = username;
2928
this.password = password;
3029
authenticationNeeded = true;
30+
31+
LOG.info("Http Proxy - host:" + host + ", port:" + port
32+
+ ", username:" + username + ", password:" + password);
3133
}
3234

35+
3336
public Proxy getNetProxy() {
3437
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
3538
}

src/cn/jpush/api/push/PushClient.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import cn.jpush.api.common.ResponseWrapper;
99
import cn.jpush.api.common.ServiceHelper;
1010
import cn.jpush.api.push.model.PushPayload;
11+
import cn.jpush.api.utils.StringUtils;
12+
13+
import com.google.common.base.Preconditions;
14+
import com.google.gson.JsonParseException;
15+
import com.google.gson.JsonParser;
1116

1217
/**
1318
* Entrance for sending Push.
@@ -24,7 +29,8 @@ public class PushClient {
2429
public static final String PUSH_PATH = "/v3/push";
2530

2631
private final NativeHttpClient _httpClient;
27-
32+
private JsonParser _jsonParser = new JsonParser();
33+
2834
// If not present, true by default.
2935
private boolean _apnsProduction = true;
3036

@@ -78,6 +84,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
7884
*/
7985
public PushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
8086
this(masterSecret, appKey);
87+
8188
this._apnsProduction = apnsProduction;
8289
this._timeToLive = timeToLive;
8390
this._globalSettingEnabled = true;
@@ -88,6 +95,8 @@ public void setBaseUrl(String baseUrl) {
8895
}
8996

9097
public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
98+
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
99+
91100
if (_globalSettingEnabled) {
92101
pushPayload.resetOptionsTimeToLive(_timeToLive);
93102
pushPayload.resetOptionsApnsProduction(_apnsProduction);
@@ -99,6 +108,15 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
99108
}
100109

101110
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
111+
Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty");
112+
113+
try {
114+
_jsonParser.parse(payloadString);
115+
} catch (JsonParseException e) {
116+
e.printStackTrace();
117+
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
118+
}
119+
102120
ResponseWrapper response = _httpClient.sendPost(_baseUrl, payloadString);
103121

104122
return PushResult.fromResponse(response);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.jpush.api.push;
2+
3+
import org.junit.Test;
4+
5+
import cn.jpush.api.common.APIConnectionException;
6+
import cn.jpush.api.common.APIRequestException;
7+
8+
public class PushClientTest {
9+
private static final String appKey ="dd1066407b044738b6479275";
10+
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
11+
12+
@Test(expected = IllegalArgumentException.class)
13+
public void test_invalid_json() {
14+
PushClient pushClient = new PushClient(masterSecret, appKey);
15+
16+
try {
17+
pushClient.sendPush("{aaa:'a}");
18+
} catch (APIConnectionException e) {
19+
e.printStackTrace();
20+
} catch (APIRequestException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
@Test(expected = IllegalArgumentException.class)
26+
public void test_empty_string() {
27+
PushClient pushClient = new PushClient(masterSecret, appKey);
28+
29+
try {
30+
pushClient.sendPush("");
31+
} catch (APIConnectionException e) {
32+
e.printStackTrace();
33+
} catch (APIRequestException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
39+
40+
41+
42+
}

0 commit comments

Comments
 (0)