Skip to content

Commit

Permalink
Added WithCancellationTokenSource method to the IServerBuilder (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaylokenov committed Aug 1, 2016
1 parent 976d95a commit cee06cc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ public void WithDefaultHeadersShouldWorkCorrectly()
.WithStatusCode(HttpStatusCode.OK);
}

[Test]
public void WithCancellationTokenSourceShouldWorkCorrectly()
{
}

[TestFixtureTearDown]
public void RestoreConfiguration()
{
Expand Down
5 changes: 5 additions & 0 deletions src/MyTested.WebApi.Tests/MyTested.WebApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.1.0\lib\net45\System.Web.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Http.Owin, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Owin.5.1.0\lib\net45\System.Web.Http.Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -158,6 +162,7 @@
<Compile Include="MyWebApiTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Setups\WebApiConfig.cs" />
<Compile Include="Setups\WebApiStartup.cs" />
<Compile Include="TestsStartup.cs" />
<Compile Include="UtilitiesTests\ExpressionParserTests.cs" />
<Compile Include="UtilitiesTests\ReflectionTests.cs" />
Expand Down
26 changes: 26 additions & 0 deletions src/MyTested.WebApi.Tests/Setups/WebApiStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace MyTested.WebApi.Tests.Setups
{
using System.Web.Http;
using Owin;

public class WebApiStartup
{
public void Configuration(IAppBuilder app)
{
app.Use((context, next) =>
{
if (context.Request.CallCancelled.IsCancellationRequested)
{
context.Response.StatusCode = 500;
context.Response.Write("Canceled");
}
return next();
});

var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
}
1 change: 1 addition & 0 deletions src/MyTested.WebApi.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.1.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.1.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.1.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Testing" version="3.0.1" targetFramework="net45" />
Expand Down
38 changes: 11 additions & 27 deletions src/MyTested.WebApi/Builders/Servers/ServerTestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ServerTestBuilder : IServerBuilder, IServerTestBuilder
private readonly IDisposable server;

private HttpRequestMessage httpRequestMessage;
private CancellationToken requestCancellationToken;
private CancellationTokenSource requestCancellationTokenSource;

/// <summary>
/// Initializes a new instance of the <see cref="ServerTestBuilder" /> class.
Expand All @@ -46,7 +46,6 @@ public class ServerTestBuilder : IServerBuilder, IServerTestBuilder
this.transformRequest = transformRequest;
this.disposeServer = disposeServer;
this.server = server;
this.requestCancellationToken = CancellationToken.None;
}

/// <summary>
Expand Down Expand Up @@ -83,34 +82,15 @@ public IServerBuilder WithDefaultRequestHeaders(IDictionary<string, IEnumerable<
headers.ForEach(h => this.WithDefaultRequestHeader(h.Key, h.Value));
return this;
}

/// <summary>
/// Adds canceled cancellation token to the server request.
/// </summary>
/// <returns>The same server builder.</returns>
public IServerBuilder WithCancellationToken()
{
return this.WithCancellationToken(true);
}


/// <summary>
/// Adds cancellation token to the server request.
/// Adds cancellation token source to the server request.
/// </summary>
/// <param name="cancelled">True or false indicating whether the token is canceled.</param>
/// <param name="cancellationTokenSource">Cancellation token source to use for the server request.</param>
/// <returns>The same server builder.</returns>
public IServerBuilder WithCancellationToken(bool cancelled)
public IServerBuilder WithCancellationTokenSource(CancellationTokenSource cancellationTokenSource)
{
return this.WithCancellationToken(new CancellationToken(cancelled));
}

/// <summary>
/// Adds cancellation token to the server request.
/// </summary>
/// <param name="cancellationToken">Cancellation token to add to the server request.</param>
/// <returns>The same server builder.</returns>
public IServerBuilder WithCancellationToken(CancellationToken cancellationToken)
{
this.requestCancellationToken = cancellationToken;
this.requestCancellationTokenSource = cancellationTokenSource;
return this;
}

Expand Down Expand Up @@ -153,7 +133,11 @@ public IHttpHandlerResponseMessageWithTimeTestBuilder ShouldReturnHttpResponseMe
{
var stopwatch = Stopwatch.StartNew();

var httpResponseMessage = invoker.SendAsync(this.httpRequestMessage, this.requestCancellationToken).Result;
var cancellationToken = this.requestCancellationTokenSource != null
? this.requestCancellationTokenSource.Token
: CancellationToken.None;

var httpResponseMessage = invoker.SendAsync(this.httpRequestMessage, cancellationToken).Result;

stopwatch.Stop();

Expand Down
19 changes: 3 additions & 16 deletions src/MyTested.WebApi/IServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,11 @@ public interface IServerBuilder
IServerBuilder WithDefaultRequestHeaders(IDictionary<string, IEnumerable<string>> headers);

/// <summary>
/// Adds canceled cancellation token to the server request.
/// Adds cancellation token source to the server request.
/// </summary>
/// <param name="cancellationTokenSource">Cancellation token source to use for the server request.</param>
/// <returns>The same server builder.</returns>
IServerBuilder WithCancellationToken();

/// <summary>
/// Adds cancellation token to the server request.
/// </summary>
/// <param name="cancelled">True or false indicating whether the token is canceled.</param>
/// <returns>The same server builder.</returns>
IServerBuilder WithCancellationToken(bool cancelled);

/// <summary>
/// Adds cancellation token to the server request.
/// </summary>
/// <param name="cancellationToken">Cancellation token to add to the server request.</param>
/// <returns>The same server builder.</returns>
IServerBuilder WithCancellationToken(CancellationToken cancellationToken);
IServerBuilder WithCancellationTokenSource(CancellationTokenSource cancellationTokenSource);

/// <summary>
/// Adds HTTP request message to the tested server.
Expand Down

0 comments on commit cee06cc

Please sign in to comment.