Ejemplo n.º 1
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;
        }