test: Add a method to build a sync response as a JsonValue

master
Damir Jelić 2021-04-21 16:10:37 +02:00
parent 2a5ede9e1a
commit a1c0acbd0c
1 changed files with 31 additions and 6 deletions

View File

@ -214,9 +214,17 @@ impl EventBuilder {
self self
} }
/// Builds a `SyncResponse` containing the events we queued so far. The next response returned /// Builds a sync response as a JSON Value containing the events we queued
/// by `build_sync_response` will then be empty if no further events were queued. /// so far.
pub fn build_sync_response(&mut self) -> SyncResponse { ///
/// The next response returned by `build_sync_response` will then be empty
/// if no further events were queued.
///
/// This method is raw JSON equivalent to
/// [build_sync_response()](#method.build_sync_response), use
/// [build_sync_response()](#method.build_sync_response) if you need a typed
/// response.
pub fn build_json_sync_response(&mut self) -> JsonValue {
let main_room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); let main_room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost");
// First time building a sync response, so initialize the `prev_batch` to a default one. // First time building a sync response, so initialize the `prev_batch` to a default one.
@ -324,13 +332,30 @@ impl EventBuilder {
} }
} }
}; };
// Clear state so that the next sync response will be empty if nothing
// was added.
self.clear();
body
}
/// Builds a `SyncResponse` containing the events we queued so far.
///
/// The next response returned by `build_sync_response` will then be empty
/// if no further events were queued.
///
/// This method is high level and typed equivalent to
/// [build_json_sync_response()](#method.build_json_sync_response), use
/// [build_json_sync_response()](#method.build_json_sync_response) if you
/// need an untyped response.
pub fn build_sync_response(&mut self) -> SyncResponse {
let body = self.build_json_sync_response();
let response = Response::builder() let response = Response::builder()
.body(serde_json::to_vec(&body).unwrap()) .body(serde_json::to_vec(&body).unwrap())
.unwrap(); .unwrap();
// Clear state so that the next sync response will be empty if nothing was added.
self.clear();
SyncResponse::try_from_http_response(response).unwrap() SyncResponse::try_from_http_response(response).unwrap()
} }