Exemple #1
0
        public HttpResponseMessage AddToCache(
            DateTime cahcedUntil,
            string url              = "http://www.com",
            string user             = null,
            byte?addRequestContent  = null,
            byte?addResponseContent = null,
            HttpMethod method       = null,
            Headers.CacheSettings.ExpirySettings expiry     = null,
            KeyValuePair <string, string[]>[] customHeaders = null)
        {
            expiry      = expiry ?? Headers.CacheSettings.ExpirySettings.NewHardUtc(DateTime.UtcNow.AddDays(10));
            cahcedUntil = new DateTime(cahcedUntil.Ticks, DateTimeKind.Utc);
            var response = new HttpResponseMessage();

            response.RequestMessage = new HttpRequestMessage();
            if (addRequestContent != null)
            {
                response.RequestMessage.Content = new SingleByteContent(addRequestContent.Value);
            }
            if (addResponseContent != null)
            {
                response.Content = new SingleByteContent(addResponseContent.Value);
            }
            if (customHeaders != null)
            {
                foreach (var h in customHeaders)
                {
                    response.Headers.Add(h.Key, h.Value);
                }
            }

            var m = (method == null || method == HttpMethod.Get) ? "G" : null;

            if (m == null)
            {
                throw new NotSupportedException(method?.ToString() ?? "null");
            }

            user = user?.Replace("$", "$$");
            var key = $"{m}$:{user}$:{new Uri(url)}";

            Dependencies
            .Setup(x => x.Cache.Get(It.Is <string>(k => k == key)))
            .Returns(Returns);

            return(response);

            FSharpAsync <FSharpOption <CachedValues> > Returns()
            {
                return(FSharpAsync.AwaitTask(ReturnsAsync()));
            }

            async Task <FSharpOption <CachedValues> > ReturnsAsync()
            {
                var resp = await FSharpAsync.StartAsTask(
                    CachedResponse.build(response),
                    FSharpOption <TaskCreationOptions> .None,
                    FSharpOption <CancellationToken> .None);

                return(FSharpOption <CachedValues> .Some(new CachedValues(
                                                             resp,
                                                             new Headers.CacheSettings.CacheSettings(expiry, true))));
            }
        }
Exemple #2
0
        public async Task SerializeAndDeserializeCompressed()
        {
            // arrange
            var cacheHeaders = CacheSettingsTests.BuildHeadersInflexible(
                cacheControlIsNull: false,
                sharedCache: false,
                noStore: false,
                immutable: true,
                maxAge: TimeSpan.FromDays(1),
                sMaxAge: TimeSpan.FromDays(1),
                pragma: new FSharpOption <string>("abc"),
                eTag: new EntityTagHeaderValue("\"def\""),
                exipiresUtc: DateTime.UtcNow.AddDays(1),
                lasModifiedUtc: new FSharpOption <DateTime>(DateTime.UtcNow.AddDays(-1)),
                vary: new FSharpOption <string>("very vary"));

            var httpResponse = new HttpResponseMessage
            {
                RequestMessage = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://www.com"),
                    Content    = new SingleByteContent(3),
                    Method     = HttpMethod.Post,
                    Version    = new Version(2, 0)
                },
                Content      = new SingleByteContent(7),
                ReasonPhrase = "OK",
                StatusCode   = System.Net.HttpStatusCode.OK,
                Version      = new Version(2, 0)
            };

            httpResponse.RequestMessage.Headers.Add("x-a-header", "h1");
            httpResponse.RequestMessage.Headers.IfUnmodifiedSince = DateTimeOffset.UtcNow;
            httpResponse.RequestMessage.Content.Headers.ContentLanguage.Add("en-us");

            httpResponse.Headers.Add("x-a-header", "h1");
            httpResponse.Headers.ETag = new EntityTagHeaderValue("\"asdas\"");
            httpResponse.Content.Headers.ContentLanguage.Add("en-us");

            var cachedResponse = await FSharpAsync.StartAsTask(CachedResponse.build(httpResponse), null, default(CancellationToken));

            var cacheSettings = build(cacheHeaders).Value;

            // act
            using (var str = await Serialization.CachedValues.Root.serialize(new CachedValues(cachedResponse, cacheSettings)).ToTask())
            {
                var stream = Streams.getStream(str);
                var result = new List <byte>(1000);
                var buffer = new byte[1000];
                var read   = 0;
                do
                {
                    read = await stream.ReadAsync(buffer, 0, 1000);

                    result.AddRange(buffer.Take(read));
                } while (read > 0);

                Assert.Fail("############# " + result.Count.ToString());
                using (var str2 = new MemoryStream(result.ToArray()))
                {
                    var backAgain = await Serialization.CachedValues.Root.deserialize(str2).ToTask();
                }
            }

            // assert
            //      Assert.True(FSharpOption<CacheSettings>.get_IsNone(result));
        }