Esempio n. 1
0
        private ICacheContext CreateContextForTypedResult <T>(Uri uri, T result, HttpResponseMessage response)
        {
            var request = new MockHttpRequestMessage()
            {
                RequestUri = uri,
            };

            response.RequestMessage = request;
            var cacheSettings = new CacheSettings();
            var settings      = new TypedBuilderSettings(new Formatter(), cacheSettings, null, null);

            settings.WithDefiniteResultType(typeof(T));
            var httpBuilderContext = new TypedBuilderContext(settings);
            var handlerContext     = new TypedResultContext <T>(httpBuilderContext, request, response, result);

            return(new CacheContext(cacheSettings, handlerContext));
        }
Esempio n. 2
0
        private ICacheContext CreateContextForHttpResponse(Uri uri, HttpResponseMessage response, string accept = "application/json")
        {
            var request = new MockHttpRequestMessage()
            {
                RequestUri = uri,
            };

            request.Headers.Accept.ParseAdd(accept);
            response.RequestMessage = request;
            var cacheSettings = new CacheSettings();
            var settings      = new HttpBuilderSettings(cacheSettings, null, null);

            settings.UriBuilder.Uri = uri;
            var httpBuilderContext = new HttpBuilderContext(settings);
            var handlerContext     = new HttpSentContext(httpBuilderContext, request, response);

            return(new CacheContext(cacheSettings, handlerContext));
        }
Esempio n. 3
0
        private async Task FulfillRequest(HttpListenerContext context)
        {
            try
            {
                var listenerRequest = context.Request;
                var url             = listenerRequest.Url.ToString();

                Interlocked.Increment(ref _totalCount);
                _urlCount.AddOrUpdate(url, 1, (u, c) =>
                {
                    return(c + 1);
                });

                long urlCount;

                _urlCount.TryGetValue(url, out urlCount);

                var request = new MockHttpRequestMessage(listenerRequest)
                {
                    RequestCount           = _totalCount,
                    RequestCountForThisUrl = urlCount
                };

                //get response before we await anything to make sure it matches request.
                var response = _responses.GetResponse(request);

                await Log(request);

                var visitTasks = _requestInspectors.Select(visitor => visitor(request)).ToList();

                await Task.WhenAll(visitTasks);

                await CreateResponse(context, response);
            }
            catch (Exception ex)
            {
                _logger.WriteLine("Error: " + ex.Message);
            }
        }
        public void CreateEntry_SetsEtagToNotNull_IfWithConcurrencyProperty()
        {
            // Arrange
            Mock<IEdmStructuralProperty> mockConcurrencyProperty = new Mock<IEdmStructuralProperty>();
            mockConcurrencyProperty.SetupGet(s => s.ConcurrencyMode).Returns(EdmConcurrencyMode.Fixed);
            mockConcurrencyProperty.SetupGet(s => s.Name).Returns("City");
            SelectExpandNode selectExpandNode = new SelectExpandNode
            {
                SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, mockConcurrencyProperty.Object }
            };
            ODataProperty[] properties = new[] { new ODataProperty(), new ODataProperty() };
            Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider);
            serializer.CallBase = true;
            serializer
                .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext))
                .Returns(properties[0]);
            serializer
                .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext))
                .Returns(properties[1]);

            MockHttpRequestMessage request = new MockHttpRequestMessage();
            HttpConfiguration configuration = new HttpConfiguration();
            Mock<IETagHandler> mockETagHandler = new Mock<IETagHandler>();
            string tag = "\"'anycity'\"";
            EntityTagHeaderValue etagHeaderValue = new EntityTagHeaderValue(tag, isWeak: true);
            mockETagHandler.Setup(e => e.CreateETag(It.IsAny<IDictionary<string, object>>())).Returns(etagHeaderValue);
            configuration.SetETagHandler(mockETagHandler.Object);
            request.SetConfiguration(configuration);
            _entityInstanceContext.Request = request;

            // Act
            ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext);

            // Assert
            Assert.Equal(etagHeaderValue.ToString(), entry.ETag);
        }
        public void CreateEntry_SetsETagToNull_IfModelDontHaveConcurrencyProperty()
        {
            // Arrange
            IEdmEntitySet orderSet = _model.EntityContainer.FindEntitySet("Orders");
            Order order = new Order()
            {
                Name = "Foo",
                Shipment = "Bar",
                ID = 10,
            };

            _writeContext.NavigationSource = orderSet;
            _entityInstanceContext = new EntityInstanceContext(_writeContext, orderSet.EntityType().AsReference(), order);

            SelectExpandNode selectExpandNode = new SelectExpandNode
            {
                SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, new Mock<IEdmStructuralProperty>().Object }
            };
            ODataProperty[] properties = new[] { new ODataProperty(), new ODataProperty() };
            Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider);
            serializer.CallBase = true;

            serializer
                .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext))
                .Returns(properties[0]);
            serializer
                .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext))
                .Returns(properties[1]);

            MockHttpRequestMessage request = new MockHttpRequestMessage();
            request.SetConfiguration(new HttpConfiguration());
            _entityInstanceContext.Request = request;

            // Act
            ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext);

            // Assert
            Assert.Null(entry.ETag);
        }