MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / mid_session_encoding_switch_rejected

Function mid_session_encoding_switch_rejected

nodedb/tests/native_protocol.rs:505–554  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

503/// response (decode failure). The connection stays open — it is not terminated.
504#[tokio::test]
505async fn mid_session_encoding_switch_rejected() {
506 let server = NativeTestServer::start().await;
507
508 let (mut stream, _ack) = do_handshake(server.addr, &HelloFrame::current())
509 .await
510 .expect("handshake");
511
512 // First frame: JSON → establishes JSON encoding for the session.
513 let ping_json = NativeRequest {
514 op: OpCode::Ping,
515 seq: 1,
516 fields: RequestFields::Text(TextFields::default()),
517 };
518 let json_bytes = sonic_rs::to_vec(&ping_json).expect("json encode");
519 write_frame(&mut stream, &json_bytes).await;
520
521 // Consume the response to the first Ping.
522 let _first_resp = tokio::time::timeout(Duration::from_secs(5), read_frame(&mut stream))
523 .await
524 .expect("timeout")
525 .expect("first response");
526
527 // Second frame: MsgPack — the session is locked to JSON, so this must fail decode.
528 let ping_mp = NativeRequest {
529 op: OpCode::Ping,
530 seq: 2,
531 fields: RequestFields::Text(TextFields::default()),
532 };
533 let mp_bytes = zerompk::to_msgpack_vec(&ping_mp).expect("msgpack encode");
534 write_frame(&mut stream, &mp_bytes).await;
535
536 let switch_resp = tokio::time::timeout(Duration::from_secs(5), read_frame(&mut stream))
537 .await
538 .expect("timeout")
539 .expect("switch response");
540
541 server.shutdown().await;
542
543 // The response must be JSON-encoded (session is still in JSON mode).
544 assert_eq!(
545 switch_resp[0], b'{',
546 "response must still be JSON after mid-session switch attempt"
547 );
548 let resp: NativeResponse = sonic_rs::from_slice(&switch_resp).expect("json decode");
549 assert_eq!(
550 resp.status,
551 nodedb_types::protocol::opcodes::ResponseStatus::Error,
552 "mid-session encoding switch must produce an Error response"
553 );
554}

Callers

nothing calls this directly

Calls 6

do_handshakeFunction · 0.85
currentFunction · 0.85
write_frameFunction · 0.70
read_frameFunction · 0.70
expectMethod · 0.45
shutdownMethod · 0.45

Tested by

no test coverage detected