This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
/
IntegrationTest.java
166 lines (138 loc) · 5.35 KB
/
IntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.google.cloud.pubsub.client.demos.appengine;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import javax.net.ssl.HttpsURLConnection;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
public class IntegrationTest {
private static final String DEFAULT_TEST_PROJECT_ID = "cloud-pubsub-sample-test";
private static final String TEST_PROJECT_ID_ENV = "TEST_PROJECT_ID";
private static final String PROJECT_ID;
private static final String MESSAGE;
private static final int MAX_RETRY = 10;
private static final long SLEEP_TIME = 1000L;
static {
String projectId = System.getenv(TEST_PROJECT_ID_ENV);
if (projectId == null) {
PROJECT_ID = DEFAULT_TEST_PROJECT_ID;
} else {
PROJECT_ID = projectId;
}
UUID uuid = UUID.randomUUID();
// The '=@~' part is for checking the descrepancy of base64
// variants used on the server side and on the client side.
MESSAGE = "=@~test-message-" + uuid;
}
private String getAppBaseURL() {
return "https://" + PROJECT_ID + ".appspot.com/";
}
private boolean hasCredentials() {
String credentialsFile = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
File f = new File(credentialsFile);
if(f.exists() && !f.isDirectory()) {
return true;
}
return false;
}
@Before
public void setUp() throws Exception {
Assume.assumeTrue(hasCredentials());
}
@Test
public void testTopPage() throws Exception {
String url = getAppBaseURL();
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
// It ensures that our Application Default Credentials work well.
assertEquals(200, responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String contents = response.toString();
assertTrue(contents.contains(PROJECT_ID));
}
private String fetchMessages() throws Exception {
String url = getAppBaseURL() + "fetch_messages";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
@Test
public void testPushHandlerIsProtected() throws Exception {
String url = getAppBaseURL() + "_ah/push-handlers/receive_message";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String urlParams = "p1=a";
byte[] postData = urlParams.getBytes( StandardCharsets.UTF_8 );
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty(
"Content-Length", Integer.toString(postData.length));
con.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded");
try(DataOutputStream wr = new DataOutputStream(
con.getOutputStream())) {
wr.write(postData);
wr.flush();
}
int responseCode = con.getResponseCode();
assertEquals(302, responseCode);
}
@Test
public void testSendMessage() throws Exception {
String url = getAppBaseURL() + "send_message";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String body = "message=" + URLEncoder.encode(MESSAGE, "UTF-8");
con.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded");
con.setRequestMethod("POST");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
// It ensures that the app successfully received the message.
assertEquals(204, responseCode);
// Try fetching the /fetch_messages endpoint and see if the
// response contains the message.
boolean found = false;
for (int i = 0; i < MAX_RETRY; i++) {
Thread.sleep(SLEEP_TIME);
String resp = fetchMessages();
if (resp.contains(MESSAGE)) {
found = true;
break;
}
}
assertTrue("We didn't get the expected message.", found);
}
}