@Test public void test_onMessage_With_Null_Target_ShouldNotTriggerCallback() throws IOException { final JSPackagerWebSocketClient.JSPackagerCallback callback = mock(JSPackagerWebSocketClient.JSPackagerCallback.class); final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws: client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"target\": null, \"action\": \"actionValue\"}")); verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); }
@Override public void onMessage(ResponseBody response) throws IOException { if (response.contentType() != WebSocket.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType()); return; } try { JsonReader reader = new JsonReader(response.charStream()); Integer version = null; String target = null; String action = null; reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName(); if (JsonToken.NULL == reader.peek()) { reader.skipValue(); continue; } if ("version".equals(field)) { version = reader.nextInt(); } else if ("target".equals(field)) { target = reader.nextString(); } else if ("action".equals(field)) { action = reader.nextString(); } } if (version != 1) { return; } if (target == null || action == null) { return; } triggerMessageCallback(target, action); } catch (IOException e) { abort("Parsing response message from websocket failed", e); } finally { response.close(); } }
JSPackagerWebSocketClient implements WebSocketListener { @Override public void onMessage(ResponseBody response) throws IOException { if (response.contentType() != WebSocket.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType()); return; } try { JsonReader reader = new JsonReader(response.charStream()); Integer version = null; String target = null; String action = null; reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName(); if (JsonToken.NULL == reader.peek()) { reader.skipValue(); continue; } if ("version".equals(field)) { version = reader.nextInt(); } else if ("target".equals(field)) { target = reader.nextString(); } else if ("action".equals(field)) { action = reader.nextString(); } } if (version != 1) { return; } if (target == null || action == null) { return; } triggerMessageCallback(target, action); } catch (IOException e) { abort("Parsing response message from websocket failed", e); } finally { response.close(); } } }
JSPackagerWebSocketClient implements WebSocketListener { @Override public void onMessage(ResponseBody response) throws IOException { if (response.contentType() != WebSocket.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType()); return; } try { JsonReader reader = new JsonReader(response.charStream()); Integer version = null; String target = null; String action = null; reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName(); if (JsonToken.NULL == reader.peek()) { reader.skipValue(); continue; } if ("version".equals(field)) { version = reader.nextInt(); } else if ("target".equals(field)) { target = reader.nextString(); } else if ("action".equals(field)) { action = reader.nextString(); } } if (version != 1) { return; } if (target == null || action == null) { return; } triggerMessageCallback(target, action); } catch (IOException e) { abort("Parsing response message from websocket failed", e); } finally { response.close(); } } JSPackagerWebSocketClient(String url, JSPackagerCallback callback); }
JSPackagerWebSocketClient implements WebSocketListener { @Override public void onMessage(ResponseBody response) throws IOException { if (response.contentType() != WebSocket.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType()); return; } try { JsonReader reader = new JsonReader(response.charStream()); Integer version = null; String target = null; String action = null; reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName(); if (JsonToken.NULL == reader.peek()) { reader.skipValue(); continue; } if ("version".equals(field)) { version = reader.nextInt(); } else if ("target".equals(field)) { target = reader.nextString(); } else if ("action".equals(field)) { action = reader.nextString(); } } if (version != 1) { return; } if (target == null || action == null) { return; } triggerMessageCallback(target, action); } catch (IOException e) { abort("Parsing response message from websocket failed", e); } finally { response.close(); } } JSPackagerWebSocketClient(String url, JSPackagerCallback callback); void connect(); void closeQuietly(); @Override void onMessage(ResponseBody response); @Override void onFailure(IOException e, Response response); @Override void onOpen(WebSocket webSocket, Response response); @Override void onClose(int code, String reason); @Override void onPong(Buffer payload); }
JSPackagerWebSocketClient implements WebSocketListener { @Override public void onMessage(ResponseBody response) throws IOException { if (response.contentType() != WebSocket.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType()); return; } try { JsonReader reader = new JsonReader(response.charStream()); Integer version = null; String target = null; String action = null; reader.beginObject(); while (reader.hasNext()) { String field = reader.nextName(); if (JsonToken.NULL == reader.peek()) { reader.skipValue(); continue; } if ("version".equals(field)) { version = reader.nextInt(); } else if ("target".equals(field)) { target = reader.nextString(); } else if ("action".equals(field)) { action = reader.nextString(); } } if (version != 1) { return; } if (target == null || action == null) { return; } triggerMessageCallback(target, action); } catch (IOException e) { abort("Parsing response message from websocket failed", e); } finally { response.close(); } } JSPackagerWebSocketClient(String url, JSPackagerCallback callback); void connect(); void closeQuietly(); @Override void onMessage(ResponseBody response); @Override void onFailure(IOException e, Response response); @Override void onOpen(WebSocket webSocket, Response response); @Override void onClose(int code, String reason); @Override void onPong(Buffer payload);  }