2020-09-11 08:41:56 +00:00
|
|
|
use async_graphql::*;
|
2020-09-17 18:22:54 +00:00
|
|
|
use futures::channel::mpsc;
|
2020-09-11 08:41:56 +00:00
|
|
|
use futures::{SinkExt, Stream, StreamExt};
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_subscription_ws_transport() {
|
|
|
|
struct QueryRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl QueryRoot {}
|
|
|
|
|
|
|
|
struct SubscriptionRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl SubscriptionRoot {
|
|
|
|
async fn values(&self) -> impl Stream<Item = i32> {
|
|
|
|
futures::stream::iter(0..10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
|
2020-09-17 18:22:54 +00:00
|
|
|
let (mut tx, rx) = mpsc::unbounded();
|
|
|
|
let mut stream = http::WebSocket::new(schema, rx);
|
|
|
|
|
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "connection_init",
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-09-17 18:22:54 +00:00
|
|
|
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap()).unwrap(),
|
|
|
|
serde_json::json!({
|
|
|
|
"type": "connection_ack",
|
|
|
|
}),
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
|
2020-09-17 18:22:54 +00:00
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "start",
|
|
|
|
"id": "1",
|
|
|
|
"payload": {
|
|
|
|
"query": "subscription { values }"
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
assert_eq!(
|
2020-09-17 18:22:54 +00:00
|
|
|
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap()).unwrap(),
|
|
|
|
serde_json::json!({
|
|
|
|
"type": "data",
|
|
|
|
"id": "1",
|
|
|
|
"payload": { "data": { "values": i } },
|
|
|
|
}),
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-09-17 18:22:54 +00:00
|
|
|
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap()).unwrap(),
|
|
|
|
serde_json::json!({
|
|
|
|
"type": "complete",
|
|
|
|
"id": "1",
|
|
|
|
}),
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_subscription_ws_transport_with_token() {
|
|
|
|
struct Token(String);
|
|
|
|
|
|
|
|
struct QueryRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl QueryRoot {}
|
|
|
|
|
|
|
|
struct SubscriptionRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl SubscriptionRoot {
|
|
|
|
async fn values(&self, ctx: &Context<'_>) -> FieldResult<impl Stream<Item = i32>> {
|
|
|
|
if ctx.data_unchecked::<Token>().0 != "123456" {
|
|
|
|
return Err("forbidden".into());
|
|
|
|
}
|
|
|
|
Ok(futures::stream::iter(0..10))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
|
2020-09-17 18:22:54 +00:00
|
|
|
let (mut tx, rx) = mpsc::unbounded();
|
|
|
|
let mut stream = http::WebSocket::with_data(
|
|
|
|
schema,
|
|
|
|
rx,
|
|
|
|
Some(|value| {
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
struct Payload {
|
|
|
|
token: String,
|
|
|
|
}
|
2020-09-11 08:41:56 +00:00
|
|
|
|
2020-09-17 18:22:54 +00:00
|
|
|
let payload: Payload = serde_json::from_value(value).unwrap();
|
|
|
|
let mut data = Data::default();
|
|
|
|
data.insert(Token(payload.token));
|
|
|
|
Ok(data)
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "connection_init",
|
|
|
|
"payload": { "token": "123456" }
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "connection_ack",
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
|
2020-09-17 18:22:54 +00:00
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "start",
|
|
|
|
"id": "1",
|
|
|
|
"payload": {
|
|
|
|
"query": "subscription { values }"
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "data",
|
|
|
|
"id": "1",
|
|
|
|
"payload": { "data": { "values": i } },
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "complete",
|
|
|
|
"id": "1",
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_subscription_ws_transport_error() {
|
|
|
|
struct QueryRoot;
|
|
|
|
|
|
|
|
struct Event {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl Event {
|
|
|
|
async fn value(&self) -> FieldResult<i32> {
|
|
|
|
if self.value < 5 {
|
|
|
|
Ok(self.value)
|
|
|
|
} else {
|
|
|
|
Err("TestError".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl QueryRoot {}
|
|
|
|
|
|
|
|
struct SubscriptionRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl SubscriptionRoot {
|
|
|
|
async fn events(&self) -> impl Stream<Item = Event> {
|
|
|
|
futures::stream::iter((0..10).map(|n| Event { value: n }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
|
2020-09-17 18:22:54 +00:00
|
|
|
let (mut tx, rx) = mpsc::unbounded();
|
|
|
|
let mut stream = http::WebSocket::new(schema, rx);
|
|
|
|
|
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "connection_init"
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "connection_ack",
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
|
2020-09-17 18:22:54 +00:00
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "start",
|
|
|
|
"id": "1",
|
|
|
|
"payload": {
|
|
|
|
"query": "subscription { events { value } }"
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
for i in 0i32..5 {
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "data",
|
|
|
|
"id": "1",
|
|
|
|
"payload": { "data": { "events": { "value": i } } },
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
2020-09-17 18:22:54 +00:00
|
|
|
"type": "data",
|
|
|
|
"id": "1",
|
|
|
|
"payload": {
|
|
|
|
"errors": [{
|
|
|
|
"message": "TestError",
|
|
|
|
"locations": [{"line": 1, "column": 25}],
|
|
|
|
"path": ["events", "value"],
|
|
|
|
}],
|
|
|
|
},
|
2020-09-11 08:41:56 +00:00
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_query_over_websocket() {
|
|
|
|
struct QueryRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-11 08:41:56 +00:00
|
|
|
impl QueryRoot {
|
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
999
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
2020-09-17 18:22:54 +00:00
|
|
|
let (mut tx, rx) = mpsc::unbounded();
|
|
|
|
let mut stream = http::WebSocket::new(schema, rx);
|
|
|
|
|
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "connection_init",
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
|
|
|
"type": "connection_ack",
|
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
|
2020-09-17 18:22:54 +00:00
|
|
|
tx.send(
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
"type": "start",
|
|
|
|
"id": "1",
|
|
|
|
"payload": {
|
|
|
|
"query": "query { value }"
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
|
|
|
"type": "data",
|
|
|
|
"id": "1",
|
|
|
|
"payload": { "data": { "value": 999 } },
|
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(serde_json::json!({
|
|
|
|
"type": "complete",
|
|
|
|
"id": "1",
|
|
|
|
})),
|
2020-09-15 03:12:19 +00:00
|
|
|
serde_json::from_str(&stream.next().await.unwrap()).unwrap()
|
2020-09-11 08:41:56 +00:00
|
|
|
);
|
|
|
|
}
|