用于单元测试的模拟 SSL HttpRequest

发布于 2024-07-23 17:26:11 字数 1376 浏览 1 评论 0原文

我正在尝试模拟 SSL HttpRequest,但我无法弄清楚如何在请求对象中将协议设置为 HTTPS。 我从 Phil Haack 的一个例子开始: http://haacked.com/archive/2005/06/11/simulated_httpcontext。 aspx

有没有办法将请求设置为SSL?

public class MockHttpRequest : SimpleWorkerRequest
{
    private string _Host;

    public MockHttpRequest(
        string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output, string host) :
        base(appVirtualDir, appPhysicalDir, page, query, output)
    {
        if (string.IsNullOrEmpty(host))
        {
            throw new ArgumentException("Host must be provided.");
        }

        _Host = host;
    }
}

public static class UnitTestingHelper
{
    public static HttpContext CreateMockHttpContext(string host, string page)
    {
        string appVirtualDir = "/";
        string appPhysicalDir = @"C:\Documents and Settings\user\My Documents\Workspace\Project\";
        string query = string.Empty;
        TextWriter output = null;

        MockHttpRequest request
            = new MockHttpRequest(appVirtualDir, appPhysicalDir, "default.aspx", query, output, host);

        // How to make the request HTTPS?

        HttpContext context = new HttpContext(request);

        return new HttpContext(request);
    }
}

I'm trying to mock out an SSL HttpRequest but I'm having trouble figuring out how to set the protocol to HTTPS in the request object. I got started from an example from Phil Haack here:
http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx

Is there a way to set the request to SSL?

public class MockHttpRequest : SimpleWorkerRequest
{
    private string _Host;

    public MockHttpRequest(
        string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output, string host) :
        base(appVirtualDir, appPhysicalDir, page, query, output)
    {
        if (string.IsNullOrEmpty(host))
        {
            throw new ArgumentException("Host must be provided.");
        }

        _Host = host;
    }
}

public static class UnitTestingHelper
{
    public static HttpContext CreateMockHttpContext(string host, string page)
    {
        string appVirtualDir = "/";
        string appPhysicalDir = @"C:\Documents and Settings\user\My Documents\Workspace\Project\";
        string query = string.Empty;
        TextWriter output = null;

        MockHttpRequest request
            = new MockHttpRequest(appVirtualDir, appPhysicalDir, "default.aspx", query, output, host);

        // How to make the request HTTPS?

        HttpContext context = new HttpContext(request);

        return new HttpContext(request);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-07-30 17:26:11

我认为 HttpContext.Request 中的某处有一个 IsSecureConnection 属性需要为 true。

I think there's a IsSecureConnection property somewhere in HttpContext.Request that needs to be true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文