I have some client code that communicates with a server using streams that I want to test. I want to test that the client sends the correct commands to the server.
To do this I have created a Connect method that takes two streams: one the client reads from and one the client writes to. I can then supply a MemoryStream to both, and read the commands sent off the MemoryStream that the client writes to.
My problem is that I need to code a method that requires those two streams to be the same. I have also noticed, that the only time I need to use two different streams, is when I want to test the client. Therefore I want to refactor the Connect method to take only one stream, which will both be written to and read from.
I cant pass a MemoryStream to this method, since the write method writes to some internal byte array, from which the read method also reads from. If I give this to a client, it will be able to read the commands it just sent!
Therefore I would ask what stream setup can do this? I simply want a stream that reads from one source, but writes to another. I would need access to both sources, so that the "server", which is my test, can send the correct responses to commands, and read the commands the client sent.
In essence I need two streams: S1 and S2. One stream is given to the client, the other to the server.
S1's read method is "connected" to S2's write method => S1 reads what S2 writes.
S1's write method is "connected" to S2's read method => S2 reads what S1 writes.
This is exactly the normal case when a client communicates with a server. Here I just want to do it in memory. How can I code this?