Skip to content

Commit

Permalink
Merge pull request #54 from geeklearningio/release/1.4.0
Browse files Browse the repository at this point in the history
Release/1.4.0
  • Loading branch information
arnaudauroux authored Aug 1, 2018
2 parents f83db84 + f37d903 commit 2539a14
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/GeekLearning.Testavior/Environment/ITestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ITestEnvironment

HttpClient Client { get; }

HttpClient CreateClient(bool supportCookies = true);

IServiceProvider ServiceProvider { get; }
}
}
16 changes: 15 additions & 1 deletion src/GeekLearning.Testavior/Environment/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ public TestEnvironment(string targetProjectRelativePath = null)
}

this.Server = this.CreateTestServer();
this.Client = Server.CreateClient();
this.Client = this.CreateClient(false);
}

public HttpClient CreateClient(bool supportCookies = true)
{
if (supportCookies)
{
var client = new HttpClient(new TestMessageHandler(this.Server.CreateHandler()));
client.BaseAddress = this.Server.BaseAddress;
return client;
}
else
{
return this.Server.CreateClient();
}
}

protected virtual TestServer CreateTestServer()
Expand Down
45 changes: 45 additions & 0 deletions src/GeekLearning.Testavior/Helpers/Http/TestMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GeekLearning.Testavior
{
public class TestMessageHandler : DelegatingHandler
{
private readonly CookieContainer cookies = new System.Net.CookieContainer();

public TestMessageHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("Cookie", this.cookies.GetCookieHeader(request.RequestUri));

var resp = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (resp.Headers.TryGetValues("Set-Cookie", out var newCookies))
{
foreach (var item in SetCookieHeaderValue.ParseList(newCookies.ToList()))
{
if (item.Domain.HasValue)
{
this.cookies.Add(request.RequestUri, new Cookie(item.Name.Value, item.Value.Value, item.Path.Value, item.Domain.Value));
}
else
{
this.cookies.Add(request.RequestUri, new Cookie(item.Name.Value, item.Value.Value, item.Path.Value));
}
}
}

return resp;
}
}
}

0 comments on commit 2539a14

Please sign in to comment.