Skip to content

Commit 636f00e

Browse files
authored
Adding connection checks to WebSocket Tests (#263)
* Adding connection checks to WebSocket Tests Signed-off-by: James Sutton <james.sutton@uk.ibm.com> * Making WebSocket Handshake Sec-WebSocket-Key 16 bytes (Issue #234) Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent 63f772c commit 636f00e

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

  • org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test
  • org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/WebSocketTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public void testWebSocketConnect() throws Exception {
131131
}
132132
finally {
133133
if (client != null) {
134+
if(client.isConnected()){
135+
client.disconnectForcibly();
136+
}
134137
log.info("Close...");
135138
client.close();
136139
}
@@ -240,6 +243,9 @@ public void largePayloadTest() throws Exception{
240243
e.printStackTrace();
241244
} finally {
242245
if (client != null) {
246+
if(client.isConnected()){
247+
client.disconnectForcibly();
248+
}
243249
log.info("Close...");
244250
client.close();
245251
}
@@ -263,10 +269,22 @@ public void testBasicAuth() throws Exception {
263269
serverURI.getPath(),
264270
serverURI.getQuery(),
265271
serverURI.getFragment());
266-
267-
IMqttClient client = clientFactory.createMqttClient(serverURIWithUserInfo, clientId);
268-
client.connect();
269-
client.disconnect();
272+
IMqttClient client = null;
273+
try {
274+
client = clientFactory.createMqttClient(serverURIWithUserInfo, clientId);
275+
client.connect();
276+
client.disconnect();
277+
} catch (Exception e){
278+
e.printStackTrace();
279+
} finally {
280+
if (client != null) {
281+
if(client.isConnected()){
282+
client.disconnectForcibly();
283+
}
284+
log.info("Close...");
285+
client.close();
286+
}
287+
}
270288
}
271289

272290
// -------------------------------------------------------------

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketHandshake.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public WebSocketHandshake(InputStream input, OutputStream output, String uri, St
6868
* @throws IOException
6969
*/
7070
public void execute() throws IOException {
71-
String key = "mqtt-" + (System.currentTimeMillis()/1000);
71+
String key = "mqtt3-" + (System.currentTimeMillis()/1000);
7272
String b64Key = Base64.encode(key);
7373
sendHandshakeRequest(b64Key);
7474
receiveHandshakeResponse(b64Key);

0 commit comments

Comments
 (0)