Skip to content

Commit

Permalink
Merge pull request #17 from martincostello/Improve-Mutability-Documen…
Browse files Browse the repository at this point in the history
…tation

Improve mutability documentation
  • Loading branch information
martincostello authored Feb 23, 2018
2 parents 88ce3e4 + 799f450 commit 5671cd0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var client = options.CreateHttpClient();
var json = await client.GetStringAsync("http://public.je-apis.com/terms");
```

`HttpRequestInterceptionBuilder` objects are mutable, so properties can be freely changed once a particular setup has been registered with an instance of `HttpClientInterceptorOptions` as the state is captured at the point of registration. This allows multiple responses and paths to be configured from a single `HttpRequestInterceptionBuilder` instance where multiple registrations against a common hostname.

#### Fault Injection

Below is a minimal example of intercepting a request to inject an HTTP fault:
Expand Down
40 changes: 40 additions & 0 deletions tests/HttpClientInterception.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,45 @@ public static async Task Use_Multiple_Registrations()
dotnetOrg.Login.ShouldBe("dotnet");
dotnetOrg.Url.ShouldBe("https://api.github.com/orgs/dotnet");
}

[Fact]
public static async Task Use_The_Same_Builder_For_Multiple_Registrations_On_The_Same_Host()
{
// Arrange
var options = new HttpClientInterceptorOptions();

// Configure a response for https://api.github.com/orgs/justeat
var builder = new HttpRequestInterceptionBuilder()
.ForHttps()
.ForHost("api.github.com")
.ForPath("orgs/justeat")
.WithJsonContent(new { id = 1516790, login = "justeat", url = "https://api.github.com/orgs/justeat" });

options.Register(builder);

// Update the same builder to configure a response for https://api.github.com/orgs/dotnet
builder.ForPath("orgs/dotnet")
.WithJsonContent(new { id = 9141961, login = "dotnet", url = "https://api.github.com/orgs/dotnet" });

options.Register(builder);

var service = RestService.For<IGitHub>(options.CreateHttpClient("https://api.github.com"));

// Act
Organization justEatOrg = await service.GetOrganizationAsync("justeat");
Organization dotnetOrg = await service.GetOrganizationAsync("dotnet");

// Assert
justEatOrg.ShouldNotBeNull();
justEatOrg.Id.ShouldBe("1516790");
justEatOrg.Login.ShouldBe("justeat");
justEatOrg.Url.ShouldBe("https://api.github.com/orgs/justeat");

// Assert
dotnetOrg.ShouldNotBeNull();
dotnetOrg.Id.ShouldBe("9141961");
dotnetOrg.Login.ShouldBe("dotnet");
dotnetOrg.Url.ShouldBe("https://api.github.com/orgs/dotnet");
}
}
}

0 comments on commit 5671cd0

Please sign in to comment.