Skip to content

Commit

Permalink
Ensure *Base types have the same members as the main types (#498)
Browse files Browse the repository at this point in the history
System.Web had *Base types that were meant for abstraction. This adds a test to ensure we have the same members on both versions and adds a few that were missing.
  • Loading branch information
twsouthwick authored Apr 10, 2024
1 parent aac9078 commit 58bc014
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Microsoft.AspNetCore.SystemWebAdapters.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"src\\Microsoft.AspNetCore.SystemWebAdapters.CoreServices\\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.csproj",
"src\\Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices\\Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices.csproj",
"src\\Microsoft.AspNetCore.SystemWebAdapters\\Microsoft.AspNetCore.SystemWebAdapters.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.Apis.Tests\\Microsoft.AspNetCore.SystemWebAdapters.Apis.Tests.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests\\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices.Tests\\Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices.Tests.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.Tests\\Microsoft.AspNetCore.SystemWebAdapters.Tests.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.NuGet.Tests\\Microsoft.AspNetCore.SystemWebAdapters.NuGet.Tests.csproj",
"test\\Microsoft.AspNetCore.SystemWebAdapters.Tests\\Microsoft.AspNetCore.SystemWebAdapters.Tests.csproj",
"test\\Samples.MVCApp.Tests\\Samples.MVCApp.Tests.csproj",
"test\\Samples.RemoteAuth.Forms.Tests\\Samples.RemoteAuth.Forms.Tests.csproj"
]
}
}
}

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protected HttpContextBase()
[SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = Constants.ApiFromAspNet)]
public virtual Exception[] AllErrors => throw new NotImplementedException();

public virtual TraceContext Trace => throw new NotImplementedException();

public virtual ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target) => throw new NotImplementedException();

public virtual void ClearError() => throw new NotImplementedException();

public virtual void AddError(Exception ex) => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public override IHttpHandler? Handler
set => _context.Handler = value;
}

public override ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target) => _context.DisposeOnPipelineCompleted(target);

public override TraceContext Trace => _context.Trace;

public override IHttpHandler? PreviousHandler => _context.PreviousHandler;

public override void RemapHandler(IHttpHandler handler) => _context.RemapHandler(handler);
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ public abstract class HttpRequestBase

public virtual string Path => throw new NotImplementedException();

public virtual string PathInfo => throw new NotImplementedException();

public virtual string CurrentExecutionFilePath => throw new NotImplementedException();

public virtual string? PhysicalPath => throw new NotImplementedException();

public virtual string? FilePath => throw new NotImplementedException();

public virtual ReadEntityBodyMode ReadEntityBodyMode => throw new NotImplementedException();

public virtual void SaveAs(string filename, bool includeHeaders) => throw new NotImplementedException();

public virtual NameValueCollection Headers => throw new NotImplementedException();

public virtual Uri Url => throw new NotImplementedException();
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequestWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ public override string? ContentType

public override string Path => _request.Path;

public override string CurrentExecutionFilePath => _request.CurrentExecutionFilePath;

public override string PathInfo => _request.PathInfo;

public override string? PhysicalPath => _request.PhysicalPath;

public override string? FilePath => _request.FilePath;

public override NameValueCollection QueryString => _request.QueryString;

public override HttpBrowserCapabilitiesBase Browser => new HttpBrowserCapabilitiesWrapper(_request.Browser);
Expand Down Expand Up @@ -93,6 +99,10 @@ public override string? ContentType

public override NameValueCollection Params => _request.Params;

public override ReadEntityBodyMode ReadEntityBodyMode => _request.ReadEntityBodyMode;

public override void SaveAs(string filename, bool includeHeaders) => _request.SaveAs(filename, includeHeaders);

public override string? this[string key] => _request[key];
}
}
13 changes: 12 additions & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SystemWebAdapters;

namespace System.Web
Expand Down Expand Up @@ -63,7 +64,7 @@ public virtual Encoding ContentEncoding
set => throw new NotImplementedException();
}

public virtual bool BufferOutput => throw new NotImplementedException();
public virtual bool BufferOutput => throw new NotImplementedException();

public virtual Stream OutputStream => throw new NotImplementedException();

Expand Down Expand Up @@ -123,6 +124,10 @@ public virtual Stream Filter

public virtual void ClearHeaders() => throw new NotImplementedException();

public virtual void Flush() => throw new NotImplementedException();

public virtual Task FlushAsync() => throw new NotImplementedException();

public virtual void WriteFile(string filename) => throw new NotImplementedException();

public virtual void TransmitFile(string filename) => throw new NotImplementedException();
Expand All @@ -142,6 +147,12 @@ public virtual Stream Filter
[SuppressMessage("Design", "CA1054:URI parameters should not be strings", Justification = Constants.ApiFromAspNet)]
public virtual void RedirectPermanent(string url, bool endResponse) => throw new NotImplementedException();

public virtual string? RedirectLocation
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

[return: NotNullIfNotNull(nameof(response))]
public static implicit operator HttpResponseBase?(HttpResponseCore? response) => response?.HttpContext.AsSystemWebBase().Response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace System.Web
{
Expand Down Expand Up @@ -122,6 +122,10 @@ public override Stream Filter

public override void ClearHeaders() => _response.ClearHeaders();

public override void Flush() => _response.Flush();

public override Task FlushAsync() => _response.FlushAsync();

public override void End() => _response.End();

public override void TransmitFile(string filename) => _response.TransmitFile(filename);
Expand All @@ -143,5 +147,10 @@ public override Stream Filter
[SuppressMessage("Design", "CA1054:URI parameters should not be strings", Justification = Constants.ApiFromAspNet)]
public override void RedirectPermanent(string url, bool endResponse) => _response.RedirectPermanent(url, endResponse);

public override string? RedirectLocation
{
get => _response.RedirectLocation;
set => _response.RedirectLocation = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.AspNetCore.SystemWebAdapters;

internal class TypeCollector : SymbolVisitor
internal sealed class TypeCollector : SymbolVisitor
{
private HashSet<string> Members { get; } = new HashSet<string>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.AspNetCore.SystemWebAdapters;

/// <summary>
/// This test validates that the *Base type (the abstraction in System.Web) has the same members as on the non-base type. For example, <see cref="HttpContext"/> and <see cref="HttpContextBase"/>.
/// This test is a best effort to ensure when an API is added to the main type, the base type gets it as well
/// </summary>
public class VerifyHttpBaseTypes
{
private static readonly HashSet<string> _skipped = new HashSet<string>()
{
"op_Implicit", // This is only defined on the non-base types as there is a well defined conversion
};

private readonly ITestOutputHelper _output;

public VerifyHttpBaseTypes(ITestOutputHelper output)
{
_output = output;
}

[InlineData(typeof(HttpContext), typeof(HttpContextBase))]
[InlineData(typeof(HttpRequest), typeof(HttpRequestBase))]
[InlineData(typeof(HttpResponse), typeof(HttpResponseBase))]
[Theory]
public void ValidateMemberExistsOnBaseType(Type type, Type baseType)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(baseType);

const BindingFlags Flags = BindingFlags.Public | BindingFlags.Instance;

var isMissing = false;

foreach (var method in type.GetMethods(Flags))
{
if (_skipped.Contains(method.Name))
{
continue;
}

var found = baseType.GetMethod(method.Name, Flags, method.GetParameters().Select(p => p.ParameterType).ToArray());

if (found is null)
{
_output.WriteLine(method.Name);
isMissing = true;
}
}

Assert.False(isMissing);
}
}

0 comments on commit 58bc014

Please sign in to comment.