Пример #1
0
        public static string Flush(string outputPath = null)
        {
            string fileName = string.Empty;

            if (Mode == HttpRecorderMode.Record && records.Count > 0)
            {
                RecordEntryPack pack = new RecordEntryPack();
                string          perfImpactFileName = string.Empty;
                string          fileDirectory      = outputPath ?? RecordsDirectory;
                fileDirectory = Path.Combine(fileDirectory, CallerIdentity);
                RecorderUtilities.EnsureDirectoryExists(fileDirectory);

                lock (records)
                {
                    foreach (RecordEntry recordEntry in records.GetAllEntities())
                    {
                        recordEntry.RequestHeaders.Remove("Authorization");
                        recordEntry.RequestUri = new Uri(recordEntry.RequestUri).PathAndQuery;
                        pack.Entries.Add(recordEntry);
                    }
                }

                fileName       = (TestIdentity ?? "record") + ".json";
                fileName       = Path.Combine(fileDirectory, fileName);
                pack.Variables = Variables;
                pack.Names     = names.Names;

                pack.Serialize(fileName);
            }

            servers.ForEach(s => s.Dispose());

            return(fileName);
        }
Пример #2
0
        public RecordEntryPack Deserialize(string path, bool extractMetaData)
        {
            RecordEntryPack rePack = Deserialize(path);

            if (rePack != null && extractMetaData)
            {
                RRInfoRecordEntry.AddRange(rePack.Entries.Select <RecordEntry, RequestResponseInfo>((re) => new RequestResponseInfo(re)));
            }

            return(rePack);
        }
Пример #3
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>();

            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;
        }