Ejemplo n.º 1
0
        public static IHttpClientFactory ConstructChallengeRespondingMock()
        {
            // A mock which returns the input challenge.
            return(HttpFactoryMock.CreateMock((message, _) => {
                var challenge = int.Parse(Regex.Match(message.RequestUri.Query, "(?<=challenge=)\\d+").Value, CultureInfo.CurrentCulture);

                return Task.FromResult(
                    new HttpResponseMessage {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent($"{{ \"challenge\": {challenge} }}")
                }
                    );
            }).Object);
        }
Ejemplo n.º 2
0
        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");
            }
        }