Skip to content

Commit 2b536c6

Browse files
authored
Add test for deserializing pageable body (Azure#1897)
1 parent 8498a75 commit 2b536c6

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

sdk/typespec/typespec_client_core/src/http/response.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,30 @@ mod tests {
341341
value: String,
342342
}
343343

344+
/// An example JSON-serialized list response type.
345+
#[derive(Model, Deserialize)]
346+
#[typespec(crate = "crate")]
347+
struct GetSecretListResponse {
348+
value: Vec<GetSecretResponse>,
349+
#[serde(rename = "nextLink")]
350+
next_link: Option<String>,
351+
}
352+
344353
/// A sample service client function.
345354
fn get_secret() -> Response<GetSecretResponse> {
346355
Response::from_bytes(
347356
StatusCode::Ok,
348357
Headers::new(),
349-
"{\"name\":\"my_secret\",\"value\":\"my_value\"}",
358+
r#"{"name":"my_secret","value":"my_value"}"#,
359+
)
360+
}
361+
362+
/// A sample service client function to return a list of secrets.
363+
fn list_secrets() -> Response<GetSecretListResponse> {
364+
Response::from_bytes(
365+
StatusCode::Ok,
366+
Headers::new(),
367+
r#"{"value":[{"name":"my_secret","value":"my_value"}],"nextLink":"?page=2"}"#,
350368
)
351369
}
352370

@@ -374,6 +392,28 @@ mod tests {
374392
assert_eq!(secret.yon_name, "my_secret");
375393
assert_eq!(secret.yon_value, "my_value");
376394
}
395+
396+
#[tokio::test]
397+
async fn deserialize_pageable_from_body() {
398+
// We need to efficiently deserialize the body twice to get the "nextLink" but return it to the caller.
399+
let response = list_secrets();
400+
let (status, headers, body) = response.deconstruct();
401+
let bytes = body.collect().await.expect("collect response");
402+
let model: GetSecretListResponse =
403+
crate::json::from_json(bytes.clone()).expect("deserialize GetSecretListResponse");
404+
assert_eq!(status, StatusCode::Ok);
405+
assert_eq!(model.value.len(), 1);
406+
assert_eq!(model.next_link, Some("?page=2".to_string()));
407+
408+
let response: Response<GetSecretListResponse> =
409+
Response::from_bytes(status, headers, bytes);
410+
assert_eq!(response.status(), StatusCode::Ok);
411+
let model = response
412+
.deserialize_body()
413+
.await
414+
.expect("deserialize GetSecretListResponse again");
415+
assert_eq!(model.next_link, Some("?page=2".to_string()));
416+
}
377417
}
378418

379419
#[cfg(feature = "xml")]

0 commit comments

Comments
 (0)