Skip to content

Commit 80b3f67

Browse files
author
Javen
committed
Fix exceptions tests bug
1 parent 3778a0c commit 80b3f67

File tree

8 files changed

+260
-180
lines changed

8 files changed

+260
-180
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

24-
import cn.jpush.api.utils.StringUtils;
25-
2624
import com.google.gson.Gson;
2725
import com.google.gson.GsonBuilder;
2826

@@ -59,9 +57,7 @@ public ResponseWrapper sendRequest(String url, String content, String method, St
5957
conn.setRequestProperty("Connection", "Keep-Alive");
6058
conn.setRequestProperty("Accept-Charset", CHARSET);
6159
conn.setRequestProperty("Charset", CHARSET);
62-
if (!StringUtils.isEmpty(authCode)) {
63-
conn.setRequestProperty("Authorization", authCode);
64-
}
60+
conn.setRequestProperty("Authorization", authCode);
6561

6662
if (METHOD_POST.equals(method)) {
6763
conn.setDoOutput(true); //POST Request

src/cn/jpush/api/push/model/Options.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public void setTimeToLive(long timeToLive) {
4545
this.timeToLive = timeToLive;
4646
}
4747

48+
public int getSendno() {
49+
return this.sendno;
50+
}
51+
4852
@Override
4953
public JsonElement toJSON() {
5054
JsonObject json = new JsonObject();

src/cn/jpush/api/push/model/PushPayload.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public void resetOptionsTimeToLive(long timeToLive) {
7070
}
7171
}
7272

73+
public int getSendno() {
74+
if (null != options) {
75+
return options.getSendno();
76+
}
77+
return 0;
78+
}
79+
7380
@Override
7481
public JsonElement toJSON() {
7582
JsonObject json = new JsonObject();

test/cn/jpush/api/push/mock/BaseMockTests.java

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package cn.jpush.api.push.mock;
22

3+
import static org.junit.Assert.*;
4+
35
import java.io.IOException;
46
import java.net.URL;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
510

611
import org.junit.After;
712
import org.junit.AfterClass;
13+
import org.junit.Before;
814
import org.junit.BeforeClass;
915

1016
import cn.jpush.api.push.PushClient;
17+
import cn.jpush.api.push.PushResult;
1118

19+
import com.google.gson.JsonArray;
20+
import com.google.gson.JsonElement;
1221
import com.google.gson.JsonObject;
22+
import com.google.gson.JsonParser;
1323
import com.google.gson.JsonPrimitive;
24+
import com.squareup.okhttp.mockwebserver.MockResponse;
1425
import com.squareup.okhttp.mockwebserver.MockWebServer;
26+
import com.squareup.okhttp.mockwebserver.RecordedRequest;
1527

1628
public class BaseMockTests {
1729
public static final String REMOTE_HOST = "https://localhost:8081";
@@ -21,6 +33,20 @@ public class BaseMockTests {
2133
public static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
2234

2335
public static final String CONTENT_TYPE_JSON = "application/json";
36+
public static final List<String> SUPPORT_PLATFORM = new ArrayList<String>();
37+
public static final List<String> SUPPORT_AUDIENCE = new ArrayList<String>();
38+
39+
static {
40+
SUPPORT_PLATFORM.add("android");
41+
SUPPORT_PLATFORM.add("ios");
42+
SUPPORT_PLATFORM.add("winphone");
43+
44+
SUPPORT_AUDIENCE.add("tag");
45+
SUPPORT_AUDIENCE.add("tag_and");
46+
SUPPORT_AUDIENCE.add("alias");
47+
SUPPORT_AUDIENCE.add("registration_id");
48+
SUPPORT_AUDIENCE.add("segment");
49+
}
2450

2551
public static final int SUCCEED_RESULT_CODE = 0;
2652
public static final int LACK_OF_PARAMS = 1002;
@@ -41,15 +67,115 @@ public class BaseMockTests {
4167
public static final String ALIAS_NO = "alias_no";
4268
public static final String REGISTRATION_ID1 = "0900e8d85ef";
4369
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
44-
70+
4571
protected static PushClient _client = null;
4672
protected static MockWebServer _mockServer = null;
4773
protected static URL _mockUrl = null;
74+
protected String _currentPayload = null;
75+
protected int _expectedErrorCode = 0;
76+
77+
@Before
78+
public void before() {
79+
}
80+
81+
private void basicRequestCheck() throws Exception {
82+
RecordedRequest request = _mockServer.takeRequest();
83+
assertNotNull("", request.getHeader("Authorization"));
84+
// assertEquals("", _currentPayload, request.getUtf8Body());
85+
assertEquals("", CONTENT_TYPE_JSON, request.getHeader("Content-Type"));
86+
assertEquals("", "keep-alive", request.getHeader("Connection"));
87+
}
4888

4989
@After
50-
public void after() {
51-
// validate input params
90+
public void after() throws Exception {
91+
int sendno = 0;
92+
int responseCode = 200;
93+
int errorCode = 0;
94+
String errorMessage = null;
95+
96+
JsonParser parser = new JsonParser();
97+
JsonObject json = parser.parse(_currentPayload).getAsJsonObject();
98+
99+
if (!json.has("platform") || !json.has("audience")
100+
|| (!json.has("notification") && !json.has("message"))) {
101+
responseCode = 400;
102+
errorCode = 1002;
103+
errorMessage = "Lack of params.";
104+
105+
} else {
106+
107+
JsonElement platform = json.get("platform");
108+
if (platform.isJsonPrimitive() && !"all".equals(platform.getAsString())) {
109+
responseCode = 400;
110+
errorCode = 1003;
111+
errorMessage = "Invalid param - platform string should only be 'all'";
112+
}
113+
114+
if (platform.isJsonArray()) {
115+
JsonArray platformArray = platform.getAsJsonArray();
116+
for (int i = 0; i < platformArray.size(); i++) {
117+
String onePlatform = platformArray.get(i).getAsString();
118+
if (!SUPPORT_PLATFORM.contains(onePlatform)) {
119+
responseCode = 400;
120+
errorCode = 1003;
121+
errorMessage = "Invalid param - platform is invalid - " + onePlatform;
122+
break;
123+
}
124+
}
125+
}
126+
127+
JsonElement audience = json.get("audience");
128+
if (audience.isJsonPrimitive() && !"all".equals(audience.getAsString())) {
129+
responseCode = 400;
130+
errorCode = 1003;
131+
errorMessage = "Invalid param - audience string should only be 'all'";
132+
}
133+
134+
if (audience.isJsonObject()) {
135+
JsonObject audienceObject = audience.getAsJsonObject();
136+
for (Map.Entry<String, JsonElement> entry : audienceObject.entrySet()) {
137+
if (!SUPPORT_AUDIENCE.contains(entry.getKey())) {
138+
responseCode = 400;
139+
errorCode = 1003;
140+
errorMessage = "Invalid param - audience is invalid - " + entry.getKey();
141+
}
142+
}
143+
}
144+
145+
if (json.has("notification")) {
146+
JsonElement notification = json.get("notification");
147+
if (!notification.isJsonObject()) {
148+
responseCode = 400;
149+
errorCode = 1003;
150+
errorMessage = "Invalid param - notification is invalid - should not be string value. ";
151+
} else {
152+
153+
}
154+
155+
} else if (json.has("message")) {
156+
JsonElement message = json.get("message");
157+
if (!message.isJsonObject()) {
158+
responseCode = 400;
159+
errorCode = 1003;
160+
errorMessage = "Invalid param - message is invalid - should not be string value. ";
161+
}
162+
}
163+
164+
}
165+
166+
if (responseCode == 200) {
167+
_mockServer.enqueue(new MockResponse()
168+
.setBody(getResponseOK(111, sendno)));
169+
} else {
170+
_mockServer.enqueue(new MockResponse()
171+
.setResponseCode(responseCode)
172+
.setBody(getResponseError(111, sendno, errorCode, errorMessage)));
173+
}
174+
175+
PushResult result = _client.sendPush(_currentPayload);
176+
assertEquals("", _expectedErrorCode, result.getErrorCode());
52177

178+
basicRequestCheck();
53179
}
54180

55181
@BeforeClass

0 commit comments

Comments
 (0)