rustyphoenixrequest 1.6.0

This library provides methods to mock request API. This is the Rust equivalent of https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/network/PhoenixRequest
Documentation
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

use crate::phoenix_request::{
	PRequestParam,
	PResponseParam,
	PhoenixRequestStream,
	PhoenixRequestMockStream,
	PhoenixRequestServer,
	PhoenixRequestMockServer
};

pub struct PEmptyRequestStream;

impl PhoenixRequestStream for PEmptyRequestStream{
	///Recieved a request from a client
	/// # Parameters
	/// - `_request` : PRequestParam recievred from the client
	/// # Returns
	/// true if a request has been recieved false otherwise
	fn recv_request(&mut self, _request: &mut PRequestParam) -> bool {
		return false;
	}
	
	///Send response to the client
	/// # Parameters
	/// - `_response` : PResponseParam to be sent to the client
	fn send_response(&mut self, _response: &PResponseParam){
		
	}
}

impl PhoenixRequestMockStream for PEmptyRequestStream {
	///Set the record mode in the mock
	/// # Parameters
	/// - `_is_mock_record` - true if the mock is in record mode
	fn set_is_record(&mut self, _is_mock_record: bool){}
	
	///Set the prefix of the current mock
	/// # Parameters
	/// - `_prefix` - prefix of the current mock
	fn set_prefix(&mut self, _prefix: &String){}
}



pub struct PEmptyRequestServer;

impl PhoenixRequestServer for PEmptyRequestServer {
	type Stream = PEmptyRequestStream;
	///Initialise the Request server
	/// # Parameters
	/// - `_hostname` : address of the server
	/// - `_port` : port of the server
	/// # Returns
	/// initialised Request server
	fn new(_hostname: &String, _port: u16) -> Self{
		PEmptyRequestServer{}
	}
	///Accept the connection and get a corresponding stream
	/// # Returns
	/// corresponding stream
	fn accept(&self) -> Self::Stream{
		PEmptyRequestStream{}
	}
}

impl PhoenixRequestMockServer for PEmptyRequestServer {
	///Set the record mode in the mock
	/// # Parameters
	/// - `_is_mock_record` - true if the mock is in record mode
	fn set_is_record(&mut self, _is_mock_record: bool){}
	
	///Set the prefix of the current mock
	/// # Parameters
	/// - `_prefix` - prefix of the current mock
	fn set_prefix(&mut self, _prefix: &String){}
}


#[cfg(test)]
mod tests {
//{We need the previously defined functions of the crate :
	use super::*;
	use crate::pmimetype::PMimeType;
	
	use crate::phoenix_generic_request_server::{
		PRequestMode,
		PGenericRequestStream,
		PGenericRequestServer,
	};
	
	#[test]
	fn test_empty_generic_request(){
		let mut server: PGenericRequestServer<PEmptyRequestServer, PEmptyRequestServer> = PGenericRequestServer::new(&String::from("127.0.0.1"), 8080, PRequestMode::NoMock);
		assert_eq!(server.get_mode(), PRequestMode::NoMock);
		server.set_mode(PRequestMode::Mock);
		assert_eq!(server.get_mode(), PRequestMode::Mock);
		server.set_prefix(&String::from("some_mock_prefix_"));
		let mut stream: PGenericRequestStream<PEmptyRequestStream, PEmptyRequestStream> = server.accept();
		
		let mut request: PRequestParam = Default::default();
		assert_eq!(stream.recv_request(&mut request), false);
		assert_eq!(stream.get_mode(), PRequestMode::Mock);
		stream.set_mode(PRequestMode::NoMock);
		assert_eq!(stream.get_mode(), PRequestMode::NoMock);
		let response = PResponseParam::new(String::from("HTTP/1.1 200 OK"), PMimeType::TextPlain, &String::from("some html page"));
		stream.send_response(&response);
	}
}