public void CheckServers() { // Arrange IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); Mock <IMemoryCache> cache = new Mock <IMemoryCache>(); var myContext = new Mock <HttpContext>(); DateTime now = DateTime.Now; Servers server = new Servers(); server.ServerId = "das423"; server.ServerURL = "/http/8080"; var json = JsonConvert.SerializeObject(server); var byteArray = Encoding.ASCII.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); stream.Flush(); stream.Position = 0; myContext.SetupGet(x => x.Request.Body).Returns(stream); var controllerContext = new ControllerContext() { HttpContext = myContext.Object, }; ServersController controller = new ServersController(memoryCache) { ControllerContext = controllerContext, }; // Act var answer = controller.AddServer(server); // Assert Assert.AreEqual(answer.Value, server); // Arrange controller = new ServersController(memoryCache); // Act ActionResult <List <Servers> > list = controller.GetServers(); // Assert Assert.IsTrue(list.Value.Count == 1); // Act answer = controller.DeleteServerById(server.ServerId); // Assert Assert.AreEqual(answer.Value, server); }
public async void GetServers_WithOutdated_OnlyReturnsCurrent() { using (var context = MockDataContext.GetContext()) { context.Servers.Add(new GameServer { Name = "A", Address = "127.0.0.1", QueryPort = 100, GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now }); context.Servers.Add(new GameServer { Name = "B", Address = "127.0.0.2", QueryPort = 200, GamePort = 200, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now - TimeSpan.FromMinutes(2) }); // This one should definitely be out of date context.Servers.Add(new GameServer { Name = "C", Address = "127.0.0.3", QueryPort = 300, GamePort = 300, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now - TimeSpan.FromMinutes(6) }); context.SaveChanges(); } // Use a separate, fresh context for testing. using (var context = MockDataContext.GetContext()) { var controller = new ServersController(context, HttpFactoryMock.CreateMock().Object, logger); IEnumerable <GameServer> servers = await controller.GetServers().ToListAsync(); Assert.Equal(2, servers.Count()); Assert.DoesNotContain(servers, server => server.Name == "C"); } }
public async void PostNewServer_WithExisting_GivesError() { // Setup using (var context = MockDataContext.GetContext()) { context.Servers.Add(new GameServer { Name = "Server Not A", Address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334", QueryPort = 100, GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now }); context.SaveChanges(); } using (var context = MockDataContext.GetContext()) { var controller = new ServersController(context, ConstructChallengeRespondingMock(), logger); var endPoint = new IPEndPoint(IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 100); MockControllerConnection(controller, endPoint.Address); var result = await controller.PostNewServer(new GameServer { Name = "Server A", Address = endPoint.Address.ToString(), QueryPort = endPoint.Port, GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, }).ConfigureAwait(false); Assert.Equal(StatusCodes.Status409Conflict, (result as IStatusCodeActionResult).StatusCode); } using (var context = MockDataContext.GetContext()) { var controller = new ServersController(context, ConstructChallengeRespondingMock(), logger); var servers = await controller.GetServers().ToListAsync(); Assert.Single(servers); Assert.Equal("Server Not A", servers[0].Name); // Nothing about the original entry should have changed. } }
public void Test_GetServer() { // Setup string serverUrl = "http://test1.com"; string serverId = "test_server1"; Server server = new Server(serverId, serverUrl); var stubRemoteServersConnector = new RemoteServersConnector(); stubRemoteServersConnector.AddServer(server); var serversController = new ServersController(stubRemoteServersConnector); // Act IActionResult getAction = serversController.GetServers(); // Assert OkObjectResult okResultGet = Assert.IsType <OkObjectResult>(getAction); string serversStr = Assert.IsType <string>(okResultGet.Value); var servers = JsonConvert.DeserializeObject(serversStr); Assert.Single((System.Collections.ICollection)servers); }