示例#1
0
        public async Task Can_Get_Organization_Repositories()
        {
            // Add a callback to log any HTTP requests made
            _fixture.Interceptor.OnSend = (request) =>
            {
                _outputHelper.WriteLine($"HTTP {request.Method} {request.RequestUri}");
                return(Task.CompletedTask);
            };

            // Arrange - use a scope to clean-up registrations
            using (_fixture.Interceptor.BeginScope())
            {
                // Setup an expected response from the GitHub API
                var builder = new HttpRequestInterceptionBuilder()
                              .ForHttps()
                              .ForHost("api.github.com")
                              .ForPath("orgs/weyland-yutani/repos")
                              .ForQuery("per_page=2")
                              .WithJsonContent(
                    new[]
                {
                    new { id = 1, name = "foo" },
                    new { id = 2, name = "bar" },
                });

                // Add a callback for when a request is intercepted
                builder.WithInterceptionCallback(
                    (request) => _outputHelper.WriteLine($"Intercepted HTTP {request.Method} {request.RequestUri}"));

                _fixture.Interceptor.Register(builder);

                string[] actual;

                using (var httpClient = new HttpClient())
                {
                    // Set up the HTTP client to use the self-hosted HTTP server for the application
                    httpClient.BaseAddress = new Uri(_fixture.ServerUrl);

                    // Act - Perform the HTTP request against our application and deserialize the response
                    string json = await httpClient.GetStringAsync("api/repos?count=2");

                    actual = JsonConvert.DeserializeObject <string[]>(json);
                }

                // Assert - Our application should have parsed the stub-names
                actual.ShouldBe(new[] { "bar", "foo" });
            }
        }