コード例 #1
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (Mode == HttpRecorderMode.Playback)
            {
                // Will throw KeyNotFoundException if the request is not recorded
                lock (records)
                {
                    var key = Matcher.GetMatchingKey(request);

                    var queue = records[key];
                    if (!queue.Any())
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "Queue empty for request {0}",
                                                                RecorderUtilities.DecodeBase64AsUri(key)));
                    }

                    HttpResponseMessage result = queue.Dequeue().GetResponse();
                    result = AddTestModeHeaders(result);

                    result.RequestMessage = request;
                    return(Task.FromResult(result));
                }
            }
            else
            {
                lock (this)
                {
                    return(base.SendAsync(request, cancellationToken).ContinueWith <HttpResponseMessage>(response =>
                    {
                        HttpResponseMessage result = response.Result;
                        if (Mode == HttpRecorderMode.Record)
                        {
                            lock (records)
                            {
                                records.Enqueue(new RecordEntry(result));
                            }
                        }

                        return result;
                    }));
                }
            }
        }
コード例 #2
0
ファイル: HttpMockServer.cs プロジェクト: QITIE/ADLSTool
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (Mode == HttpRecorderMode.Playback)
            {
                // Will throw KeyNotFoundException if the request is not recorded
                var result = records[Matcher.GetMatchingKey(request)].Dequeue().GetResponse();
                result.RequestMessage = request;
                return(Task.FromResult(result));
            }
            else
            {
                return(base.SendAsync(request, cancellationToken).ContinueWith <HttpResponseMessage>(response =>
                {
                    HttpResponseMessage result = response.Result;
                    if (Mode == HttpRecorderMode.Record)
                    {
                        records.Enqueue(new RecordEntry(result));
                    }

                    return result;
                }));
            }
        }
コード例 #3
0
ファイル: HttpMockServer.cs プロジェクト: QITIE/ADLSTool
        public static void Initialize(string callerIdentity, string testIdentity, HttpRecorderMode mode)
        {
            CallerIdentity = callerIdentity;
            TestIdentity   = testIdentity;
            Mode           = mode;
            names          = new AssetNames();
            servers        = new List <HttpMockServer>();
            records        = new Records(Matcher);
            Variables      = new Dictionary <string, string>();

            if (Mode == HttpRecorderMode.Playback)
            {
                string recordDir = Path.Combine(RecordsDirectory, CallerIdentity);
                var    fileName  = Path.GetFullPath(Path.Combine(recordDir, testIdentity.Replace(".json", "") + ".json"));
                if (!HttpMockServer.FileSystemUtilsObject.DirectoryExists(recordDir) ||
                    !HttpMockServer.FileSystemUtilsObject.FileExists(fileName))
                {
                    throw new ArgumentException(
                              string.Format("Unable to find recorded mock file '{0}'.", fileName), "callerIdentity");
                }
                else
                {
                    RecordEntryPack pack = RecordEntryPack.Deserialize(fileName);
                    foreach (var entry in pack.Entries)
                    {
                        records.Enqueue(entry);
                    }
                    foreach (var func in pack.Names.Keys)
                    {
                        pack.Names[func].ForEach(n => names.Enqueue(func, n));
                    }
                    Variables = pack.Variables;
                    if (Variables == null)
                    {
                        Variables = new Dictionary <string, string>();
                    }
                }
            }

            initialized = true;
        }
コード例 #4
0
        public static void Initialize(string callerIdentity, string testIdentity, HttpRecorderMode mode)
        {
            CallerIdentity = callerIdentity;
            TestIdentity   = testIdentity;
            Mode           = mode;
            names          = new AssetNames();
            servers        = new List <HttpMockServer>();
            records        = new Records(Matcher);
            Variables      = new Dictionary <string, string>();
            string location = string.Empty;

#if FullNetFx
            var asmCollection = AppDomain.CurrentDomain.GetAssemblies();

            foreach (Assembly asm in asmCollection)
            {
                if (asm.GetType(CallerIdentity) != null)
                {
                    location = asm.Location;
                    break;
                }
            }
#elif !FullNetFx
//netcoreapp11 || netcoreapp20
            location = AppContext.BaseDirectory;
#endif
            RecordsDirectory = Path.Combine(location, RecordsDirectory);

            if (Mode == HttpRecorderMode.Playback)
            {
                string recordDir = Path.Combine(RecordsDirectory, CallerIdentity);
                var    fileName  = Path.GetFullPath(Path.Combine(recordDir, testIdentity.Replace(".json", "") + ".json"));
                if (!HttpMockServer.FileSystemUtilsObject.DirectoryExists(recordDir) ||
                    !HttpMockServer.FileSystemUtilsObject.FileExists(fileName))
                {
                    throw new ArgumentException(
                              string.Format("Unable to find recorded mock file '{0}'.", fileName), "callerIdentity");
                }
                else
                {
                    RecordEntryPack pack = RecordEntryPack.Deserialize(fileName);
                    lock (records)
                    {
                        foreach (var entry in pack.Entries)
                        {
                            records.Enqueue(entry);
                        }
                    }
                    foreach (var func in pack.Names.Keys)
                    {
                        pack.Names[func].ForEach(n => names.Enqueue(func, n));
                    }
                    Variables = pack.Variables;
                    if (Variables == null)
                    {
                        Variables = new Dictionary <string, string>();
                    }
                }
            }

            initialized = true;
        }