use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.
the class ApolloWSHandlerTest method testQueryWsCall.
private void testQueryWsCall(BiConsumer<WebSocket, JsonObject> sender) {
client.webSocket("/graphql", onSuccess(websocket -> {
websocket.exceptionHandler(this::fail);
websocket.endHandler(v -> testComplete());
AtomicReference<String> id = new AtomicReference<>();
AtomicInteger counter = new AtomicInteger();
websocket.textMessageHandler(text -> {
JsonObject obj = new JsonObject(text);
ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
if (type.equals(CONNECTION_KEEP_ALIVE)) {
return;
}
int current = counter.getAndIncrement();
if (current == 0) {
assertEquals(CONNECTION_ACK, type);
} else if (current == 1) {
assertTrue(id.compareAndSet(null, obj.getString("id")));
assertEquals(DATA, type);
int val = obj.getJsonObject("payload").getJsonObject("data").getJsonObject("staticCounter").getInteger("count");
assertEquals(STATIC_COUNT, val);
} else if (current == 2) {
assertEquals(id.get(), obj.getString("id"));
assertEquals(COMPLETE, type);
websocket.close();
} else {
fail();
}
});
JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
JsonObject message = new JsonObject().put("payload", new JsonObject().put("query", "query Query { staticCounter { count } }")).put("type", "start").put("id", "1");
websocket.write(messageInit.toBuffer());
sender.accept(websocket, message);
}));
await();
}
use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.
the class ApolloWSHandlerTest method testSubscriptionWsCallWithDoubleConnectionInit.
@Test
public void testSubscriptionWsCallWithDoubleConnectionInit() {
client.webSocket("/graphql", onSuccess(websocket -> {
websocket.exceptionHandler(this::fail);
websocket.endHandler(v -> testComplete());
AtomicInteger counter = new AtomicInteger();
websocket.textMessageHandler(text -> {
JsonObject obj = new JsonObject(text);
ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
if (type.equals(CONNECTION_KEEP_ALIVE)) {
return;
}
int current = counter.getAndIncrement();
if (current == 0) {
assertEquals(CONNECTION_ACK, type);
} else if (current == 1) {
assertEquals(ERROR, type);
websocket.close();
} else {
fail();
}
});
JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
websocket.write(messageInit.toBuffer());
websocket.write(messageInit.toBuffer());
}));
await();
}
use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.
the class ApolloWSHandlerTest method testSubscriptionWsCallWithFailedPromise.
@Test
public void testSubscriptionWsCallWithFailedPromise() {
String rejectMessage = "test";
client.webSocket("/graphql", onSuccess(websocket -> {
websocket.exceptionHandler(this::fail);
websocket.endHandler(v -> testComplete());
websocket.textMessageHandler(text -> {
JsonObject obj = new JsonObject(text);
ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
assertEquals(CONNECTION_ERROR, type);
assertEquals(rejectMessage, obj.getString("payload"));
websocket.close();
});
JsonObject messageInit = new JsonObject().put("payload", new JsonObject().put("rejectMessage", rejectMessage)).put("type", "connection_init").put("id", "1");
websocket.write(messageInit.toBuffer());
}));
await();
}
use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.
the class ApolloWSConnectionHandler method handleMessage.
private void handleMessage(JsonObject jsonObject) {
String opId = jsonObject.getString("id");
ApolloWSMessageType type = from(jsonObject.getString("type"));
if (type == null) {
sendMessage(opId, ERROR, "Unknown message type: " + jsonObject.getString("type"));
return;
}
ApolloWSMessageImpl message = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject);
Handler<ApolloWSMessage> mh = apolloWSHandler.getMessageHandler();
if (mh != null) {
mh.handle(message);
}
Handler<ApolloWSConnectionInitEvent> connectionInitHandler = apolloWSHandler.getConnectionInitHandler();
switch(type) {
case CONNECTION_INIT:
if (!connectionInitialized.compareAndSet(false, true)) {
sendMessage(opId, ERROR, "CONNECTION_INIT can only be sent once").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
break;
}
if (connectionInitHandler != null) {
connectionInitHandler.handle(new ApolloWSConnectionInitEvent() {
@Override
public ApolloWSMessage message() {
return message;
}
@Override
public boolean tryComplete(Object o) {
return connectionPromise.tryComplete(o);
}
@Override
public boolean tryFail(Throwable throwable) {
return connectionPromise.tryFail(throwable);
}
@Override
public Future<Object> future() {
return connectionPromise.future();
}
});
} else {
connectionPromise.complete();
}
connectionPromise.future().onComplete(ar -> {
if (ar.succeeded()) {
connect();
} else {
sendMessage(opId, CONNECTION_ERROR, ar.cause().getMessage()).onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
}
});
break;
case CONNECTION_TERMINATE:
serverWebSocket.close();
break;
case START:
if (!connectionInitialized.get()) {
sendMessage(opId, ERROR, "CONNECTION_INIT has to be sent before START").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
break;
}
connectionPromise.future().onComplete(ar -> {
if (ar.succeeded()) {
ApolloWSMessage messageWithParams = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject, ar.result());
start(messageWithParams);
} else {
sendMessage(opId, ERROR, ar.cause().getMessage());
stop(opId);
}
});
break;
case STOP:
stop(opId);
break;
default:
sendMessage(opId, ERROR, "Unsupported message type: " + type);
break;
}
}
use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.
the class ApolloWSHandlerTest method testSubscriptionWsCall.
@Test
public void testSubscriptionWsCall() {
client.webSocket("/graphql", onSuccess(websocket -> {
websocket.exceptionHandler(this::fail);
websocket.endHandler(v -> testComplete());
AtomicReference<String> id = new AtomicReference<>();
AtomicInteger counter = new AtomicInteger();
websocket.textMessageHandler(text -> {
JsonObject obj = new JsonObject(text);
ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
if (type.equals(CONNECTION_KEEP_ALIVE)) {
return;
}
int current = counter.getAndIncrement();
if (current == 0) {
assertEquals(CONNECTION_ACK, type);
} else if (current >= 1 && current <= MAX_COUNT) {
if (current == 1) {
assertTrue(id.compareAndSet(null, obj.getString("id")));
} else {
assertEquals(id.get(), obj.getString("id"));
}
assertEquals(DATA, ApolloWSMessageType.from(obj.getString("type")));
int val = obj.getJsonObject("payload").getJsonObject("data").getJsonObject("counter").getInteger("count");
assertEquals(current - 1, val);
} else if (current == MAX_COUNT + 1) {
assertEquals(id.get(), obj.getString("id"));
assertEquals(COMPLETE, ApolloWSMessageType.from(obj.getString("type")));
websocket.close();
} else {
fail();
}
});
JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
JsonObject message = new JsonObject().put("payload", new JsonObject().put("query", "subscription Subscription { counter { count } }")).put("type", "start").put("id", "1");
websocket.write(messageInit.toBuffer());
websocket.write(message.toBuffer());
}));
await();
}
Aggregations