Skip to content

Commit e8a9a6c

Browse files
Add docs for bundles
Add documentation for HTTP bundles. Resolves #75.
1 parent b409e24 commit e8a9a6c

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ dotnet add package JustEat.HttpClientInterception
3131

3232
#### Request Interception
3333

34-
Below is a minimal example of intercepting a request to an HTTP API for a JSON resource to return a custom response:
34+
##### Fluent API
35+
36+
Below is a minimal example of intercepting a request to an HTTP API for a JSON resource to return a custom response using the fluent API:
3537

3638
```csharp
3739
// using JustEat.HttpClientInterception;
@@ -54,6 +56,62 @@ var json = await client.GetStringAsync("http://public.je-apis.com/terms");
5456

5557
`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.
5658

59+
##### _HTTP Bundle_ Files
60+
61+
HTTP requests to intercept can also be configured in an _"HTTP bundle"_ file, which can be used to store the HTTP requests to intercept and their corresponding responses as JSON.
62+
63+
This functionality is analogous to our [_Shock_](https://github.com/justeat/Shock "Shock") pod for iOS.
64+
65+
###### JSON
66+
67+
Below is an example bundle file, which can return content in formats such as a string, JSON and base64-encoded data.
68+
69+
The full JSON schema for HTTP bundle files can be found [here](https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json "JSON Schema for HTTP request interception bundles for use with JustEat.HttpClientInterception.").
70+
71+
```json
72+
{
73+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
74+
"id": "my-bundle",
75+
"comment": "A bundle of HTTP requests",
76+
"items": [
77+
{
78+
"id": "home",
79+
"comment": "Returns the home page",
80+
"uri": "https://www.just-eat.co.uk",
81+
"contentString": "<html><head><title>Just Eat</title></head></html>"
82+
},
83+
{
84+
"id": "terms",
85+
"comment": "Returns the Ts & Cs",
86+
"uri": "https://public.je-apis.com/terms",
87+
"contentFormat": "json",
88+
"contentJson": {
89+
"Id": 1,
90+
"Link": "https://www.just-eat.co.uk/privacy-policy"
91+
}
92+
}
93+
]
94+
}
95+
```
96+
97+
###### Code
98+
99+
```cs
100+
// using JustEat.HttpClientInterception;
101+
102+
var options = new HttpClientInterceptorOptions().RegisterBundle("my-bundle.json");
103+
104+
var client = options.CreateHttpClient();
105+
106+
// The value of html will be "<html><head><title>Just Eat</title></head></html>"
107+
var html = await client.GetStringAsync("https://www.just-eat.co.uk");
108+
109+
// The value of json will be "{\"Id\":1,\"Link\":\"https://www.just-eat.co.uk/privacy-policy\"}"
110+
var json = await client.GetStringAsync("http://public.je-apis.com/terms");
111+
```
112+
113+
Further examples of using HTTP bundles can be found in the [tests](https://github.com/justeat/httpclient-interception/blob/master/tests/HttpClientInterception.Tests/Bundles/BundleExtensionsTests.cs "BundleExtensionsTests.cs"), such as for changing the response code, the HTTP method, and matching to HTTP requests based on the request headers.
114+
57115
#### Fault Injection
58116

59117
Below is a minimal example of intercepting a request to inject an HTTP fault:

0 commit comments

Comments
 (0)