Skip to content

Commit f21a9cf

Browse files
Merge pull request #495 from martincostello/fix-bundles-with-no-parameters
Fix bundle templating with no values in bundle
2 parents 765b8c2 + b4e6570 commit f21a9cf

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/HttpClientInterception/Bundles/BundleItemConverter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ public static HttpRequestInterceptionBuilder FromItem(
1616
IEnumerable<KeyValuePair<string, string>> templateValues)
1717
{
1818
// Override the template values in the JSON with any user-specified values
19-
if (item.TemplateValues?.Count > 0)
19+
foreach (var pair in templateValues)
2020
{
21-
foreach (var pair in templateValues)
22-
{
23-
item.TemplateValues[pair.Key] = pair.Value;
24-
}
21+
item.TemplateValues ??= new Dictionary<string, string>();
22+
item.TemplateValues[pair.Key] = pair.Value;
2523
}
2624

2725
ValidateItem(item, out Uri? uri, out Version? version);

tests/HttpClientInterception.Tests/Bundles/BundleExtensionsTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,32 @@ public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templ
249249
.ShouldBe(@"[{""id"":123456,""name"":""httpclient-interception"",""full_name"":""justeat/httpclient-interception"",""private"":false,""owner"":{""login"":""justeat"",""id"":1516790}}]");
250250
}
251251

252+
[Fact]
253+
public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templated_Json_TemplateValues_Only_Defined_In_Code()
254+
{
255+
// Arrange
256+
var options = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();
257+
258+
var templateValues = new Dictionary<string, string>()
259+
{
260+
["AvatarUrl"] = "https://avatars.githubusercontent.com/u/1516790?v=4",
261+
["BlogUrl"] = "https://tech.justeattakeaway.com/",
262+
["CompanyName"] = "justeat",
263+
["RepoName"] = "httpclient-interception",
264+
};
265+
266+
// Act
267+
options.RegisterBundle(Path.Join("Bundles", "templated-bundle-json-no-parameters.json"), templateValues);
268+
269+
// Assert
270+
string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat/repos");
271+
content
272+
.Replace(" ", string.Empty, StringComparison.Ordinal)
273+
.Replace("\n", string.Empty, StringComparison.Ordinal)
274+
.Replace("\r", string.Empty, StringComparison.Ordinal)
275+
.ShouldBe(@"[{""id"":123456,""name"":""httpclient-interception"",""full_name"":""justeat/httpclient-interception"",""private"":false,""owner"":{""login"":""justeat"",""id"":1516790}}]");
276+
}
277+
252278
[Fact]
253279
public static void RegisterBundle_Validates_Parameters()
254280
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/main/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
3+
"id": "templated-bundle-json-no-parameters",
4+
"comment": "An HTTP request bundle that uses templating for JSON where the parameters are only defined in code.",
5+
"version": 1,
6+
"items": [
7+
{
8+
"comment": "An HTTP request for a JSON response that uses templating.",
9+
"uri": "https://api.github.com/orgs/${CompanyName}/repos",
10+
"contentFormat": "json",
11+
"contentJson": [
12+
{
13+
"id": 123456,
14+
"name": "${RepoName}",
15+
"full_name": "${CompanyName}/${RepoName}",
16+
"private": false,
17+
"owner": {
18+
"login": "${CompanyName}",
19+
"id": 1516790
20+
}
21+
}
22+
]
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)