Added a single test with a sub message on `Payload`.
This commit is contained in:
parent
aa35f99f5d
commit
e3953c7a4f
|
@ -6,3 +6,10 @@ message Payload {
|
|||
string route = 1; // a route can be fx /application/person
|
||||
bytes body = 2; // this can represent a person, but that is left up to the implementor to decide
|
||||
}
|
||||
|
||||
// TODO: move Person to tests
|
||||
// used for testing purposes
|
||||
message Person {
|
||||
string name = 1;
|
||||
int32 age = 2;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
extern crate commander_host;
|
||||
extern crate kernel;
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::{str, thread, time};
|
||||
|
||||
use commander_host::Host;
|
||||
use kernel::router::Action;
|
||||
use kernel::tcp::{ConnectOptions, StreamClient};
|
||||
use kernel::Payload;
|
||||
use kernel::Person;
|
||||
|
||||
extern crate prost;
|
||||
use prost::Message;
|
||||
|
@ -241,7 +243,7 @@ fn can_add_one_action_and_start_and_call_it_with_exactly_511_bytes() {
|
|||
|
||||
#[test]
|
||||
fn can_add_one_action_and_start_and_call_it_with_exactly_513_bytes() {
|
||||
let connect_options = ConnectOptions::new(String::from("127.0.0.1"), 6617);
|
||||
let connect_options = ConnectOptions::new(String::from("127.0.0.1"), 6667);
|
||||
let mut host = Host::new(connect_options.clone());
|
||||
let test_action = ServerTestAction;
|
||||
host.add_action("route/one", Box::new(test_action));
|
||||
|
@ -295,7 +297,61 @@ fn can_add_one_action_and_start_and_call_it_with_exactly_513_bytes() {
|
|||
println!("stop called");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_add_one_action_and_start_and_call_it_with_sub_message() {
|
||||
let connect_options = ConnectOptions::new(String::from("127.0.0.1"), 6668);
|
||||
let mut host = Host::new(connect_options.clone());
|
||||
let test_action = ServerTestActionWithSubMessage;
|
||||
host.add_action("route/one", Box::new(test_action));
|
||||
|
||||
match host.start() {
|
||||
Ok(_) => println!("host started"),
|
||||
Err(_e) => {
|
||||
panic!("didn't start host")
|
||||
}
|
||||
}
|
||||
|
||||
thread::sleep(time::Duration::from_millis(10)); // we need a bit of time for the server to start
|
||||
|
||||
let client = StreamClient::new(connect_options);
|
||||
let person = Person {
|
||||
name: String::from("testy"),
|
||||
age: 555,
|
||||
};
|
||||
|
||||
let payload = Payload {
|
||||
route: String::from("route/one"),
|
||||
body: person.encode_to_vec(),
|
||||
};
|
||||
|
||||
let encoded_bytes = payload.encode_to_vec();
|
||||
|
||||
let received_data = client.send(encoded_bytes);
|
||||
let received_data = match received_data {
|
||||
Ok(data) => data,
|
||||
Err(_) => {
|
||||
panic!("something went wrong unwrapping")
|
||||
}
|
||||
};
|
||||
|
||||
let received_person = Person::decode(Cursor::new(&received_data));
|
||||
let received_person = received_person.unwrap();
|
||||
assert_eq!("test", received_person.name);
|
||||
|
||||
assert_eq!(666, received_person.age);
|
||||
|
||||
println!("calling stop");
|
||||
|
||||
match host.stop() {
|
||||
Ok(_) => println!("host stopped"),
|
||||
Err(_e) => panic!("couldn't stop"),
|
||||
}
|
||||
|
||||
println!("stop called");
|
||||
}
|
||||
|
||||
struct ServerTestAction;
|
||||
struct ServerTestActionWithSubMessage;
|
||||
impl Action for ServerTestAction {
|
||||
fn work(&self, data: &Vec<u8>) -> Vec<u8> {
|
||||
let got = match str::from_utf8(data) {
|
||||
|
@ -309,3 +365,21 @@ impl Action for ServerTestAction {
|
|||
return_string.as_bytes().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for ServerTestActionWithSubMessage {
|
||||
fn work(&self, data: &Vec<u8>) -> Vec<u8> {
|
||||
let person = Person::decode(Cursor::new(&data));
|
||||
let person = person.unwrap();
|
||||
|
||||
if person.name == "testy" {
|
||||
let return_person = Person {
|
||||
name: String::from("test"),
|
||||
age: 666,
|
||||
};
|
||||
|
||||
return_person.encode_to_vec()
|
||||
} else {
|
||||
[0u8; 1].to_vec()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue