コード例 #1
0
    public InterceptionBenchmarks()
    {
        _options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

        var builder = new HttpRequestInterceptionBuilder();

        builder
        .Requests()
        .ForHttp()
        .ForHost("www.google.co.uk")
        .ForPath("search")
        .ForQuery("q=Just+Eat")
        .Responds()
        .WithMediaType("text/html")
        .WithContent(@"<!DOCTYPE html><html dir=""ltr"" lang=""en""><head><title>Just Eat</title></head></html>")
        .RegisterWith(_options);

        builder
        .Requests()
        .ForHttps()
        .ForHost("files.domain.com")
        .ForPath("setup.exe")
        .ForQuery(string.Empty)
        .Responds()
        .WithMediaType("application/octet-stream")
        .WithContent(() => new byte[] { 0, 1, 2, 3, 4 })
        .RegisterWith(_options);

        builder
        .Requests()
        .ForHttps()
        .ForHost("api.github.com")
        .ForPath("orgs/justeat")
        .ForQuery(string.Empty)
        .Responds()
        .WithMediaType("application/json")
        .WithJsonContent(new { id = 1516790, login = "******", url = "https://api.github.com/orgs/justeat" })
        .RegisterWith(_options);

        builder
        .Requests()
        .ForQuery("page=1")
        .Responds()
        .WithContentStream(() => File.OpenRead("organization.json"))
        .RegisterWith(_options);

        var refitSettings = new RefitSettings()
        {
            ContentSerializer = new SystemTextJsonContentSerializer(),
        };

#pragma warning disable CA2000
        _client = _options.CreateHttpClient();
        _serviceNewtonsoftJson = RestService.For <IGitHub>(_options.CreateHttpClient("https://api.github.com"));
        _serviceSystemTextJson = RestService.For <IGitHub>(_options.CreateHttpClient("https://api.github.com"), refitSettings);
#pragma warning restore CA2000
    }
コード例 #2
0
    public static async Task Intercept_Http_Get_For_Json_Object()
    {
        // begin-snippet: minimal-example

        // Arrange
        var options = new HttpClientInterceptorOptions();
        var builder = new HttpRequestInterceptionBuilder();

        builder
        .Requests()
        .ForGet()
        .ForHttps()
        .ForHost("public.je-apis.com")
        .ForPath("terms")
        .Responds()
        .WithJsonContent(new { Id = 1, Link = "https://www.just-eat.co.uk/privacy-policy" })
        .RegisterWith(options);

        using var client = options.CreateHttpClient();

        // Act
        // The value of json will be: {"Id":1, "Link":"https://www.just-eat.co.uk/privacy-policy"}
        string json = await client.GetStringAsync("https://public.je-apis.com/terms");

        // end-snippet

        // Assert
        var content = JObject.Parse(json);

        content.Value <int>("Id").ShouldBe(1);
        content.Value <string>("Link").ShouldBe("https://www.just-eat.co.uk/privacy-policy");
    }
コード例 #3
0
        /// <summary>
        /// Returns a new <see cref="HttpRequestInterceptionBuilder"/> that is configured
        /// for responding for TfL API requests for line information for transport modes.
        /// </summary>
        /// <returns>
        /// The <see cref="HttpRequestInterceptionBuilder"/> configured from the current instance.
        /// </returns>
        public HttpRequestInterceptionBuilder ForLines()
        {
            string encoded = Uri.EscapeDataString(string.Join(",", Modes()));

            var builder = new HttpRequestInterceptionBuilder();

            builder
            .Requests()
            .ForGet()
            .ForHttps()
            .ForHost("api.tfl.gov.uk")
            .ForPath($"Line/Mode/{encoded}")
            .IgnoringQuery();

            builder
            .Responds()
            .WithJsonContent(Lines());

            return(builder);
        }
コード例 #4
0
    public static async Task Intercept_Http_Get_For_Json_Object_Using_System_Text_Json()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();
        var builder = new HttpRequestInterceptionBuilder();

        builder.Requests().ForGet().ForHttps().ForHost("public.je-apis.com").ForPath("terms")
        .Responds().WithJsonContent(new { Id = 1, Link = "https://www.just-eat.co.uk/privacy-policy" })
        .RegisterWith(options);

        using var client = options.CreateHttpClient();

        // Act
        using var utf8Json = await client.GetStreamAsync("https://public.je-apis.com/terms");

        // Assert
        using var content = await JsonDocument.ParseAsync(utf8Json);

        content.RootElement.GetProperty("Id").GetInt32().ShouldBe(1);
        content.RootElement.GetProperty("Link").GetString().ShouldBe("https://www.just-eat.co.uk/privacy-policy");
    }