Example #1
0
        public async Task Can_Get_Stop_Points_If_Response_Cannot_Be_Cached()
        {
            // Arrange
            var builder = CreateBuilder()
                          .Requests()
                          .ForPath("Line/victoria/StopPoints")
                          .Responds()
                          .WithJsonContent(new[] { new { id = "940GZZLUGPK", commonName = "Green Park Underground Station", lat = 51.506947, lon = -0.142787 } });

            _interceptor.Register(builder);

            using var httpClient   = _interceptor.CreateHttpClient();
            httpClient.BaseAddress = _options.BaseUri;

            ITflClient client = CreateClient(httpClient);
            var        target = new TflService(client, _cache, _options);

            // Act
            ICollection <StopPoint> actual1 = await target.GetStopPointsByLineAsync("victoria");

            ICollection <StopPoint> actual2 = await target.GetStopPointsByLineAsync("victoria");

            // Assert
            Assert.NotNull(actual1);
            Assert.Equal(1, actual1.Count);

            var item = actual1.First();

            Assert.Equal("940GZZLUGPK", item.Id);
            Assert.Equal("Green Park Underground Station", item.Name);
            Assert.Equal(51.506947, item.Latitude);
            Assert.Equal(-0.142787, item.Longitude);

            Assert.NotSame(actual1, actual2);
        }
        public async Task Can_Get_Stop_Points_If_Response_Can_Be_Cached()
        {
            // Arrange
            var builder = CreateBuilder()
                          .Requests()
                          .ForPath("Line/victoria/StopPoints")
                          .Responds()
                          .WithResponseHeader("Cache-Control", "max-age=3600")
                          .WithJsonContent(new[] { new { id = "940GZZLUOXC", commonName = "Oxford Circus Underground Station", lat = 51.515224, lon = -0.141903 } });

            _interceptor.Register(builder);

            ICollection <StopPoint> actual1;
            ICollection <StopPoint> actual2;

            using (var httpClient = _interceptor.CreateHttpClient())
            {
                httpClient.BaseAddress = _options.BaseUri;

                var client = Refit.RestService.For <ITflClient>(httpClient);
                var target = new TflService(client, _cache, _options);

                // Act
                actual1 = await target.GetStopPointsByLineAsync("victoria");

                actual2 = await target.GetStopPointsByLineAsync("victoria");
            }

            // Assert
            Assert.NotNull(actual1);
            Assert.Equal(1, actual1.Count);

            var item = actual1.First();

            Assert.Equal("940GZZLUOXC", item.Id);
            Assert.Equal("Oxford Circus Underground Station", item.Name);
            Assert.Equal(51.515224, item.Latitude);
            Assert.Equal(-0.141903, item.Longitude);

            Assert.Same(actual1, actual2);
        }