public static async Task Can_Intercept_Http_Requests_From_Bundle_File_If_No_Content()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

        // Act
        options.RegisterBundle(Path.Join("Bundles", "no-content.json"));

        // Assert
        string content = await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/");

        content.ShouldBe(string.Empty);
    }
    public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templated_Base64()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

        // Act
        options.RegisterBundle(Path.Join("Bundles", "templated-bundle-base64.json"));

        // Assert
        string content = await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/");

        content.ShouldBe("<html><head><title>Just Eat</title></head></html>");
    }
    public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Non_Default_Status_Codes()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

        // Act
        options.RegisterBundle(Path.Join("Bundles", "http-status-codes.json"));

        // Assert
        await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/1", HttpStatusCode.NotFound);

        await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/2", HttpStatusCode.NotFound);
    }
    public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Null_Json()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

        // Act
        options.RegisterBundle(Path.Join("Bundles", "content-as-null-json.json"));

        // Assert
        string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat");

        content.ShouldBe(string.Empty);
    }
示例#5
0
        public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Json_Array()
        {
            // Arrange
            var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

            // Act
            options.RegisterBundle(Path.Join("Bundles", "content-as-json-array.json"));

            // Assert
            string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat/repos");

            content
            .Replace(" ", string.Empty, StringComparison.Ordinal)
            .Replace("\n", string.Empty, StringComparison.Ordinal)
            .Replace("\r", string.Empty, StringComparison.Ordinal)
            .ShouldBe(@"[""httpclient-interception""]");
        }
示例#6
0
        public static async Task Can_Intercept_Http_Requests_With_Different_Path_If_The_Uri_Query_String()
        {
            // Arrange
            var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

            // Act
            options.RegisterBundle(Path.Join("Bundles", "ignoring-query.json"));

            // Assert
            string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat?foo=bar");

            content
            .Replace(" ", string.Empty, StringComparison.Ordinal)
            .Replace("\n", string.Empty, StringComparison.Ordinal)
            .Replace("\r", string.Empty, StringComparison.Ordinal)
            .ShouldBe(@"{""id"":1516790,""login"":""justeat"",""url"":""https://api.github.com/orgs/justeat""}");
        }
示例#7
0
        public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templated_Json()
        {
            // Arrange
            var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

            // Act
            options.RegisterBundle(Path.Join("Bundles", "templated-bundle-json.json"));

            // Assert
            string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat");

            content
            .Replace(" ", string.Empty, StringComparison.Ordinal)
            .Replace("\n", string.Empty, StringComparison.Ordinal)
            .Replace("\r", string.Empty, StringComparison.Ordinal)
            .ShouldBe(@"{""id"":1516790,""login"":""justeat"",""url"":""https://api.github.com/orgs/justeat""}");
        }
示例#8
0
        public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templated_String()
        {
            // Arrange
            var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

            var headers = new Dictionary <string, string>()
            {
                { "user-agent", "My-App/1.0.0" },
            };

            // Act
            options.RegisterBundle(Path.Join("Bundles", "templated-bundle-string.json"));

            // Assert
            string content = await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/", headers : headers);

            content.ShouldBe("<html><head><title>Just Eat</title></head></html>");
        }
示例#9
0
        public static async Task Can_Intercept_Http_Requests_From_Bundle_File()
        {
            // Arrange
            var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();

            var headers = new Dictionary <string, string>()
            {
                { "accept", "application/vnd.github.v3+json" },
                { "authorization", "token my-token" },
                { "user-agent", "My-App/1.0.0" },
            };

            // Act
            options.RegisterBundle(Path.Join("Bundles", "http-request-bundle.json"));

            // Assert
            await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/", mediaType : "text/html");

            await HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/order-history");

            await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat", headers : headers, mediaType : "application/json");
        }
示例#10
0
 /// <summary>
 /// Registers a bundle of HTTP request interceptions from a specified JSON file.
 /// </summary>
 /// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to register the bundle with.</param>
 /// <param name="path">The path of the JSON file containing the serialized bundle.</param>
 /// <returns>
 /// The value specified by <paramref name="options"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="options"/> or <paramref name="path"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="NotSupportedException">
 /// The version of the serialized bundle is not supported.
 /// </exception>
 public static HttpClientInterceptorOptions RegisterBundle(this HttpClientInterceptorOptions options, string path)
 {
     return(options.RegisterBundle(path, Array.Empty <KeyValuePair <string, string> >()));
 }