Beispiel #1
0
        public HttpResponseMessage AddHttpRequest(
            byte?addResponseContent,
            string url       = "http://www.com",
            int responseCode = 200)
        {
            var response = new HttpResponseMessage();

            if (addResponseContent.HasValue)
            {
                response.Content = new SingleByteContent(addResponseContent.Value);
            }

            response.StatusCode = (HttpStatusCode)responseCode;

            var  lck   = new object();
            bool first = true;

            Predicate <Tuple <HttpRequestMessage, CancellationToken> > assertUrl = AssertUrl;

            Dependencies
            .Setup(x => x.Send(Match.Create(assertUrl)))
            .Returns <Tuple <HttpRequestMessage, CancellationToken> >(Returns);

            return(response);

            bool AssertUrl(Tuple <HttpRequestMessage, CancellationToken> input)
            {
                return(input.Item1.RequestUri == new Uri(url));
            }

            FSharpAsync <HttpResponseMessage> Returns(Tuple <HttpRequestMessage, CancellationToken> req)
            {
                lock (lck)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }

                response.RequestMessage = req.Item1;
                var result = Task.FromResult(response);

                return(FSharpAsync.AwaitTask(result));
            }
        }
Beispiel #2
0
        private static Mock <ICachingHttpClientDependencies> BuildDependencies()
        {
            var dependencies = new Mock <ICachingHttpClientDependencies>();

            dependencies
            .Setup(x => x.Cache.Get(It.IsAny <string>()))
            .Returns(FSharpAsync.AwaitTask(Task.FromResult(FSharpOption <CachedValues> .None)));

            dependencies
            .Setup(x => x.Cache.Put(It.IsAny <Tuple <string, CachedValues> >()))
            .Returns(FSharpAsync.AwaitTask(Task.FromResult(AppTestUtils.unit)));

            dependencies
            .Setup(x => x.Cache.Delete(It.IsAny <string>()))
            .Returns(FSharpAsync.AwaitTask(Task.FromResult(AppTestUtils.unit)));

            dependencies
            .Setup(x => x.Cache.BuildUserKey(It.IsAny <CachedRequest.CachedRequest>()))
            .Returns <CachedRequest.CachedRequest>(GetUserKey);

            return(dependencies);
        }
 public FSharpAsync <string> FSharpAsyncFailureMethod(string parameter)
 {
     return(FSharpAsync.AwaitTask(
                Task.FromException <string>(new InvalidOperationException("Test exception"))));
 }
 public FSharpAsync <string> FSharpAsyncMethod(string parameter)
 {
     return(FSharpAsync.AwaitTask(Task.FromResult(parameter)));
 }
 public override FSharpAsync <Unit> Invoke(Unit unitVar)
 {
     _ = builder;
     return(FSharpAsync.AwaitTask(_projectorHandler.Project(_stream, span)));
 }
 public FSharpAsync <AudioPlayer.AudioPlayerInfo> MoveToPreviousTrack(AudioPlayer.AudioPlayerInfo value)
 {
     System.Windows.MessageBox.Show("MoveToPreviousTrack");
     return(FSharpAsync.AwaitTask(Task.FromResult(value)));
 }
 public FSharpAsync <AudioPlayer.AudioPlayerInfo> StartAudioPlayer(AudioPlayer.AudioPlayerInfo value)
 {
     System.Windows.MessageBox.Show("StartAudioPlayer");
     return(FSharpAsync.AwaitTask(Task.FromResult(value)));
 }
 FSharpAsync <IMarsContext> ICreateModelContext <IMarsContext> .CreateModelContextAsync() => FSharpAsync.AwaitTask(CreateDataContext());
Beispiel #9
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))));
            }
        }