Esempio n. 1
0
        public static string GetAssetName(string testName, string prefix)
        {
            if (Mode == HttpRecorderMode.Playback)
            {
                return(names[testName].Dequeue());
            }
            else
            {
                string generated = prefix + randomGenerator.Next(9999);

                if (Mode == HttpRecorderMode.Record)
                {
                    if (names.ContainsKey(testName))
                    {
                        while (names[testName].Any(n => n.Equals(generated)))
                        {
                            generated = prefix + randomGenerator.Next(9999);
                        }
                    }
                    names.Enqueue(testName, generated);
                }

                return(generated);
            }
        }
Esempio n. 2
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;
        }
Esempio n. 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>();
            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;
        }