Skip to main content

datex_native/com_interfaces/http/
mod.rs

1pub mod http_server;
2pub mod http_client;
3
4#[cfg(test)]
5mod tests {
6    use ntest_timeout::timeout;
7    use tokio::join;
8    use datex_core::global::dxb_block::DXBBlock;
9    use crate::com_interfaces::http::http_client::HTTPClientInterfaceSetupDataNative;
10    use crate::com_interfaces::http::http_server::HTTPServerInterfaceSetupDataNative;
11    use crate::com_interfaces::tests::{test_receive_block, test_send_block_async_callback, test_send_block_sync_once_callback};
12    use datex_core::network::com_interfaces::com_interface::factory::{ComInterfaceAsyncFactory};
13
14    use datex_core::network::com_interfaces::default_setup_data::http::http_client::HTTPClientInterfaceSetupData;
15    use datex_core::network::com_interfaces::default_setup_data::http::http_server::HTTPServerInterfaceSetupData;
16    use datex_core::utils::async_iterators::async_next_pin_box;
17
18    #[tokio::test]
19    #[timeout(100000)]
20    async fn test_connect_and_communicate() {
21        
22        let address= "0.0.0.0:45679".to_string();
23
24        let mut server_interface_configuration =
25            HTTPServerInterfaceSetupDataNative(HTTPServerInterfaceSetupData {
26                bind_address: address.clone(),
27                accept_addresses: None,
28            })
29                .create_interface()
30                .await
31                .unwrap();
32
33        let mut client_interface_configuration = HTTPClientInterfaceSetupDataNative(HTTPClientInterfaceSetupData {
34                url: format!("http://{}", address),
35            })
36                .create_interface()
37                .await
38                .unwrap();
39
40        // get client socket
41        let client_socket = async_next_pin_box(&mut client_interface_configuration.new_sockets_iterator).await.unwrap().unwrap();
42
43        // check client properties
44        assert_eq!(
45            client_interface_configuration.properties.name,
46            Some(format!("http://{}", address)),
47        );
48        assert_eq!(
49            client_interface_configuration.properties.channel,
50            "http"
51        );
52        assert_eq!(
53            client_interface_configuration.properties.interface_type,
54            "http-client"
55        );
56
57        // check server properties
58        assert_eq!(
59            server_interface_configuration.properties.name,
60            Some(address)
61        );
62        assert_eq!(
63            server_interface_configuration.properties.channel,
64            "http"
65        );
66        assert_eq!(
67            server_interface_configuration.properties.interface_type,
68            "http-server"
69        );
70
71        // send data from client to server and back
72        let message = DXBBlock::new_with_body(b"request");
73        let message_clone = message.clone();
74
75        let response_message = DXBBlock::new_with_body(b"response");
76        let response_message_clone = response_message.clone();
77
78        join!(
79            async move {
80                // get tmp socket on server side
81                let server_socket = async_next_pin_box(&mut server_interface_configuration.new_sockets_iterator).await.unwrap().unwrap();
82                // receive data on server
83                test_receive_block(&mut server_socket.iterator.unwrap(), message_clone).await;
84
85                // send data from server to client (sent via HTTP response)
86                test_send_block_sync_once_callback(&server_socket.send_callback, response_message_clone.clone());
87            },
88            async move {
89                // sleep 100ms to wait for server to accept connections
90                tokio::time::sleep(core::time::Duration::from_millis(100)).await;
91                // send data from client to server
92                test_send_block_async_callback(&client_socket.send_callback, message).await;
93
94                // receive response data on client
95                test_receive_block(&mut client_socket.iterator.unwrap(), response_message).await;
96            }
97        );
98    }
99}