| 1 |
|
| 2 |
|
| 3 |
use crate::common::*; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const SUBSCRIBE_PATH: &str = "/api/v1/sync/subscribe"; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
fn sse_changed_block() -> &'static str { |
| 17 |
"event: changed\n\n" |
| 18 |
} |
| 19 |
|
| 20 |
#[tokio::test] |
| 21 |
async fn subscribe_yields_changed_event() { |
| 22 |
let kit = MockKit::start().await; |
| 23 |
kit.get(SUBSCRIBE_PATH).text(sse_changed_block()).await; |
| 24 |
|
| 25 |
let client = kit.authed(); |
| 26 |
let mut stream = client.subscribe().await.expect("subscribe should succeed"); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
assert_eq!(stream.next_change().await, Some(())); |
| 31 |
} |
| 32 |
|
| 33 |
#[tokio::test] |
| 34 |
async fn subscribe_reconnects_transparently_after_stream_drop() { |
| 35 |
let kit = MockKit::start().await; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
kit.get(SUBSCRIBE_PATH).text(sse_changed_block()).await; |
| 40 |
|
| 41 |
let client = kit.authed(); |
| 42 |
let mut stream = client.subscribe().await.expect("subscribe should succeed"); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
assert_eq!(stream.next_change().await, Some(())); |
| 47 |
assert_eq!(stream.next_change().await, Some(())); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
let hits = kit.hits(SUBSCRIBE_PATH).await; |
| 52 |
assert!( |
| 53 |
hits >= 2, |
| 54 |
"expected a reconnect (>=2 subscribe requests), got {hits}" |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
#[tokio::test] |
| 59 |
async fn subscribe_stream_closes_on_auth_rejection() { |
| 60 |
let kit = MockKit::start().await; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
kit.get(SUBSCRIBE_PATH).once().text("").await; |
| 65 |
kit.get(SUBSCRIBE_PATH).code(401).text("unauthorized").await; |
| 66 |
|
| 67 |
let client = kit.authed(); |
| 68 |
let mut stream = client.subscribe().await.expect("initial subscribe is 200"); |
| 69 |
|
| 70 |
|
| 71 |
assert_eq!(stream.next_change().await, None); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|