Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
#896 Have TestHost populate features before calling IHttpContextFacto…
Browse files Browse the repository at this point in the history
…ry.Create
  • Loading branch information
Tratcher committed Feb 28, 2017
1 parent af953b4 commit 9deb626
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/Microsoft.AspNetCore.TestHost/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,49 +126,51 @@ internal RequestState(HttpRequestMessage request, PathString pathBase, IHttpAppl
}

var contextFeatures = new FeatureCollection();
contextFeatures.Set<IHttpRequestFeature>(new RequestFeature());
var requestFeature = new RequestFeature();
contextFeatures.Set<IHttpRequestFeature>(requestFeature);
_responseFeature = new ResponseFeature();
contextFeatures.Set<IHttpResponseFeature>(_responseFeature);
Context = application.CreateContext(contextFeatures);
var httpContext = Context.HttpContext;
var requestLifetimeFeature = new HttpRequestLifetimeFeature();
contextFeatures.Set<IHttpRequestLifetimeFeature>(requestLifetimeFeature);

var serverRequest = httpContext.Request;
serverRequest.Protocol = "HTTP/" + request.Version.ToString(2);
serverRequest.Scheme = request.RequestUri.Scheme;
serverRequest.Method = request.Method.ToString();
requestFeature.Protocol = "HTTP/" + request.Version.ToString(2);
requestFeature.Scheme = request.RequestUri.Scheme;
requestFeature.Method = request.Method.ToString();

var fullPath = PathString.FromUriComponent(request.RequestUri);
PathString remainder;
if (fullPath.StartsWithSegments(pathBase, out remainder))
{
serverRequest.PathBase = pathBase;
serverRequest.Path = remainder;
requestFeature.PathBase = pathBase.Value;
requestFeature.Path = remainder.Value;
}
else
{
serverRequest.PathBase = PathString.Empty;
serverRequest.Path = fullPath;
requestFeature.PathBase = string.Empty;
requestFeature.Path = fullPath.Value;
}

serverRequest.QueryString = QueryString.FromUriComponent(request.RequestUri);
requestFeature.QueryString = QueryString.FromUriComponent(request.RequestUri).Value;

foreach (var header in request.Headers)
{
serverRequest.Headers.Append(header.Key, header.Value.ToArray());
requestFeature.Headers.Append(header.Key, header.Value.ToArray());
}
var requestContent = request.Content;
if (requestContent != null)
{
foreach (var header in request.Content.Headers)
{
serverRequest.Headers.Append(header.Key, header.Value.ToArray());
requestFeature.Headers.Append(header.Key, header.Value.ToArray());
}
}

_responseStream = new ResponseStream(ReturnResponseMessageAsync, AbortRequest);
httpContext.Response.Body = _responseStream;
httpContext.Response.StatusCode = 200;
httpContext.RequestAborted = _requestAbortedSource.Token;
_responseFeature.Body = _responseStream;
_responseFeature.StatusCode = 200;
requestLifetimeFeature.RequestAborted = _requestAbortedSource.Token;

Context = application.CreateContext(contextFeatures);
}

public Context Context { get; private set; }
Expand Down

0 comments on commit 9deb626

Please sign in to comment.