From 5bf4883cd99255902e669b74e54f77a2ed01f4ad Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Mon, 7 Jul 2014 10:48:07 -0700 Subject: [PATCH] #91 Provide a default constructor for DefaultHttpContext. --- .../DeafultHttpRequestFeature.cs | 30 ++++----- .../DefaultHttpContext.cs | 7 ++ .../DefaultHttpResponseFeature.cs | 33 +++++++++ .../Microsoft.AspNet.PipelineCore.kproj | 2 + .../MapPathMiddlewareTests.cs | 4 +- .../MapPredicateMiddlewareTests.cs | 4 +- .../Microsoft.AspNet.Http.Tests.kproj | 3 +- .../OwinEnvironmentTests.cs | 67 +------------------ .../DefaultHttpContextTests.cs | 19 +----- .../DefaultHttpRequestTests.cs | 28 +++----- 10 files changed, 73 insertions(+), 124 deletions(-) rename test/Microsoft.AspNet.Http.Tests/Fakes.cs => src/Microsoft.AspNet.PipelineCore/DeafultHttpRequestFeature.cs (51%) create mode 100644 src/Microsoft.AspNet.PipelineCore/DefaultHttpResponseFeature.cs diff --git a/test/Microsoft.AspNet.Http.Tests/Fakes.cs b/src/Microsoft.AspNet.PipelineCore/DeafultHttpRequestFeature.cs similarity index 51% rename from test/Microsoft.AspNet.Http.Tests/Fakes.cs rename to src/Microsoft.AspNet.PipelineCore/DeafultHttpRequestFeature.cs index d33efeaa2b..bfbdf518a7 100644 --- a/test/Microsoft.AspNet.Http.Tests/Fakes.cs +++ b/src/Microsoft.AspNet.PipelineCore/DeafultHttpRequestFeature.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -6,10 +6,22 @@ using System.Collections.Generic; using System.IO; using Microsoft.AspNet.HttpFeature; -namespace Microsoft.AspNet.Builder.Extensions +namespace Microsoft.AspNet.PipelineCore { - public class FakeHttpRequestFeature : IHttpRequestFeature + public class DeafultHttpRequestFeature : IHttpRequestFeature { + public DeafultHttpRequestFeature() + { + Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + Body = Stream.Null; + Protocol = string.Empty; + Scheme = string.Empty; + Method = string.Empty; + PathBase = string.Empty; + Path = string.Empty; + QueryString = string.Empty; + } + public string Protocol { get; set; } public string Scheme { get; set; } public string Method { get; set; } @@ -19,16 +31,4 @@ namespace Microsoft.AspNet.Builder.Extensions public IDictionary Headers { get; set; } public Stream Body { get; set; } } - - public class FakeHttpResponseFeature : IHttpResponseFeature - { - public int StatusCode { get; set; } - public string ReasonPhrase { get; set; } - public IDictionary Headers { get; set; } - public Stream Body { get; set; } - public void OnSendingHeaders(Action callback, object state) - { - throw new NotImplementedException(); - } - } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs b/src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs index 9afbc4368c..af1f15e790 100644 --- a/src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs +++ b/src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs @@ -33,6 +33,13 @@ namespace Microsoft.AspNet.PipelineCore private FeatureReference _webSockets; private IFeatureCollection _features; + public DefaultHttpContext() + : this(new FeatureCollection()) + { + SetFeature(new DeafultHttpRequestFeature()); + SetFeature(new DefaultHttpResponseFeature()); + } + public DefaultHttpContext(IFeatureCollection features) { _features = features; diff --git a/src/Microsoft.AspNet.PipelineCore/DefaultHttpResponseFeature.cs b/src/Microsoft.AspNet.PipelineCore/DefaultHttpResponseFeature.cs new file mode 100644 index 0000000000..819ff17421 --- /dev/null +++ b/src/Microsoft.AspNet.PipelineCore/DefaultHttpResponseFeature.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.AspNet.HttpFeature; + +namespace Microsoft.AspNet.PipelineCore +{ + public class DefaultHttpResponseFeature : IHttpResponseFeature + { + public DefaultHttpResponseFeature() + { + StatusCode = 200; + Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + Body = Stream.Null; + } + + public int StatusCode { get; set; } + + public string ReasonPhrase { get; set; } + + public IDictionary Headers { get; set; } + + public Stream Body { get; set; } + + public void OnSendingHeaders(Action callback, object state) + { + throw new NotSupportedException(); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.PipelineCore/Microsoft.AspNet.PipelineCore.kproj b/src/Microsoft.AspNet.PipelineCore/Microsoft.AspNet.PipelineCore.kproj index 05dd7de5dd..054abcaa32 100644 --- a/src/Microsoft.AspNet.PipelineCore/Microsoft.AspNet.PipelineCore.kproj +++ b/src/Microsoft.AspNet.PipelineCore/Microsoft.AspNet.PipelineCore.kproj @@ -27,6 +27,8 @@ + + diff --git a/test/Microsoft.AspNet.Http.Tests/MapPathMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Tests/MapPathMiddlewareTests.cs index 3bca69be56..4a9d68dd45 100644 --- a/test/Microsoft.AspNet.Http.Tests/MapPathMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Tests/MapPathMiddlewareTests.cs @@ -187,9 +187,7 @@ namespace Microsoft.AspNet.Builder.Extensions private HttpContext CreateRequest(string basePath, string requestPath) { - HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection()); - context.SetFeature(new FakeHttpRequestFeature()); - context.SetFeature(new FakeHttpResponseFeature()); + HttpContext context = new DefaultHttpContext(); context.Request.PathBase = new PathString(basePath); context.Request.Path = new PathString(requestPath); return context; diff --git a/test/Microsoft.AspNet.Http.Tests/MapPredicateMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Tests/MapPredicateMiddlewareTests.cs index 17cce7c777..eb8ee8be9c 100644 --- a/test/Microsoft.AspNet.Http.Tests/MapPredicateMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Tests/MapPredicateMiddlewareTests.cs @@ -176,9 +176,7 @@ namespace Microsoft.AspNet.Builder.Extensions private HttpContext CreateRequest() { - HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection()); - context.SetFeature(new FakeHttpRequestFeature()); - context.SetFeature(new FakeHttpResponseFeature()); + HttpContext context = new DefaultHttpContext(); return context; } } diff --git a/test/Microsoft.AspNet.Http.Tests/Microsoft.AspNet.Http.Tests.kproj b/test/Microsoft.AspNet.Http.Tests/Microsoft.AspNet.Http.Tests.kproj index 883a9d895c..f8e3963d7c 100644 --- a/test/Microsoft.AspNet.Http.Tests/Microsoft.AspNet.Http.Tests.kproj +++ b/test/Microsoft.AspNet.Http.Tests/Microsoft.AspNet.Http.Tests.kproj @@ -21,10 +21,9 @@ - - + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs b/test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs index d7ce8ae590..cdc5eb680f 100644 --- a/test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs +++ b/test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs @@ -1,16 +1,11 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Claims; -using System.Threading; -using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.Http; -using Microsoft.AspNet.HttpFeature; -using Microsoft.AspNet.HttpFeature.Security; using Microsoft.AspNet.PipelineCore; using Xunit; @@ -101,66 +96,8 @@ namespace Microsoft.AspNet.Owin private HttpContext CreateContext() { - var features = new FeatureCollection(); - features.Add(typeof(IHttpRequestFeature), new MoqHttpRequestFeature()); - features.Add(typeof(IHttpResponseFeature), new MoqHttpResponseFeature()); - features.Add(typeof(IHttpRequestLifetimeFeature), new MoqHttpRequestLifetimeFeature()); - return new DefaultHttpContext(features); - } - - private class MoqHttpRequestFeature : IHttpRequestFeature - { - public MoqHttpRequestFeature() - { - Headers = new Dictionary(); - } - - public string Method { get; set; } - - public string Scheme { get; set; } - - public string Protocol { get; set; } - - public Stream Body { get; set; } - - public string PathBase { get; set; } - - public string Path { get; set; } - - public string QueryString { get; set; } - - public IDictionary Headers { get; set; } - } - - private class MoqHttpResponseFeature : IHttpResponseFeature - { - public MoqHttpResponseFeature() - { - Headers = new Dictionary(); - } - - public Stream Body { get; set; } - - public int StatusCode { get; set; } - - public string ReasonPhrase { get; set; } - - public IDictionary Headers { get; set; } - - public void OnSendingHeaders(Action callback, object state) - { - throw new NotImplementedException(); - } - } - - private class MoqHttpRequestLifetimeFeature : IHttpRequestLifetimeFeature - { - public CancellationToken RequestAborted { get; private set; } - - public void Abort() - { - throw new NotImplementedException(); - } + var context = new DefaultHttpContext(); + return context; } } } diff --git a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs index 6ece4245fa..723811a947 100644 --- a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs +++ b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs @@ -80,25 +80,8 @@ namespace Microsoft.AspNet.PipelineCore.Tests private HttpContext CreateContext() { - var context = new DefaultHttpContext(new FeatureCollection()); - context.SetFeature(new FakeHttpResponse()); + var context = new DefaultHttpContext(); return context; } - - private class FakeHttpResponse : IHttpResponseFeature - { - public int StatusCode { get; set; } - - public string ReasonPhrase { get; set; } - - public IDictionary Headers { get; set; } - - public Stream Body { get; set; } - - public void OnSendingHeaders(Action callback, object state) - { - throw new NotImplementedException(); - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs index 93d816e875..baa1c6c4ed 100644 --- a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs +++ b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs @@ -5,9 +5,7 @@ using System; using System.Collections.Generic; using System.Globalization; using Microsoft.AspNet.Http; -using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.HttpFeature; -using Moq; using Xunit; namespace Microsoft.AspNet.PipelineCore.Tests @@ -57,9 +55,9 @@ namespace Microsoft.AspNet.PipelineCore.Tests // Arrange const string expected = "localhost:9001"; - var headers = new Dictionary(StringComparer.Ordinal) + var headers = new Dictionary(StringComparer.OrdinalIgnoreCase) { - { "Host", new string[]{ expected } }, + { "Host", new string[] { expected } }, }; var request = CreateRequest(headers); @@ -77,7 +75,7 @@ namespace Microsoft.AspNet.PipelineCore.Tests // Arrange const string expected = "löcalhöst"; - var headers = new Dictionary(StringComparer.Ordinal) + var headers = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Host", new string[]{ "xn--lcalhst-90ae" } }, }; @@ -97,7 +95,7 @@ namespace Microsoft.AspNet.PipelineCore.Tests // Arrange const string expected = "xn--lcalhst-90ae"; - var headers = new Dictionary(StringComparer.Ordinal); + var headers = new Dictionary(StringComparer.OrdinalIgnoreCase); var request = CreateRequest(headers); @@ -108,25 +106,19 @@ namespace Microsoft.AspNet.PipelineCore.Tests Assert.Equal(expected, headers["Host"][0]); } - private static DefaultHttpRequest CreateRequest(IDictionary headers) + private static HttpRequest CreateRequest(IDictionary headers) { - var requestInfo = new Mock(); - requestInfo.SetupGet(r => r.Headers).Returns(headers); - - var features = new FeatureCollection(); - features.Add(typeof(IHttpRequestFeature), requestInfo.Object); - - var context = new DefaultHttpContext(features); - return new DefaultHttpRequest(context, features); + var context = new DefaultHttpContext(); + context.GetFeature().Headers = headers; + return context.Request; } - private static DefaultHttpRequest GetRequestWithContentLength(string contentLength = null) + private static HttpRequest GetRequestWithContentLength(string contentLength = null) { - var headers = new Dictionary(StringComparer.Ordinal); + var headers = new Dictionary(StringComparer.OrdinalIgnoreCase); if (contentLength != null) { headers.Add("Content-Length", new[] { contentLength }); - } return CreateRequest(headers);