Skip to content

Commit

Permalink
Use FormFeature to populate Request.Form
Browse files Browse the repository at this point in the history
* Populate ContenType and ContentLength to Headers
  • Loading branch information
joemcbride committed Feb 16, 2017
1 parent baad396 commit c987f5a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 55 deletions.
2 changes: 2 additions & 0 deletions src/Alba/FormDataExtensions.cs
Expand Up @@ -15,6 +15,8 @@ public static class FormDataExtensions
{
var post = formData(values).Join("&");

context.Request.ContentLength = post.Length;

var postBytes = Encoding.UTF8.GetBytes(post);

var stream = new MemoryStream();
Expand Down
35 changes: 35 additions & 0 deletions src/Alba/HeaderExtensions.cs
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace Alba
{
public static class HeaderExtensions
{
public static long? GetContentLength(this IHeaderDictionary headers)
{
long length;
var rawValue = headers[HeaderNames.ContentLength];

if (rawValue.Count == 1 &&
!string.IsNullOrWhiteSpace(rawValue[0]) &&
HeaderUtilities.TryParseInt64(rawValue[0], out length))
{
return length;
}

return null;
}

public static void SetContentLength(this IHeaderDictionary headers, long? value)
{
if (value.HasValue)
{
headers[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value);
}
else
{
headers.Remove(HeaderNames.ContentLength);
}
}
}
}
9 changes: 1 addition & 8 deletions src/Alba/HttpRequestBody.cs
@@ -1,10 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba
Expand Down Expand Up @@ -40,7 +37,7 @@ public void XmlInputIs(object target)

public void JsonInputIs(object target)
{
string json = _system.ToJson(target);
var json = _system.ToJson(target);

JsonInputIs(json);
}
Expand Down Expand Up @@ -69,10 +66,6 @@ private void writeTextToBody(string json)
public void WriteFormData(Dictionary<string, string> input)
{
_parent.Request.ContentType(MimeType.HttpFormMimetype);

//TODO: Is this the real form data length?
_parent.Request.ContentLength = input.Count;

_parent.WriteFormData(input);
}

Expand Down
1 change: 1 addition & 0 deletions src/Alba/Scenario.cs
Expand Up @@ -12,6 +12,7 @@
using Baseline;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;

namespace Alba
{
Expand Down
45 changes: 19 additions & 26 deletions src/Alba/Stubs/StubHttpRequest.cs
Expand Up @@ -4,21 +4,25 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

namespace Alba.Stubs
{
public class StubHttpRequest : HttpRequest
{
public StubHttpRequest(StubHttpContext context)
private readonly FormFeature _formFeature;

public StubHttpRequest(HttpContext context)
{
HttpContext = context;
_formFeature = new FormFeature(this);
}

public override Task<IFormCollection> ReadFormAsync(CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
return _formFeature.ReadFormAsync(cancellationToken);
}

public override HttpContext HttpContext { get; }
Expand All @@ -36,35 +40,24 @@ public override Task<IFormCollection> ReadFormAsync(CancellationToken cancellati

public override long? ContentLength
{
get
{
if (!Headers.ContainsKey("content-length"))
{
return null;
}

return long.Parse(Headers["content-length"].First());
}
set
{
Headers["content-length"] = new StringValues(value.ToString());
}
get { return Headers.GetContentLength(); }
set { Headers.SetContentLength(value); }
}

public override string ContentType
{
get
{
return Headers["content-type"].FirstOrDefault();
}
set
{
Headers["content-type"] = new StringValues(value);
}
get { return Headers[HeaderNames.ContentType]; }
set { Headers[HeaderNames.ContentType] = value; }
}

public override Stream Body { get; set; } = new MemoryStream();
public override bool HasFormContentType { get; } = false; // TODO -- do something here.
public override IFormCollection Form { get; set; }

public override bool HasFormContentType => _formFeature.HasFormContentType;

public override IFormCollection Form
{
get { return _formFeature.ReadForm(); }
set { _formFeature.Form = value; }
}
}
}
26 changes: 5 additions & 21 deletions src/Alba/Stubs/StubHttpResponse.cs
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

namespace Alba.Stubs
{
Expand Down Expand Up @@ -44,31 +45,14 @@ public override void Redirect(string location, bool permanent)

public override long? ContentLength
{
get
{
if (!Headers.ContainsKey("content-length"))
{
return null;
}

return long.Parse(Headers["content-length"].First());
}
set
{
Headers["content-length"] = new StringValues(value.ToString());
}
get { return Headers.GetContentLength(); }
set { Headers.SetContentLength(value); }
}

public override string ContentType
{
get
{
return Headers["content-type"].FirstOrDefault();
}
set
{
Headers["content-type"] = new StringValues(value);
}
get { return Headers[HeaderNames.ContentType]; }
set { Headers[HeaderNames.ContentType] = value; }
}

public override IResponseCookies Cookies { get; }
Expand Down

0 comments on commit c987f5a

Please sign in to comment.