Skip to content

Commit b60b750

Browse files
Merge pull request #78 from martincostello/Add-Bundle-Ignore-Support
Add bundle ignore support
2 parents a756ccc + c4d2e21 commit b60b750

File tree

8 files changed

+157
-2
lines changed

8 files changed

+157
-2
lines changed

src/HttpClientInterception/BundleExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ public static HttpClientInterceptorOptions RegisterBundle(
7676
{
7777
foreach (var item in bundle.Items)
7878
{
79-
if (item != null)
79+
if (item == null || item.Skip)
8080
{
81-
builders.Add(BundleItemConverter.FromItem(item, templateValues));
81+
continue;
8282
}
83+
84+
builders.Add(BundleItemConverter.FromItem(item, templateValues));
8385
}
8486
}
8587

src/HttpClientInterception/Bundles/BundleItem.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,23 @@ internal sealed class BundleItem
9797
/// </summary>
9898
[JsonProperty("templateValues", NullValueHandling = NullValueHandling.Ignore)]
9999
public IDictionary<string, string> TemplateValues { get; set; }
100+
101+
/// <summary>
102+
/// Gets or sets a value indicating whether to ignore the URI's path.
103+
/// </summary>
104+
[JsonProperty("ignorePath", NullValueHandling = NullValueHandling.Ignore)]
105+
public bool IgnorePath { get; set; }
106+
107+
/// <summary>
108+
/// Gets or sets a value indicating whether to ignore the URI's query string.
109+
/// </summary>
110+
[JsonProperty("ignoreQuery", NullValueHandling = NullValueHandling.Ignore)]
111+
public bool IgnoreQuery { get; set; }
112+
113+
/// <summary>
114+
/// Gets or sets a value indicating whether to skip the item.
115+
/// </summary>
116+
[JsonProperty("skip", NullValueHandling = NullValueHandling.Ignore)]
117+
public bool Skip { get; set; }
100118
}
101119
}

src/HttpClientInterception/Bundles/BundleItemConverter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public static HttpRequestInterceptionBuilder FromItem(
7070
builder.HavingPriority(item.Priority.Value);
7171
}
7272

73+
if (item.IgnorePath)
74+
{
75+
builder.IgnoringPath(ignorePath: true);
76+
}
77+
78+
if (item.IgnoreQuery)
79+
{
80+
builder.IgnoringQuery(ignoreQuery: true);
81+
}
82+
7383
return builder.SetContent(item);
7484
}
7585

src/HttpClientInterception/Bundles/http-request-bundle-schema.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
"type": "string",
4747
"description": "The absolute URI for the HTTP request."
4848
},
49+
"ignorePath": {
50+
"type": "boolean",
51+
"description": "Whether to ignore the URI's path when matching the interception.",
52+
"default": false
53+
},
54+
"ignoreQuery": {
55+
"type": "boolean",
56+
"description": "Whether to ignore the URI's query string when matching the interception.",
57+
"default": false
58+
},
4959
"priority": {
5060
"type": "integer",
5161
"description": "The optional priority value for the item."
@@ -113,6 +123,11 @@
113123
"additionalProperties": {
114124
"type": "string"
115125
}
126+
},
127+
"skip": {
128+
"type": "boolean",
129+
"description": "Whether to skip the item so it is not used.",
130+
"default": false
116131
}
117132
},
118133
"required": [ "uri" ]

tests/HttpClientInterception.Tests/Bundles/BundleExtensionsTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,58 @@ public static async Task Can_Intercept_Http_Requests_From_Bundle_File_If_No_Cont
333333
content.ShouldBe(string.Empty);
334334
}
335335

336+
[Fact]
337+
public static async Task Can_Intercept_Http_Requests_With_Different_Path_If_The_Uri_Path_Is_Ignored()
338+
{
339+
// Arrange
340+
var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();
341+
342+
// Act
343+
options.RegisterBundle(Path.Join("Bundles", "ignoring-path.json"));
344+
345+
// Assert
346+
string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/some-other-org");
347+
348+
content
349+
.Replace(" ", string.Empty, StringComparison.Ordinal)
350+
.Replace("\n", string.Empty, StringComparison.Ordinal)
351+
.Replace("\r", string.Empty, StringComparison.Ordinal)
352+
.ShouldBe(@"{""id"":1516790,""login"":""justeat"",""url"":""https://api.github.com/orgs/justeat""}");
353+
}
354+
355+
[Fact]
356+
public static async Task Can_Intercept_Http_Requests_With_Different_Path_If_The_Uri_Query_String()
357+
{
358+
// Arrange
359+
var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();
360+
361+
// Act
362+
options.RegisterBundle(Path.Join("Bundles", "ignoring-query.json"));
363+
364+
// Assert
365+
string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat?foo=bar");
366+
367+
content
368+
.Replace(" ", string.Empty, StringComparison.Ordinal)
369+
.Replace("\n", string.Empty, StringComparison.Ordinal)
370+
.Replace("\r", string.Empty, StringComparison.Ordinal)
371+
.ShouldBe(@"{""id"":1516790,""login"":""justeat"",""url"":""https://api.github.com/orgs/justeat""}");
372+
}
373+
374+
[Fact]
375+
public static async Task Can_Skip_Bundle_Items()
376+
{
377+
// Arrange
378+
var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();
379+
380+
// Act
381+
options.RegisterBundle(Path.Join("Bundles", "skipped-item-bundle.json"));
382+
383+
// Assert
384+
await Assert.ThrowsAsync<InvalidOperationException>(
385+
() => HttpAssert.GetAsync(options, "https://www.just-eat.co.uk/"));
386+
}
387+
336388
[Theory]
337389
[MemberData(nameof(BundleFiles))]
338390
public static async Task Bundle_Schema_Is_Valid_From_Test(string bundlePath)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
3+
"id": "ignoring-path-bundle",
4+
"comment": "An HTTP request bundle where the path is ignored.",
5+
"items": [
6+
{
7+
"id": "JSON",
8+
"comment": "An HTTP request where the path is ignored.",
9+
"uri": "https://api.github.com/orgs/justeat",
10+
"ignorePath": true,
11+
"contentFormat": "json",
12+
"contentJson": {
13+
"id": 1516790,
14+
"login": "justeat",
15+
"url": "https://api.github.com/orgs/justeat"
16+
}
17+
}
18+
]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
3+
"id": "ignoring-path-bundle",
4+
"comment": "An HTTP request bundle where the path is ignored.",
5+
"items": [
6+
{
7+
"id": "JSON",
8+
"comment": "An HTTP request where the query string is ignored.",
9+
"uri": "https://api.github.com/orgs/justeat",
10+
"ignoreQuery": true,
11+
"contentFormat": "json",
12+
"contentJson": {
13+
"id": 1516790,
14+
"login": "justeat",
15+
"url": "https://api.github.com/orgs/justeat"
16+
}
17+
}
18+
]
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
3+
"id": "test-skipped-item-bundle-bundle",
4+
"comment": "An HTTP request bundle where the item is skipped.",
5+
"version": 1,
6+
"items": [
7+
{
8+
"id": "html",
9+
"comment": "A skipped HTTP request.",
10+
"uri": "https://www.just-eat.co.uk/",
11+
"contentHeaders": {
12+
"content-type": [
13+
"text/html; charset=utf-8"
14+
]
15+
},
16+
"contentString": "<html><head><title>Just Eat</title></head></html>",
17+
"skip": true
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)