Esempio n. 1
0
        public Requester(CommandLineOptions options)
        {

            var requestHandler = new WebRequestHandler()
                                     {
                                         UseCookies = false,
                                         UseDefaultCredentials = true
                                     };
            if (options.UseProxy)
            {
                requestHandler.UseProxy = true;
                requestHandler.Proxy = WebRequest.GetSystemWebProxy();
            }
            _client = new HttpClient(requestHandler);
            _options = options;
            _url = new TokenisedString(options.Url);
            if (!string.IsNullOrEmpty(options.Template))
            {
                _templateParser = new TemplateParser(File.ReadAllText(options.Template));
                _randomValueProvider = new RandomValueProvider(
                    new[] { _url }.Concat(_templateParser.Headers.Select(x => x.Value)).ToArray());
            }
            else
            {
                _randomValueProvider = new RandomValueProvider(_url);
            }
            _valueProvider = new NoValueProvider();

            if (!string.IsNullOrEmpty(_options.Plugin)) // plugin
            {
                var assembly = Assembly.LoadFile(_options.Plugin);
                var valueProviderType = assembly.GetExportedTypes().Where(t => typeof (IValueProvider)
                                                                      .IsAssignableFrom(t)).FirstOrDefault();
                if(valueProviderType==null)
                    throw new ArgumentException("No public type in plugin implements IValueProvider.");

                _valueProvider = (IValueProvider)Activator.CreateInstance(valueProviderType);
            }
            else if (!string.IsNullOrEmpty(options.ValuesFile)) // csv values file
            {
                _valueProvider = new SeparatedFileValueProvider(options.ValuesFile, 
                    options.IsTsv ? '\t' : ',');
            }

        }
        public AsyncRequesterBase(CommandLineOptions options)
        {
#if NET452
            var requestHandler = new WebRequestHandler()
            {
                UseCookies            = false,
                UseDefaultCredentials = true,
                ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true
            };
#else
            var requestHandler = new HttpClientHandler()
            {
                UseCookies            = false,
                UseDefaultCredentials = true
            };
#endif

            if (options.UseProxy)
            {
                requestHandler.UseProxy = true;
                requestHandler.Proxy    = WebRequest.GetSystemWebProxy();
            }
            _client  = new HttpClient(requestHandler);
            _options = options;
            _url     = new TokenisedString(options.Url);
            if (!string.IsNullOrEmpty(options.Template))
            {
                _templateParser      = new TemplateParser(File.ReadAllText(options.Template));
                _randomValueProvider = new RandomValueProvider(
                    new[] { _url }.Concat(_templateParser.Headers.Select(x => x.Value)).ToArray());
            }
            else
            {
                _randomValueProvider = new RandomValueProvider(_url);
            }
            _valueProvider = new NoValueProvider();

            if (!string.IsNullOrEmpty(_options.Plugin)) // plugin
            {
                var assembly          = Assembly.LoadFile(_options.Plugin);
                var valueProviderType = assembly.GetExportedTypes().FirstOrDefault(t => typeof(IValueProvider)
                                                                                   .IsAssignableFrom(t));
                if (valueProviderType != null)
                {
                    _valueProvider = (IValueProvider)Activator.CreateInstance(valueProviderType);
                    Console.WriteLine($"Loaded {valueProviderType.FullName} from the plugin as IValueProvider.");
                }

                var responseOverride = assembly.GetExportedTypes().FirstOrDefault(t => typeof(IResponseStatusOverride)
                                                                                  .IsAssignableFrom(t));

                if (responseOverride != null)
                {
                    _responseStatusOverride = (IResponseStatusOverride)Activator.CreateInstance(responseOverride);
                    Console.WriteLine($"Loaded {responseOverride.FullName} from the plugin as IResponseStatusOverride.");
                }
            }
            else if (!string.IsNullOrEmpty(options.ValuesFile)) // csv values file
            {
                _valueProvider = new SeparatedFileValueProvider(options.ValuesFile,
                                                                options.IsTsv ? '\t' : ',', options.Shuffle);
            }
        }