Skip to content

Commit 692d879

Browse files
author
Hans Larsen
committed
fixup: install-code feature
1 parent ecf1597 commit 692d879

File tree

1 file changed

+99
-3
lines changed

1 file changed

+99
-3
lines changed

dfx/src/lib/api_client.rs

+99-3
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ pub fn install_code(
183183
arg: arg.unwrap_or_else(|| Blob(vec![])),
184184
},
185185
)
186-
.and_then(|response| match response.error_for_status() {
187-
Ok(_) => ok(()),
188-
Err(e) => err(DfxError::from(e)),
186+
.and_then(|response| {
187+
result(
188+
response
189+
.error_for_status()
190+
.map(|_| ())
191+
.map_err(DfxError::from),
192+
)
189193
})
190194
}
191195

@@ -399,4 +403,96 @@ mod tests {
399403
Err(e) => assert!(false, format!("{:#?}", e)),
400404
}
401405
}
406+
407+
#[test]
408+
fn install_code_request_serialization() {
409+
use serde_cbor::Value;
410+
use std::convert::TryInto;
411+
412+
let canister_id = 1;
413+
let module = Blob(vec![1]);
414+
let arg = Blob(vec![2]);
415+
416+
let request = SubmitRequest::InstallCode {
417+
canister_id,
418+
module,
419+
arg,
420+
};
421+
422+
let actual: Value = serde_cbor::from_slice(&serde_cbor::to_vec(&request).unwrap()).unwrap();
423+
424+
let expected = Value::Map(
425+
vec![
426+
(
427+
Value::Text("request_type".to_string()),
428+
Value::Text("install_code".to_string()),
429+
),
430+
(
431+
Value::Text("canister_id".to_string()),
432+
Value::Integer(canister_id.try_into().unwrap()),
433+
),
434+
(Value::Text("module".to_string()), Value::Bytes(vec![1])),
435+
(Value::Text("arg".to_string()), Value::Bytes(vec![2])),
436+
]
437+
.into_iter()
438+
.collect(),
439+
);
440+
441+
assert_eq!(actual, expected);
442+
}
443+
444+
#[test]
445+
fn install_code_response_replied() {
446+
let _ = env_logger::try_init();
447+
448+
let _m = mock("POST", "/api/v1/submit")
449+
.with_status(200)
450+
.with_header("content-type", "application/cbor")
451+
.create();
452+
453+
let client = Client::new(ClientConfig {
454+
url: mockito::server_url(),
455+
});
456+
457+
let future = install_code(client, 1, Blob(vec![1]), None);
458+
459+
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
460+
let result = runtime.block_on(future);
461+
462+
_m.assert();
463+
464+
match result {
465+
Ok(()) => {}
466+
Err(e) => assert!(false, format!("{:#?}", e)),
467+
}
468+
}
469+
470+
#[test]
471+
fn install_code_response_rejected() {
472+
let _ = env_logger::try_init();
473+
474+
let _m = mock("POST", "/api/v1/submit")
475+
.with_status(400)
476+
.with_header("content-type", "application/cbor")
477+
.create();
478+
479+
let client = Client::new(ClientConfig {
480+
url: mockito::server_url(),
481+
});
482+
483+
let future = install_code(client, 1, Blob(vec![1]), None);
484+
485+
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
486+
let result = runtime.block_on(future);
487+
488+
_m.assert();
489+
490+
match result {
491+
Ok(()) => assert!(false, "Install succeeded."),
492+
Err(e) => match e {
493+
DfxError::Reqwest(_err) => (),
494+
_ => assert!(false, format!("{:#?}", e)),
495+
},
496+
}
497+
}
402498
}

0 commit comments

Comments
 (0)