public void CommsServer_HttpRequestRaisesRequestReceived() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; var eventReceivedSignal = new System.Threading.ManualResetEvent(false); System.Net.Http.HttpRequestMessage receivedMessage = null; UdpEndPoint receivedFrom = null; server.RequestReceived += (sender, e) => { receivedMessage = e.Message; receivedFrom = e.ReceivedFrom; requestReceived = true; eventReceivedSignal.Set(); }; server.ResponseReceived += (sender, e) => { responseReceived = true; }; server.BeginListeningForBroadcasts(); var message = String.Format(@"M-SEARCH * HTTP/1.1 HOST: {0}:{1} MAN: ""ssdp:discover"" MX: {3} ST: {2} ", "", SsdpConstants.MulticastLocalAdminAddress, SsdpConstants.MulticastPort, "test search target", "1" ); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); eventReceivedSignal.WaitOne(10000); Assert.IsTrue(requestReceived); Assert.IsFalse(responseReceived); Assert.AreEqual("M-SEARCH", receivedMessage.Method.Method); Assert.AreEqual("*", receivedMessage.RequestUri.ToString()); Assert.AreEqual(SsdpConstants.MulticastLocalAdminAddress, receivedFrom.IPAddress); Assert.AreEqual(SsdpConstants.MulticastPort, receivedFrom.Port); }
public void CommsServer_HttpResponseInvalidVersionIgnored() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; server.RequestReceived += (sender, e) => { requestReceived = true; }; server.ResponseReceived += (sender, e) => { responseReceived = true; }; var message = @"HTTP/A.1 200 OK "; server.BeginListeningForBroadcasts(); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); Assert.IsFalse(requestReceived); Assert.IsFalse(responseReceived); }
public void CommsServer_HttpResponseRaisesResponseReceived() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; var eventReceivedSignal = new System.Threading.ManualResetEvent(false); System.Net.Http.HttpResponseMessage receivedMessage = null; UdpEndPoint receivedFrom = null; server.RequestReceived += (sender, e) => { requestReceived = true; }; server.ResponseReceived += (sender, e) => { receivedMessage = e.Message; receivedFrom = e.ReceivedFrom; responseReceived = true; eventReceivedSignal.Set(); }; server.BeginListeningForBroadcasts(); var message = String.Format(@"HTTP/1.1 200 OK EXT: DATE: {0} CACHE-CONTROL: public, max-age=1800 ST:test search target SERVER: TestOS/1.0 UPnP/1.0 RSSDP/1.0 USN:testusn LOCATION:http://somedevice:1700 ", DateTime.UtcNow.ToString("r") ); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); eventReceivedSignal.WaitOne(10000); Assert.IsFalse(requestReceived); Assert.IsTrue(responseReceived); Assert.IsTrue(receivedMessage.IsSuccessStatusCode); Assert.AreEqual(System.Net.HttpStatusCode.OK, receivedMessage.StatusCode); Assert.AreEqual(SsdpConstants.MulticastLocalAdminAddress, receivedFrom.IPAddress); Assert.AreEqual(SsdpConstants.MulticastPort, receivedFrom.Port); }
public void CommsServer_ListeningForBroadcastsCreatesMulticastSocket() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); Assert.IsNull(socketFactory.MulticastSocket); server.BeginListeningForBroadcasts(); Assert.IsNotNull(socketFactory.MulticastSocket); }
public void CommsServer_BeginListeningForBroadcastsThrowsIfDisposed() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); server.Dispose(); server.BeginListeningForBroadcasts(); }
public void CommsServer_StopListeningForBroadcastsDisposesMulticastSocket() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); server.BeginListeningForBroadcasts(); server.StopListeningForBroadcasts(); Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed); }
public void CommsServer_ReconnectsOnClosedSocket() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); server.BeginListeningForBroadcasts(); var socket1 = socketFactory.MulticastSocket; System.Threading.Thread.Sleep(500); socketFactory.ThrowSocketClosedException(); System.Threading.Thread.Sleep(2000); var requestReceived = false; using (var eventReceivedSignal = new System.Threading.ManualResetEvent(false)) { System.Net.Http.HttpRequestMessage receivedMessage = null; server.RequestReceived += (sender, e) => { receivedMessage = e.Message; requestReceived = true; eventReceivedSignal.Set(); }; var message = String.Format(@"M-SEARCH * HTTP/1.1 HOST: {0}:{1} MAN: ""ssdp:discover"" MX: {3} ST: {2} CONTENT-ENCODING:UTF8 some content here ", SsdpConstants.MulticastLocalAdminAddress, SsdpConstants.MulticastPort, "test search target", "1" ); socketFactory.MulticastSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort } ); eventReceivedSignal.WaitOne(120000); Assert.AreNotEqual(socket1, socketFactory.MulticastSocket); Assert.IsTrue(requestReceived); } }
public void CommsServer_HandlesRequestContentHeadersOk() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; using (var eventReceivedSignal = new System.Threading.ManualResetEvent(false)) { System.Net.Http.HttpRequestMessage receivedMessage = null; server.RequestReceived += (sender, e) => { receivedMessage = e.Message; requestReceived = true; eventReceivedSignal.Set(); }; server.ResponseReceived += (sender, e) => { responseReceived = true; }; var message = String.Format(@"M-SEARCH * HTTP/1.1 HOST: {0}:{1} MAN: ""ssdp:discover"" MX: {3} ST: {2} CONTENT-ENCODING:UTF8 some content here ", SsdpConstants.MulticastLocalAdminAddress, SsdpConstants.MulticastPort, "test search target", "1" ); server.BeginListeningForBroadcasts(); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); eventReceivedSignal.WaitOne(10000); Assert.IsTrue(requestReceived); Assert.IsFalse(responseReceived); Assert.AreEqual("UTF8", receivedMessage.Content.Headers.ContentEncoding.FirstOrDefault()); } }
public void CommsServer_HttpRequestWithNonAsterixUriIgnored() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; var eventReceivedSignal = new System.Threading.ManualResetEvent(false); server.RequestReceived += (sender, e) => { requestReceived = true; eventReceivedSignal.Set(); }; server.ResponseReceived += (sender, e) => { responseReceived = true; eventReceivedSignal.Set(); }; server.BeginListeningForBroadcasts(); var message = String.Format(@"M-SEARCH http://someuri HTTP/1.1 HOST: {0}:{1} MAN: ""ssdp:discover"" MX: {3} ST: {2} ", "", SsdpConstants.MulticastLocalAdminAddress, SsdpConstants.MulticastPort, "test search target", "1" ); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); eventReceivedSignal.WaitOne(1000); Assert.IsFalse(requestReceived); Assert.IsFalse(responseReceived); }
public void CommsServer_DisposeStopsListeningWithoutError() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); server.BeginListeningForBroadcasts(); server.Dispose(); Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed); var mockSocket = socketFactory.MulticastSocket as MockSocket; Assert.IsTrue(mockSocket.IsDisposed); }
public void CommsServer_InvalidHttpRequestIgnored() { var socketFactory = new MockSocketFactory(); var server = new SsdpCommunicationsServer(socketFactory); var requestReceived = false; var responseReceived = false; server.RequestReceived += (sender, e) => { requestReceived = true; }; server.ResponseReceived += (sender, e) => { responseReceived = true; }; var message = String.Format(@"M-SEARCH * HTTP/1.1 ", "", SsdpConstants.MulticastLocalAdminAddress, SsdpConstants.MulticastPort, "test search target", "1" ); server.BeginListeningForBroadcasts(); var mockSocket = socketFactory.MulticastSocket as MockSocket; mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort }); Assert.IsFalse(requestReceived); Assert.IsFalse(responseReceived); }