mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 08:05:34 +00:00
Update okhttp3
Reviewed By: bestander Differential Revision: D5078004 fbshipit-source-id: 79c66cedeeb682d8bb4e67798b41115899fd1c81
This commit is contained in:
parent
658f632f59
commit
93a1d592d6
@ -283,10 +283,9 @@ dependencies {
|
|||||||
compile 'com.facebook.fresco:imagepipeline-okhttp3:1.0.1'
|
compile 'com.facebook.fresco:imagepipeline-okhttp3:1.0.1'
|
||||||
compile 'com.facebook.soloader:soloader:0.1.0'
|
compile 'com.facebook.soloader:soloader:0.1.0'
|
||||||
compile 'com.google.code.findbugs:jsr305:3.0.0'
|
compile 'com.google.code.findbugs:jsr305:3.0.0'
|
||||||
compile 'com.squareup.okhttp3:okhttp:3.4.1'
|
compile 'com.squareup.okhttp3:okhttp:3.8.0'
|
||||||
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
|
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.8.0'
|
||||||
compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'
|
compile 'com.squareup.okio:okio:1.13.0'
|
||||||
compile 'com.squareup.okio:okio:1.9.0'
|
|
||||||
compile 'org.webkit:android-jsc:r174650'
|
compile 'org.webkit:android-jsc:r174650'
|
||||||
|
|
||||||
testCompile "junit:junit:${JUNIT_VERSION}"
|
testCompile "junit:junit:${JUNIT_VERSION}"
|
||||||
|
@ -12,7 +12,6 @@ android_library(
|
|||||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3"),
|
react_native_dep("third-party/java/okhttp:okhttp3"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
|
|
||||||
react_native_dep("third-party/java/okio:okio"),
|
react_native_dep("third-party/java/okio:okio"),
|
||||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||||
react_native_target("java/com/facebook/react/common:common"),
|
react_native_target("java/com/facebook/react/common:common"),
|
||||||
|
@ -19,13 +19,9 @@ import com.facebook.react.bridge.Inspector;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.WebSocket;
|
||||||
import okhttp3.ws.WebSocket;
|
import okhttp3.WebSocketListener;
|
||||||
import okhttp3.ws.WebSocketCall;
|
|
||||||
import okhttp3.ws.WebSocketListener;
|
|
||||||
import okio.Buffer;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -175,7 +171,7 @@ public class InspectorPackagerConnection {
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Connection implements WebSocketListener {
|
private class Connection extends WebSocketListener {
|
||||||
private static final int RECONNECT_DELAY_MS = 2000;
|
private static final int RECONNECT_DELAY_MS = 2000;
|
||||||
|
|
||||||
private final String mUrl;
|
private final String mUrl;
|
||||||
@ -196,9 +192,9 @@ public class InspectorPackagerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(IOException e, Response response) {
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
abort("Websocket exception", e);
|
abort("Websocket exception", t);
|
||||||
}
|
}
|
||||||
if (!mClosed) {
|
if (!mClosed) {
|
||||||
reconnect();
|
reconnect();
|
||||||
@ -206,22 +202,16 @@ public class InspectorPackagerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(ResponseBody message) throws IOException {
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
try {
|
try {
|
||||||
handleProxyMessage(new JSONObject(message.string()));
|
handleProxyMessage(new JSONObject(text));
|
||||||
} catch (JSONException e) {
|
} catch (Exception e) {
|
||||||
throw new IOException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
|
||||||
message.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPong(Buffer payload) {
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClose(int code, String reason) {
|
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
closeAllConnections();
|
closeAllConnections();
|
||||||
if (!mClosed) {
|
if (!mClosed) {
|
||||||
@ -240,8 +230,7 @@ public class InspectorPackagerConnection {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(mUrl).build();
|
Request request = new Request.Builder().url(mUrl).build();
|
||||||
WebSocketCall call = WebSocketCall.create(httpClient, request);
|
httpClient.newWebSocket(request, this);
|
||||||
call.enqueue(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reconnect() {
|
private void reconnect() {
|
||||||
@ -270,7 +259,7 @@ public class InspectorPackagerConnection {
|
|||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
try {
|
try {
|
||||||
mWebSocket.close(1000, "End of session");
|
mWebSocket.close(1000, "End of session");
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
// swallow, no need to handle it here
|
// swallow, no need to handle it here
|
||||||
}
|
}
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
@ -285,8 +274,8 @@ public class InspectorPackagerConnection {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
sockets[0].sendMessage(RequestBody.create(WebSocket.TEXT, object.toString()));
|
sockets[0].send(object.toString());
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
FLog.w(TAG, "Couldn't send event to packager", e);
|
FLog.w(TAG, "Couldn't send event to packager", e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -304,7 +293,7 @@ public class InspectorPackagerConnection {
|
|||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
try {
|
try {
|
||||||
mWebSocket.close(1000, "End of session");
|
mWebSocket.close(1000, "End of session");
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
// swallow, no need to handle it here
|
// swallow, no need to handle it here
|
||||||
}
|
}
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
|
@ -18,6 +18,7 @@ import com.facebook.infer.annotation.Assertions;
|
|||||||
import com.facebook.react.common.JavascriptException;
|
import com.facebook.react.common.JavascriptException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -28,18 +29,14 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.WebSocket;
|
||||||
import okhttp3.ws.WebSocket;
|
import okhttp3.WebSocketListener;
|
||||||
import okhttp3.ws.WebSocketCall;
|
|
||||||
import okhttp3.ws.WebSocketListener;
|
|
||||||
import okio.Buffer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper around WebSocketClient that recognizes RN debugging message format.
|
* A wrapper around WebSocketClient that recognizes RN debugging message format.
|
||||||
*/
|
*/
|
||||||
public class JSDebuggerWebSocketClient implements WebSocketListener {
|
public class JSDebuggerWebSocketClient extends WebSocketListener {
|
||||||
|
|
||||||
private static final String TAG = "JSDebuggerWebSocketClient";
|
private static final String TAG = "JSDebuggerWebSocketClient";
|
||||||
|
|
||||||
@ -67,8 +64,7 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(url).build();
|
Request request = new Request.Builder().url(url).build();
|
||||||
WebSocketCall call = WebSocketCall.create(mHttpClient, request);
|
mHttpClient.newWebSocket(request, this);
|
||||||
call.enqueue(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void prepareJSRuntime(JSDebuggerCallback callback) {
|
public void prepareJSRuntime(JSDebuggerCallback callback) {
|
||||||
@ -142,7 +138,7 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
try {
|
try {
|
||||||
mWebSocket.close(1000, "End of session");
|
mWebSocket.close(1000, "End of session");
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
// swallow, no need to handle it here
|
// swallow, no need to handle it here
|
||||||
}
|
}
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
@ -157,8 +153,8 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message));
|
mWebSocket.send(message);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
triggerRequestFailure(requestID, e);
|
triggerRequestFailure(requestID, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -180,16 +176,11 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(ResponseBody response) throws IOException {
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
if (response.contentType() != WebSocket.TEXT) {
|
|
||||||
FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer replyID = null;
|
Integer replyID = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JsonReader reader = new JsonReader(response.charStream());
|
JsonReader reader = new JsonReader(new StringReader(text));
|
||||||
String result = null;
|
String result = null;
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
@ -218,14 +209,12 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
} else {
|
} else {
|
||||||
abort("Parsing response message from websocket failed", e);
|
abort("Parsing response message from websocket failed", e);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
response.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(IOException e, Response response) {
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
abort("Websocket exception", e);
|
abort("Websocket exception", t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -236,15 +225,10 @@ public class JSDebuggerWebSocketClient implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int code, String reason) {
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPong(Buffer payload) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
private void abort(String message, Throwable cause) {
|
private void abort(String message, Throwable cause) {
|
||||||
FLog.e(TAG, "Error occurred, shutting down websocket connection: " + message, cause);
|
FLog.e(TAG, "Error occurred, shutting down websocket connection: " + message, cause);
|
||||||
closeQuietly();
|
closeQuietly();
|
||||||
|
@ -11,7 +11,6 @@ android_library(
|
|||||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3"),
|
react_native_dep("third-party/java/okhttp:okhttp3"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
|
|
||||||
react_native_dep("third-party/java/okio:okio"),
|
react_native_dep("third-party/java/okio:okio"),
|
||||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||||
react_native_target("java/com/facebook/react/common:common"),
|
react_native_target("java/com/facebook/react/common:common"),
|
||||||
|
@ -9,11 +9,15 @@
|
|||||||
|
|
||||||
package com.facebook.react.modules.websocket;
|
package com.facebook.react.modules.websocket;
|
||||||
|
|
||||||
import android.util.Base64;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.IllegalStateException;
|
import java.net.URI;
|
||||||
import javax.annotation.Nullable;
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.facebook.common.logging.FLog;
|
import com.facebook.common.logging.FLog;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
@ -33,21 +37,9 @@ import com.facebook.react.modules.network.ForwardingCookieHandler;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.WebSocket;
|
||||||
import okhttp3.ws.WebSocket;
|
import okhttp3.WebSocketListener;
|
||||||
import okhttp3.ws.WebSocketCall;
|
|
||||||
import okhttp3.ws.WebSocketListener;
|
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import okio.Buffer;
|
|
||||||
import okio.ByteString;
|
import okio.ByteString;
|
||||||
|
|
||||||
@ReactModule(name = "WebSocketModule", hasConstants = false)
|
@ReactModule(name = "WebSocketModule", hasConstants = false)
|
||||||
@ -132,7 +124,7 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {
|
client.newWebSocket(builder.build(), new WebSocketListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket webSocket, Response response) {
|
public void onOpen(WebSocket webSocket, Response response) {
|
||||||
@ -143,7 +135,7 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int code, String reason) {
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
WritableMap params = Arguments.createMap();
|
WritableMap params = Arguments.createMap();
|
||||||
params.putInt("id", id);
|
params.putInt("id", id);
|
||||||
params.putInt("code", code);
|
params.putInt("code", code);
|
||||||
@ -152,40 +144,26 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(IOException e, Response response) {
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
notifyWebSocketFailed(id, t.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPong(Buffer buffer) {
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMessage(ResponseBody response) throws IOException {
|
|
||||||
String message;
|
|
||||||
try {
|
|
||||||
if (response.contentType() == WebSocket.BINARY) {
|
|
||||||
message = Base64.encodeToString(response.source().readByteArray(), Base64.NO_WRAP);
|
|
||||||
} else {
|
|
||||||
message = response.source().readUtf8();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
response.source().close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
FLog.e(
|
|
||||||
ReactConstants.TAG,
|
|
||||||
"Could not close BufferedSource for WebSocket id " + id,
|
|
||||||
e);
|
|
||||||
}
|
|
||||||
|
|
||||||
WritableMap params = Arguments.createMap();
|
WritableMap params = Arguments.createMap();
|
||||||
params.putInt("id", id);
|
params.putInt("id", id);
|
||||||
params.putString("data", message);
|
params.putString("data", text);
|
||||||
params.putString("type", response.contentType() == WebSocket.BINARY ? "binary" : "text");
|
params.putString("type", "text");
|
||||||
|
sendEvent("websocketMessage", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||||
|
String text = bytes.utf8();
|
||||||
|
WritableMap params = Arguments.createMap();
|
||||||
|
params.putInt("id", id);
|
||||||
|
params.putString("data", text);
|
||||||
|
params.putString("type", "binary");
|
||||||
sendEvent("websocketMessage", params);
|
sendEvent("websocketMessage", params);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -221,8 +199,8 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
client.sendMessage(RequestBody.create(WebSocket.TEXT, message));
|
client.send(message);
|
||||||
} catch (IOException | IllegalStateException e) {
|
} catch (Exception e) {
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
notifyWebSocketFailed(id, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,9 +213,8 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
client.sendMessage(
|
client.send(ByteString.decodeBase64(base64String));
|
||||||
RequestBody.create(WebSocket.BINARY, ByteString.decodeBase64(base64String)));
|
} catch (Exception e) {
|
||||||
} catch (IOException | IllegalStateException e) {
|
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
notifyWebSocketFailed(id, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -250,9 +227,8 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Buffer buffer = new Buffer();
|
client.send(ByteString.EMPTY);
|
||||||
client.sendPing(buffer);
|
} catch (Exception e) {
|
||||||
} catch (IOException | IllegalStateException e) {
|
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
notifyWebSocketFailed(id, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ android_library(
|
|||||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3"),
|
react_native_dep("third-party/java/okhttp:okhttp3"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
|
|
||||||
react_native_dep("third-party/java/okio:okio"),
|
react_native_dep("third-party/java/okio:okio"),
|
||||||
react_native_target("java/com/facebook/react/modules/systeminfo:systeminfo-moduleless"),
|
react_native_target("java/com/facebook/react/modules/systeminfo:systeminfo-moduleless"),
|
||||||
] + ([react_native_target("jni/packagerconnection:jni")] if not IS_OSS_BUILD else []),
|
] + ([react_native_target("jni/packagerconnection:jni")] if not IS_OSS_BUILD else []),
|
||||||
|
@ -15,9 +15,7 @@ import android.net.Uri;
|
|||||||
import com.facebook.common.logging.FLog;
|
import com.facebook.common.logging.FLog;
|
||||||
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
|
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
|
||||||
|
|
||||||
import okhttp3.RequestBody;
|
import okio.ByteString;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import okhttp3.ws.WebSocket;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -42,7 +40,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
|
|||||||
message.put("version", PROTOCOL_VERSION);
|
message.put("version", PROTOCOL_VERSION);
|
||||||
message.put("id", mId);
|
message.put("id", mId);
|
||||||
message.put("result", result);
|
message.put("result", result);
|
||||||
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
|
mWebSocket.sendMessage(message.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
FLog.e(TAG, "Responding failed", e);
|
FLog.e(TAG, "Responding failed", e);
|
||||||
}
|
}
|
||||||
@ -54,7 +52,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
|
|||||||
message.put("version", PROTOCOL_VERSION);
|
message.put("version", PROTOCOL_VERSION);
|
||||||
message.put("id", mId);
|
message.put("id", mId);
|
||||||
message.put("error", error);
|
message.put("error", error);
|
||||||
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
|
mWebSocket.sendMessage(message.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
FLog.e(TAG, "Responding with error failed", e);
|
FLog.e(TAG, "Responding with error failed", e);
|
||||||
}
|
}
|
||||||
@ -89,16 +87,9 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(ResponseBody response) {
|
public void onMessage(String text) {
|
||||||
if (response.contentType() != WebSocket.TEXT) {
|
|
||||||
FLog.w(
|
|
||||||
TAG,
|
|
||||||
"Websocket received message with payload of unexpected type " + response.contentType());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JSONObject message = new JSONObject(response.string());
|
JSONObject message = new JSONObject(text);
|
||||||
|
|
||||||
int version = message.optInt("version");
|
int version = message.optInt("version");
|
||||||
String method = message.optString("method");
|
String method = message.optString("method");
|
||||||
@ -130,11 +121,14 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
|
|||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
FLog.e(TAG, "Handling the message failed", e);
|
FLog.e(TAG, "Handling the message failed", e);
|
||||||
} finally {
|
|
||||||
response.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(ByteString bytes) {
|
||||||
|
FLog.w(TAG, "Websocket received message with payload of unexpected type binary");
|
||||||
|
}
|
||||||
|
|
||||||
private void abortOnMessage(Object id, String reason) {
|
private void abortOnMessage(Object id, String reason) {
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
(new ResponderImpl(id)).error(reason);
|
(new ResponderImpl(id)).error(reason);
|
||||||
|
@ -21,24 +21,22 @@ import com.facebook.common.logging.FLog;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.WebSocket;
|
||||||
import okhttp3.ws.WebSocket;
|
import okhttp3.WebSocketListener;
|
||||||
import okhttp3.ws.WebSocketCall;
|
import okio.ByteString;
|
||||||
import okhttp3.ws.WebSocketListener;
|
|
||||||
import okio.Buffer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper around WebSocketClient that reconnects automatically
|
* A wrapper around WebSocketClient that reconnects automatically
|
||||||
*/
|
*/
|
||||||
final public class ReconnectingWebSocket implements WebSocketListener {
|
final public class ReconnectingWebSocket extends WebSocketListener {
|
||||||
private static final String TAG = ReconnectingWebSocket.class.getSimpleName();
|
private static final String TAG = ReconnectingWebSocket.class.getSimpleName();
|
||||||
|
|
||||||
private static final int RECONNECT_DELAY_MS = 2000;
|
private static final int RECONNECT_DELAY_MS = 2000;
|
||||||
|
|
||||||
public interface MessageCallback {
|
public interface MessageCallback {
|
||||||
void onMessage(ResponseBody message);
|
void onMessage(String text);
|
||||||
|
void onMessage(ByteString bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ConnectionCallback {
|
public interface ConnectionCallback {
|
||||||
@ -77,8 +75,7 @@ final public class ReconnectingWebSocket implements WebSocketListener {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(mUrl).build();
|
Request request = new Request.Builder().url(mUrl).build();
|
||||||
WebSocketCall call = WebSocketCall.create(httpClient, request);
|
httpClient.newWebSocket(request, this);
|
||||||
call.enqueue(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void delayedReconnect() {
|
private synchronized void delayedReconnect() {
|
||||||
@ -122,7 +119,7 @@ final public class ReconnectingWebSocket implements WebSocketListener {
|
|||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
try {
|
try {
|
||||||
mWebSocket.close(1000, "End of session");
|
mWebSocket.close(1000, "End of session");
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
// swallow, no need to handle it here
|
// swallow, no need to handle it here
|
||||||
}
|
}
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
@ -145,9 +142,9 @@ final public class ReconnectingWebSocket implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onFailure(IOException e, Response response) {
|
public synchronized void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
abort("Websocket exception", e);
|
abort("Websocket exception", t);
|
||||||
}
|
}
|
||||||
if (!mClosed) {
|
if (!mClosed) {
|
||||||
if (mConnectionCallback != null) {
|
if (mConnectionCallback != null) {
|
||||||
@ -158,17 +155,21 @@ final public class ReconnectingWebSocket implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onMessage(ResponseBody message) {
|
public synchronized void onMessage(WebSocket webSocket, String text) {
|
||||||
if (mMessageCallback != null) {
|
if (mMessageCallback != null) {
|
||||||
mMessageCallback.onMessage(message);
|
mMessageCallback.onMessage(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onPong(Buffer payload) { }
|
public synchronized void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||||
|
if (mMessageCallback != null) {
|
||||||
|
mMessageCallback.onMessage(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onClose(int code, String reason) {
|
public synchronized void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
mWebSocket = null;
|
mWebSocket = null;
|
||||||
if (!mClosed) {
|
if (!mClosed) {
|
||||||
if (mConnectionCallback != null) {
|
if (mConnectionCallback != null) {
|
||||||
@ -178,9 +179,17 @@ final public class ReconnectingWebSocket implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void sendMessage(RequestBody message) throws IOException {
|
public synchronized void sendMessage(String message) throws IOException {
|
||||||
if (mWebSocket != null) {
|
if (mWebSocket != null) {
|
||||||
mWebSocket.sendMessage(message);
|
mWebSocket.send(message);
|
||||||
|
} else {
|
||||||
|
throw new ClosedChannelException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void sendMessage(ByteString message) throws IOException {
|
||||||
|
if (mWebSocket != null) {
|
||||||
|
mWebSocket.send(message);
|
||||||
} else {
|
} else {
|
||||||
throw new ClosedChannelException();
|
throw new ClosedChannelException();
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ prebuilt_jar(
|
|||||||
|
|
||||||
remote_file(
|
remote_file(
|
||||||
name = "okhttp3-binary-jar",
|
name = "okhttp3-binary-jar",
|
||||||
sha1 = "c7c4f9e35c2fd5900da24f9872e3971801f08ce0",
|
sha1 = "5a11f020cce2d11eb71ba916700600e18c4547e7",
|
||||||
url = "mvn:com.squareup.okhttp3:okhttp:jar:3.4.1",
|
url = "mvn:com.squareup.okhttp3:okhttp:jar:3.8.0",
|
||||||
)
|
)
|
||||||
|
|
||||||
prebuilt_jar(
|
prebuilt_jar(
|
||||||
@ -18,18 +18,6 @@ prebuilt_jar(
|
|||||||
|
|
||||||
remote_file(
|
remote_file(
|
||||||
name = "okhttp3-urlconnection-binary-jar",
|
name = "okhttp3-urlconnection-binary-jar",
|
||||||
sha1 = "63994437f62bc861bc20c605d12962f7246116d1",
|
sha1 = "265257b82f20bb0371a926cc8ceb5f7bb17c0df8",
|
||||||
url = "mvn:com.squareup.okhttp3:okhttp-urlconnection:jar:3.4.1",
|
url = "mvn:com.squareup.okhttp3:okhttp-urlconnection:jar:3.8.0",
|
||||||
)
|
|
||||||
|
|
||||||
prebuilt_jar(
|
|
||||||
name = "okhttp3-ws",
|
|
||||||
binary_jar = ":okhttp3-ws-binary-jar",
|
|
||||||
visibility = ["//ReactAndroid/..."],
|
|
||||||
)
|
|
||||||
|
|
||||||
remote_file(
|
|
||||||
name = "okhttp3-ws-binary-jar",
|
|
||||||
sha1 = "8ace66ef7002d98f633377c9e67daeeb196d8c3b",
|
|
||||||
url = "mvn:com.squareup.okhttp3:okhttp-ws:jar:3.4.1",
|
|
||||||
)
|
)
|
||||||
|
@ -6,6 +6,6 @@ prebuilt_jar(
|
|||||||
|
|
||||||
remote_file(
|
remote_file(
|
||||||
name = "okio-binary-jar",
|
name = "okio-binary-jar",
|
||||||
sha1 = "f824591a0016efbaeddb8300bee54832a1398cfa",
|
sha1 = "a9283170b7305c8d92d25aff02a6ab7e45d06cbe",
|
||||||
url = "mvn:com.squareup.okio:okio:jar:1.9.0",
|
url = "mvn:com.squareup.okio:okio:jar:1.13.0",
|
||||||
)
|
)
|
||||||
|
@ -15,7 +15,6 @@ rn_robolectric_test(
|
|||||||
react_native_dep("third-party/java/junit:junit"),
|
react_native_dep("third-party/java/junit:junit"),
|
||||||
react_native_dep("third-party/java/mockito:mockito"),
|
react_native_dep("third-party/java/mockito:mockito"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3"),
|
react_native_dep("third-party/java/okhttp:okhttp3"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
|
|
||||||
react_native_dep("third-party/java/okio:okio"),
|
react_native_dep("third-party/java/okio:okio"),
|
||||||
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
|
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
|
||||||
react_native_target("java/com/facebook/react:react"),
|
react_native_target("java/com/facebook/react:react"),
|
||||||
|
@ -22,8 +22,7 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import okhttp3.ResponseBody;
|
import okio.ByteString;
|
||||||
import okhttp3.ws.WebSocket;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@ -78,7 +77,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks() throws Exception {
|
public void test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.BINARY, "{\"replyID\":0, \"result\":\"OK\"}"));
|
client.onMessage(null, ByteString.encodeUtf8("{\"replyID\":0, \"result\":\"OK\"}"));
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
||||||
}
|
}
|
||||||
@ -87,7 +86,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks() throws Exception {
|
public void test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"result\":\"OK\"}"));
|
client.onMessage(null, "{\"result\":\"OK\"}");
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
||||||
}
|
}
|
||||||
@ -96,7 +95,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks() throws Exception {
|
public void test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":null, \"result\":\"OK\"}"));
|
client.onMessage(null, "{\"replyID\":null, \"result\":\"OK\"}");
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
||||||
}
|
}
|
||||||
@ -105,7 +104,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_WithResult_ShouldTriggerRequestSuccess() throws Exception {
|
public void test_onMessage_WithResult_ShouldTriggerRequestSuccess() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":0, \"result\":\"OK\"}"));
|
client.onMessage(null, "{\"replyID\":0, \"result\":\"OK\"}");
|
||||||
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", 0, "OK");
|
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", 0, "OK");
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
||||||
}
|
}
|
||||||
@ -114,7 +113,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_With_Null_Result_ShouldTriggerRequestSuccess() throws Exception {
|
public void test_onMessage_With_Null_Result_ShouldTriggerRequestSuccess() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":0, \"result\":null}"));
|
client.onMessage(null, "{\"replyID\":0, \"result\":null}");
|
||||||
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", 0, null);
|
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", 0, null);
|
||||||
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
|
||||||
}
|
}
|
||||||
@ -123,7 +122,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_WithError_ShouldCallAbort() throws Exception {
|
public void test_onMessage_WithError_ShouldCallAbort() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":0, \"error\":\"BOOM\"}"));
|
client.onMessage(null, "{\"replyID\":0, \"error\":\"BOOM\"}");
|
||||||
PowerMockito.verifyPrivate(client).invoke("abort", eq("BOOM"), isA(JavascriptException.class));
|
PowerMockito.verifyPrivate(client).invoke("abort", eq("BOOM"), isA(JavascriptException.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +130,7 @@ public class JSDebuggerWebSocketClientTest {
|
|||||||
public void test_onMessage_With_Null_Error_ShouldTriggerRequestSuccess() throws Exception {
|
public void test_onMessage_With_Null_Error_ShouldTriggerRequestSuccess() throws Exception {
|
||||||
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());
|
||||||
|
|
||||||
client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":0, \"error\":null}"));
|
client.onMessage(null, "{\"replyID\":0, \"error\":null}");
|
||||||
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", anyInt(), anyString());
|
PowerMockito.verifyPrivate(client).invoke("triggerRequestSuccess", anyInt(), anyString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ rn_robolectric_test(
|
|||||||
react_native_dep("third-party/java/junit:junit"),
|
react_native_dep("third-party/java/junit:junit"),
|
||||||
react_native_dep("third-party/java/mockito:mockito"),
|
react_native_dep("third-party/java/mockito:mockito"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3"),
|
react_native_dep("third-party/java/okhttp:okhttp3"),
|
||||||
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
|
|
||||||
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
|
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
|
||||||
react_native_target("java/com/facebook/react/packagerconnection:packagerconnection"),
|
react_native_target("java/com/facebook/react/packagerconnection:packagerconnection"),
|
||||||
],
|
],
|
||||||
|
@ -17,8 +17,7 @@ import java.io.IOException;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import okhttp3.ResponseBody;
|
import okio.ByteString;
|
||||||
import okhttp3.ws.WebSocket;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
@ -47,10 +46,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 2, \"method\": \"methodValue\", \"params\": \"paramsValue\"}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 2, \"method\": \"methodValue\", \"params\": \"paramsValue\"}"));
|
|
||||||
verify(handler).onNotification(eq("paramsValue"));
|
verify(handler).onNotification(eq("paramsValue"));
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -60,10 +56,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 2, \"id\": \"idValue\", \"method\": \"methodValue\", \"params\": \"paramsValue\"}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 2, \"id\": \"idValue\", \"method\": \"methodValue\", \"params\": \"paramsValue\"}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler).onRequest(eq("paramsValue"), any(Responder.class));
|
verify(handler).onRequest(eq("paramsValue"), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -73,10 +66,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 2, \"method\": \"methodValue\"}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 2, \"method\": \"methodValue\"}"));
|
|
||||||
verify(handler).onNotification(eq(null));
|
verify(handler).onNotification(eq(null));
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -86,10 +76,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage(ByteString.encodeUtf8("{\"version\": 2, \"method\": \"methodValue\"}"));
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.BINARY,
|
|
||||||
"{\"version\": 2, \"method\": \"methodValue\"}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -99,10 +86,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 2}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 2}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -112,10 +96,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 2, \"method\": null}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 2, \"method\": null}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -125,10 +106,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage(ByteString.EMPTY);
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.BINARY,
|
|
||||||
"{\"version\": 2, \"method\": \"methodValue2\"}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
@ -138,10 +116,7 @@ public class JSPackagerClientTest {
|
|||||||
RequestHandler handler = mock(RequestHandler.class);
|
RequestHandler handler = mock(RequestHandler.class);
|
||||||
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
final JSPackagerClient client = new JSPackagerClient("test_client", mSettings, createRH("methodValue", handler));
|
||||||
|
|
||||||
client.onMessage(
|
client.onMessage("{\"version\": 1, \"method\": \"methodValue\"}");
|
||||||
ResponseBody.create(
|
|
||||||
WebSocket.TEXT,
|
|
||||||
"{\"version\": 1, \"method\": \"methodValue\"}"));
|
|
||||||
verify(handler, never()).onNotification(any());
|
verify(handler, never()).onNotification(any());
|
||||||
verify(handler, never()).onRequest(any(), any(Responder.class));
|
verify(handler, never()).onRequest(any(), any(Responder.class));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user