Skip to content

Commit

Permalink
Add example test for bundles
Browse files Browse the repository at this point in the history
Add an example unit test for using bundle files.
  • Loading branch information
martincostello committed Mar 24, 2019
1 parent 3da002f commit b409e24
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/HttpClientInterception.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,5 +623,41 @@ public static async Task Use_Custom_Request_Matching_With_Priorities()
(await client.GetStringAsync("https://www.just-eat.co.uk/")).ShouldContain("Fourth");
}
}

[Fact]
public static async Task Intercept_Http_Requests_Registered_Using_A_Bundle_File()
{
// Arrange
var options = new HttpClientInterceptorOptions()
.RegisterBundle("example-bundle.json")
.ThrowsOnMissingRegistration();

// Act
string content;

using (var client = options.CreateHttpClient())
{
content = await client.GetStringAsync("https://www.just-eat.co.uk/");
}

// Assert
content.ShouldBe("<html><head><title>Just Eat</title></head></html>");

// Act
using (var client = options.CreateHttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
client.DefaultRequestHeaders.Add("Authorization", "bearer my-token");
client.DefaultRequestHeaders.Add("User-Agent", "My-App/1.0.0");

content = await client.GetStringAsync("https://api.github.com/orgs/justeat");
}

// Assert
var organization = JObject.Parse(content);
organization.Value<int>("id").ShouldBe(1516790);
organization.Value<string>("login").ShouldBe("justeat");
organization.Value<string>("url").ShouldBe("https://api.github.com/orgs/justeat");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json;Bundles\*.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="example-bundle.json;xunit.runner.json;Bundles\*.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\..\src\HttpClientInterception\Bundles\http-request-bundle-schema.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
Expand Down
41 changes: 41 additions & 0 deletions tests/HttpClientInterception.Tests/example-bundle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
"id": "example-http-request-bundle",
"comment": "An example bundle of HTTP requests to be intercepted.",
"version": 1,
"items": [
{
"id": "html",
"comment": "An HTTP request that returns an HTML string.",
"uri": "https://www.just-eat.co.uk/",
"contentString": "<html><head><title>Just Eat</title></head></html>"
},
{
"id": "json",
"comment": "An HTTP request that returns JSON.",
"uri": "https://api.github.com/orgs/justeat",
"requestHeaders": {
"Accept": [
"application/vnd.github.v3+json"
],
"Authorization": [
"bearer my-token"
],
"User-Agent": [
"My-App/1.0.0"
]
},
"responseHeaders": {
"X-RateLimit-Limit": [
"60"
]
},
"contentFormat": "json",
"contentJson": {
"id": 1516790,
"login": "justeat",
"url": "https://api.github.com/orgs/justeat"
}
}
]
}

0 comments on commit b409e24

Please sign in to comment.