Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ public sealed partial class HttpCookieCollection : System.Collections.Specialize
public partial class HttpException : System.SystemException
{
public HttpException() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(int httpStatusCode) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(int httpStatusCode, string message) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(int httpStatusCode, string message, System.Exception innerException) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(System.Net.HttpStatusCode httpStatusCode) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(System.Net.HttpStatusCode httpStatusCode, string message) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(System.Net.HttpStatusCode httpStatusCode, string message, System.Exception innerException) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(string message) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public HttpException(string message, System.Exception innerException) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public int StatusCode { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public int GetHttpCode() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public sealed partial class HttpFileCollection : System.Collections.Specialized.NameObjectCollectionBase
{
Expand Down
52 changes: 52 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// 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.Net;

namespace System.Web;

public class HttpException : SystemException
{
private readonly int httpStatusCode = 500;

public HttpException()
{
}
Expand All @@ -17,4 +22,51 @@ public HttpException(string message, Exception innerException)
: base(message, innerException)
{
}

public HttpException(int httpStatusCode)
{
this.httpStatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode)
{
this.httpStatusCode = (int)httpStatusCode;
}

public HttpException(int httpStatusCode, string message)
: base(message)
{
this.httpStatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode, string message)
: base(message)
{
this.httpStatusCode = (int)httpStatusCode;
}

public HttpException(int httpStatusCode, string message, Exception innerException)
: base(message, innerException)
{
this.httpStatusCode = httpStatusCode;
}

public HttpException(HttpStatusCode httpStatusCode, string message, Exception innerException)
: base(message, innerException)
{
this.httpStatusCode = (int)httpStatusCode;
}

public int GetHttpCode()
{
return this.httpStatusCode;
}

public int StatusCode
{
get
{
return this.httpStatusCode;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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.Net;
using System.Web;
using AutoFixture;
using Xunit;

namespace Microsoft.AspNetCore.SystemWebAdapters;

public class HttpExceptionTests
{
[Fact]
public void DefaultValues()
{
// Act
var exception = new HttpException();

// Assert
Assert.Equal(500, exception.GetHttpCode());
}

[Fact]
public void Create()
{
// Arrange
var message = "message";

// Act
var exception = new HttpException(message);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal(500, exception.GetHttpCode());
}

[Fact]
public void CreateWithInnerException()
{
// Arrange
var message = "message";
var innerException = new Exception();

// Act
var exception = new HttpException(message, innerException);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal(innerException, exception.InnerException);
Assert.Equal(500, exception.GetHttpCode());
}

[Fact]
public void CreateWithHttpStatusCode()
{
// Arrange
var httpStatusCode = 404;

// Act
var exception = new HttpException(httpStatusCode);

// Assert
Assert.Equal(httpStatusCode, exception.GetHttpCode());
}

[Fact]
public void CreateWithHttpStatusCodeEnum()
{
// Arrange
var httpStatusCode = HttpStatusCode.NotFound;

// Act
var exception = new HttpException(httpStatusCode);

// Assert
Assert.Equal((int)httpStatusCode, exception.GetHttpCode());
}

[Fact]
public void CreateWithHttpStatusCodeAndMessage()
{
// Arrange
var httpStatusCode = 404;
var message = "message";

// Act
var exception = new HttpException(httpStatusCode, message);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal(httpStatusCode, exception.GetHttpCode());
}

[Fact]
public void CreateWithHttpStatusCodeEnumAndMessage()
{
// Arrange
var httpStatusCode = HttpStatusCode.NotFound;
var message = "message";

// Act
var exception = new HttpException(httpStatusCode, message);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal((int)httpStatusCode, exception.GetHttpCode());
}

[Fact]
public void CreateWithIntCodeAndMessageAndInnerException()
{
// Arrange
var httpStatusCode = 404;
var message = "message";
var innerException = new Exception();

// Act
var exception = new HttpException(httpStatusCode, message, innerException);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal(httpStatusCode, exception.GetHttpCode());
}

[Fact]
public void CreateWithEnumHttpStatusCodeAndMessageAndInnerException()
{
// Arrange
var httpStatusCode = HttpStatusCode.NotFound;
var message = "message";
var innerException = new Exception();

// Act
var exception = new HttpException(httpStatusCode, message, innerException);

// Assert
Assert.Equal(message, exception.Message);
Assert.Equal((int)httpStatusCode, exception.GetHttpCode());
}
}