-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish porting tests for KubeClient.Http
- Loading branch information
Showing
18 changed files
with
2,013 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
test/KubeClient.Tests/Http/BuildMessage/TypedRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Net.Http; | ||
using Xunit; | ||
|
||
namespace KubeClient.Tests.Http.BuildMessage | ||
{ | ||
using KubeClient.Http; | ||
using KubeClient.Http.Testability.Xunit; | ||
|
||
/// <summary> | ||
/// Message-building tests for an <see cref="HttpRequest{TContext}"/> (<see cref="HttpRequest{TContext}.BuildRequestMessage"/>). | ||
/// </summary> | ||
public class TypedRequest | ||
{ | ||
/// <summary> | ||
/// The default context for requests used by tests. | ||
/// </summary> | ||
static readonly string DefaultContext = "Hello World"; | ||
|
||
/// <summary> | ||
/// An empty request. | ||
/// </summary> | ||
static readonly HttpRequest<string> EmptyRequest = HttpRequest<string>.Empty; | ||
|
||
/// <summary> | ||
/// A request with an absolute URI. | ||
/// </summary> | ||
static readonly HttpRequest<string> AbsoluteRequest = HttpRequest<string>.Create("http://localhost:1234"); | ||
|
||
/// <summary> | ||
/// A request with a relative URI. | ||
/// </summary> | ||
static readonly HttpRequest<string> RelativeRequest = HttpRequest<string>.Create("foo/bar"); | ||
|
||
#region Empty requests | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> throws <see cref="InvalidOperationException"/>. | ||
/// </summary> | ||
[Fact] | ||
public void Empty_Throws() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
EmptyRequest.BuildRequestMessage(HttpMethod.Get, DefaultContext); | ||
}); | ||
} | ||
|
||
#endregion Empty requests | ||
|
||
#region Relative URIs | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with a relative URI throws <see cref="InvalidOperationException"/> if no base URI is supplied. | ||
/// </summary> | ||
[Fact] | ||
public void RelativeUri_NoBaseUri_Throws() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
RelativeRequest.BuildRequestMessage(HttpMethod.Get, DefaultContext); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with a relative URI prepends the supplied base URI to the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void RelativeUri_BaseUri_PrependsBaseUri() | ||
{ | ||
Uri baseUri = new Uri("http://tintoy.io:5678/"); | ||
|
||
RequestAssert.MessageHasUri(RelativeRequest, DefaultContext, baseUri, | ||
expectedUri: new Uri(baseUri, RelativeRequest.Uri) | ||
); | ||
} | ||
|
||
#endregion // Relative URIs | ||
|
||
#region Absolute URIs | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI ignores the lack of a base URI and uses the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_NoBaseUri_UsesRequestUri() | ||
{ | ||
RequestAssert.MessageHasUri(AbsoluteRequest, DefaultContext, | ||
expectedUri: AbsoluteRequest.Uri | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI ignores the supplied base URI and uses the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_BaseUri_UsesRequestUri() | ||
{ | ||
Uri baseUri = new Uri("http://tintoy.io:5678/"); | ||
|
||
RequestAssert.MessageHasUri(AbsoluteRequest, DefaultContext, baseUri, | ||
expectedUri: AbsoluteRequest.Uri | ||
); | ||
} | ||
|
||
#endregion // Absolute URIs | ||
} | ||
} |
251 changes: 251 additions & 0 deletions
251
test/KubeClient.Tests/Http/BuildMessage/UntypedRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
using System; | ||
using System.Net.Http; | ||
using Xunit; | ||
|
||
namespace KubeClient.Tests.Http.BuildMessage | ||
{ | ||
using KubeClient.Http; | ||
using KubeClient.Http.Testability.Xunit; | ||
|
||
/// <summary> | ||
/// Message-building tests for <see cref="HttpRequest"/> (<see cref="HttpRequest.BuildRequestMessage"/>). | ||
/// </summary> | ||
public class UntypedRequest | ||
{ | ||
/// <summary> | ||
/// A request with an absolute URI. | ||
/// </summary> | ||
static readonly HttpRequest AbsoluteRequest = HttpRequest.Create("http://localhost:1234"); | ||
|
||
/// <summary> | ||
/// A request with a relative URI. | ||
/// </summary> | ||
static readonly HttpRequest RelativeRequest = HttpRequest.Create("foo/bar"); | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> throws <see cref="InvalidOperationException"/>. | ||
/// </summary> | ||
[Fact] | ||
public void Empty_Throws() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
HttpRequest.Empty.BuildRequestMessage(HttpMethod.Get); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with a relative URI throws <see cref="InvalidOperationException"/> if no base URI is supplied. | ||
/// </summary> | ||
[Fact] | ||
public void RelativeUri_NoBaseUri_Throws() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
RelativeRequest.BuildRequestMessage(HttpMethod.Get); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with a relative URI prepends the supplied base URI to the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void RelativeUri_BaseUri_PrependsBaseUri() | ||
{ | ||
Uri baseUri = new Uri("http://tintoy.io:5678/"); | ||
|
||
RequestAssert.MessageHasUri(RelativeRequest, baseUri, | ||
expectedUri: new Uri(baseUri, RelativeRequest.Uri) | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI ignores the lack of a base URI and uses the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_NoBaseUri_UsesRequestUri() | ||
{ | ||
RequestAssert.MessageHasUri(AbsoluteRequest, | ||
expectedUri: AbsoluteRequest.Uri | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI ignores the supplied base URI and uses the request URI. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_BaseUri_UsesRequestUri() | ||
{ | ||
Uri baseUri = new Uri("http://tintoy.io:5678/"); | ||
|
||
RequestAssert.MessageHasUri(AbsoluteRequest, baseUri, | ||
expectedUri: AbsoluteRequest.Uri | ||
); | ||
} | ||
|
||
#region Template URIs | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute template URI, using statically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void Absoluteuri_Template() | ||
{ | ||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}") | ||
.WithTemplateParameter("action", "foo") | ||
.WithTemplateParameter("id", "bar"); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute template URI, using dynamically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_Template_DeferredValues() | ||
{ | ||
string action = "foo"; | ||
string id = "bar"; | ||
|
||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}") | ||
.WithTemplateParameter("action", () => action) | ||
.WithTemplateParameter("id", () => id); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar" | ||
); | ||
|
||
action = "diddly"; | ||
id = "dee"; | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/diddly/dee" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute template URI that includes a query component, using statically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_Template_Query() | ||
{ | ||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/{action}/{id}?flag={flag}") | ||
.WithTemplateParameter("action", "foo") | ||
.WithTemplateParameter("id", "bar") | ||
.WithTemplateParameter("flag", true); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?flag=True" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute template URI that includes a query component, using dynamically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_Template_Query_DeferredValues() | ||
{ | ||
string action = "foo"; | ||
string id = "bar"; | ||
bool? flag = true; | ||
|
||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/") | ||
.WithRelativeUri("{action}/{id}?flag={flag?}") | ||
.WithTemplateParameter("action", () => action) | ||
.WithTemplateParameter("id", () => id) | ||
.WithTemplateParameter("flag", () => flag); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?flag=True" | ||
); | ||
|
||
action = "diddly"; | ||
id = "dee"; | ||
flag = null; | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/diddly/dee" | ||
); | ||
} | ||
|
||
#endregion // Template URIs | ||
|
||
#region Query parameters | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, using statically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_Query() | ||
{ | ||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/foo/bar") | ||
.WithQueryParameter("flag", true); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?flag=True" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, using dynamically-bound template parameters. | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_AddQuery_DeferredValues() | ||
{ | ||
bool? flag = true; | ||
|
||
HttpRequest request = | ||
HttpRequest.Factory.Create("http://localhost:1234/foo/bar") | ||
.WithQueryParameter("flag", () => flag); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?flag=True" | ||
); | ||
|
||
flag = null; | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, with no additional path component). | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_AddQuery_EmptyPath() | ||
{ | ||
HttpRequest request = | ||
AbsoluteRequest.WithRelativeUri("foo/bar") | ||
.WithRelativeUri("?baz=bonk"); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?baz=bonk" | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An <see cref="HttpRequest"/> with an absolute URI that adds a query component, with no additional path component). | ||
/// </summary> | ||
[Fact] | ||
public void AbsoluteUri_WithQuery_AddQuery_EmptyPath() | ||
{ | ||
HttpRequest request = | ||
AbsoluteRequest.WithRelativeUri("foo/bar?baz=bonk") | ||
.WithRelativeUri("?bo=diddly"); | ||
|
||
RequestAssert.MessageHasUri(request, | ||
expectedUri: "http://localhost:1234/foo/bar?baz=bonk&bo=diddly" | ||
); | ||
} | ||
|
||
#endregion // Query parameters | ||
} | ||
} |
Oops, something went wrong.