private HttpClient CreateHttpClient(
     HttpRecorderMode mode,
     [CallerMemberName] string testName = "",
     IInteractionRepository repository  = null,
     IInteractionAnonymizer anonymizer  = null)
 => new HttpClient(
     new HttpRecorderDelegatingHandler(testName, mode: mode, repository: repository, anonymizer: anonymizer)
 {
     InnerHandler = new HttpClientHandler(),
 })
 {
     BaseAddress = _fixture.ServerUri,
 };
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpRecorderDelegatingHandler" /> class.
 /// </summary>
 /// <param name="interactionName">
 /// The name of the interaction.
 /// If you use the default <see cref="IInteractionRepository"/>, this will be the path to the HAR file (relative or absolute) and
 /// if no file extension is provided, .har will be used.
 /// </param>
 /// <param name="mode">The <see cref="HttpRecorderMode" />. Defaults to <see cref="HttpRecorderMode.Auto" />.</param>
 /// <param name="matcher">
 /// The <see cref="IRequestMatcher"/> to use to match interactions with incoming <see cref="HttpRequestMessage"/>.
 /// Defaults to matching Once by <see cref="HttpMethod"/> and <see cref="HttpRequestMessage.RequestUri"/>.
 /// <see cref="RulesMatcher.ByHttpMethod"/> and <see cref="RulesMatcher.ByRequestUri"/>.
 /// </param>
 /// <param name="repository">
 /// The <see cref="IInteractionRepository"/> to use to read/write the interaction.
 /// Defaults to <see cref="HttpArchiveInteractionRepository"/>.
 /// </param>
 /// <param name="anonymizer">
 /// The <see cref="IInteractionAnonymizer"/> to use to anonymize the interaction.
 /// Defaults to <see cref="RulesInteractionAnonymizer.Default"/>.
 /// </param>
 public HttpRecorderDelegatingHandler(
     string interactionName,
     HttpRecorderMode mode             = HttpRecorderMode.Auto,
     IRequestMatcher matcher           = null,
     IInteractionRepository repository = null,
     IInteractionAnonymizer anonymizer = null)
 {
     InteractionName = interactionName;
     Mode            = mode;
     _matcher        = matcher ?? RulesMatcher.MatchOnce.ByHttpMethod().ByRequestUri();
     _repository     = repository ?? new HttpArchiveInteractionRepository();
     _anonymizer     = anonymizer ?? RulesInteractionAnonymizer.Default;
 }
Exemplo n.º 3
0
        public async Task ItShouldDoNothingByDefault()
        {
            var interaction = BuildInteraction(
                new HttpRequestMessage {
                RequestUri = new Uri("http://first")
            });

            IInteractionAnonymizer anonymizer = RulesInteractionAnonymizer.Default;

            var result = await anonymizer.Anonymize(interaction);

            result.Should().BeEquivalentTo(interaction);
        }
Exemplo n.º 4
0
        public async Task ItShouldAnonymizeRequestHeader()
        {
            var request = new HttpRequestMessage();

            request.Headers.TryAddWithoutValidation("X-RequestHeader", "Value");
            request.Content = new ByteArrayContent(Array.Empty <byte>());
            request.Content.Headers.TryAddWithoutValidation("X-RequestHeader", "Value2");
            var interaction = BuildInteraction(request);

            IInteractionAnonymizer anonymizer = RulesInteractionAnonymizer.Default
                                                .AnonymizeRequestHeader("X-RequestHeader");

            var result = await anonymizer.Anonymize(interaction);

            result.Messages[0].Response.RequestMessage.Headers.GetValues("X-RequestHeader").First().Should().Be(RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds <see cref="HttpRecorderDelegatingHandler"/> as a HttpMessageHandler in the client pipeline.
        /// </summary>
        /// <param name="httpClientBuilder">The <see cref="IHttpClientBuilder"/>.</param>
        /// <param name="interactionName">
        /// The name of the interaction.
        /// If you use the default <see cref="IInteractionRepository"/>, this will be the path to the HAR file (relative or absolute) and
        /// if no file extension is provided, .har will be used.
        /// </param>
        /// <param name="mode">The <see cref="HttpRecorderMode" />. Defaults to <see cref="HttpRecorderMode.Auto" />.</param>
        /// <param name="matcher">
        /// The <see cref="IRequestMatcher"/> to use to match interactions with incoming <see cref="HttpRequestMessage"/>.
        /// Defaults to matching Once by <see cref="HttpMethod"/> and <see cref="HttpRequestMessage.RequestUri"/>.
        /// <see cref="RulesMatcher.ByHttpMethod"/> and <see cref="RulesMatcher.ByRequestUri"/>.
        /// </param>
        /// <param name="repository">
        /// The <see cref="IInteractionRepository"/> to use to read/write the interaction.
        /// Defaults to <see cref="HttpArchiveInteractionRepository"/>.
        /// </param>
        /// <param name="anonymizer">
        /// The <see cref="IInteractionAnonymizer"/> to use to anonymize the interaction.
        /// Defaults to <see cref="RulesInteractionAnonymizer.Default"/>.
        /// </param>
        /// <returns>The updated <see cref="IHttpClientBuilder"/>.</returns>
        public static IHttpClientBuilder AddHttpRecorder(
            this IHttpClientBuilder httpClientBuilder,
            string interactionName,
            HttpRecorderMode mode             = HttpRecorderMode.Auto,
            IRequestMatcher matcher           = null,
            IInteractionRepository repository = null,
            IInteractionAnonymizer anonymizer = null)
        {
            var recorder = new HttpRecorderDelegatingHandler(
                interactionName,
                mode: mode,
                matcher: matcher,
                repository: repository,
                anonymizer: anonymizer);

            return(httpClientBuilder.AddHttpMessageHandler((sp) => recorder));
        }
Exemplo n.º 6
0
        public async Task ItShouldAnonymizeRequestQueryStringParameter()
        {
            var interaction = BuildInteraction(
                new HttpRequestMessage {
                RequestUri = new Uri("http://first/")
            },
                new HttpRequestMessage {
                RequestUri = new Uri("https://second/?key=foo&value=bar")
            });

            IInteractionAnonymizer anonymizer = RulesInteractionAnonymizer.Default
                                                .AnonymizeRequestQueryStringParameter("key");

            var result = await anonymizer.Anonymize(interaction);

            result.Messages[0].Response.RequestMessage.RequestUri.ToString().Should().Be("http://first/");
            result.Messages[1].Response.RequestMessage.RequestUri.ToString().Should().Be($"https://second/?key={RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue}&value=bar");
        }