Skip to content

Commit

Permalink
Fix bundle templating with no values in bundle
Browse files Browse the repository at this point in the history
Fix templating not working if there are no values defined in `templateValues` of the bundle's JSON.
  • Loading branch information
martincostello committed Nov 15, 2022
1 parent 765b8c2 commit b4e6570
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/HttpClientInterception/Bundles/BundleItemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ public static HttpRequestInterceptionBuilder FromItem(
IEnumerable<KeyValuePair<string, string>> templateValues)
{
// Override the template values in the JSON with any user-specified values
if (item.TemplateValues?.Count > 0)
foreach (var pair in templateValues)
{
foreach (var pair in templateValues)
{
item.TemplateValues[pair.Key] = pair.Value;
}
item.TemplateValues ??= new Dictionary<string, string>();
item.TemplateValues[pair.Key] = pair.Value;
}

ValidateItem(item, out Uri? uri, out Version? version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ public static async Task Can_Intercept_Http_Requests_From_Bundle_File_With_Templ
.ShouldBe(@"[{""id"":123456,""name"":""httpclient-interception"",""full_name"":""justeat/httpclient-interception"",""private"":false,""owner"":{""login"":""justeat"",""id"":1516790}}]");
}

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

var templateValues = new Dictionary<string, string>()
{
["AvatarUrl"] = "https://avatars.githubusercontent.com/u/1516790?v=4",
["BlogUrl"] = "https://tech.justeattakeaway.com/",
["CompanyName"] = "justeat",
["RepoName"] = "httpclient-interception",
};

// Act
options.RegisterBundle(Path.Join("Bundles", "templated-bundle-json-no-parameters.json"), templateValues);

// Assert
string content = await HttpAssert.GetAsync(options, "https://api.github.com/orgs/justeat/repos");
content
.Replace(" ", string.Empty, StringComparison.Ordinal)
.Replace("\n", string.Empty, StringComparison.Ordinal)
.Replace("\r", string.Empty, StringComparison.Ordinal)
.ShouldBe(@"[{""id"":123456,""name"":""httpclient-interception"",""full_name"":""justeat/httpclient-interception"",""private"":false,""owner"":{""login"":""justeat"",""id"":1516790}}]");
}

[Fact]
public static void RegisterBundle_Validates_Parameters()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/main/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
"id": "templated-bundle-json-no-parameters",
"comment": "An HTTP request bundle that uses templating for JSON where the parameters are only defined in code.",
"version": 1,
"items": [
{
"comment": "An HTTP request for a JSON response that uses templating.",
"uri": "https://api.github.com/orgs/${CompanyName}/repos",
"contentFormat": "json",
"contentJson": [
{
"id": 123456,
"name": "${RepoName}",
"full_name": "${CompanyName}/${RepoName}",
"private": false,
"owner": {
"login": "${CompanyName}",
"id": 1516790
}
}
]
}
]
}

0 comments on commit b4e6570

Please sign in to comment.