0

我正在使用 NUnit 和 NSubstitute 对使用 HttpResponse 的函数进行一些单元测试,我知道您无法模拟这些对象,因此我创建了接口来表示它们和其中的一些属性。

我无法理解如何为 Response.Cache.VaryByHeader 创建接口

// This is my HttpResponse interface 
public interface IHttpResponse
{
    Stream Filter { get ; set; }
    IHttpCachePolicy Cache { get; set; }
    void AppendHeader(string name, string value);
}   

// concrete httresponse 
public class HttpResponseProxy : IHttpResponse
{
    private HttpResponse _httpResponse;

    public Stream Filter {
        get {
            return _httpResponse.Filter ?? new MemoryStream();
        }
        set { _httpResponse.Filter = value; }
    }

    public IHttpCachePolicy Cache
    {
        get { return new HttpCachePolicyProxy(_httpResponse.Cache); }
        set { }
    }

    public HttpResponseProxy(HttpResponse httpResponse)
    {
        if (httpResponse == null)
        {
            throw new ArgumentNullException("httpResponse");
        }

        _httpResponse = httpResponse;
        _httpResponse.Filter = httpResponse.Filter;
    }

    public void AppendHeader(string name, string value)
    {
        _httpResponse.AppendHeader(name, value);
    }
}    

// HttpCachePolicy interface 
public interface IHttpCachePolicy
{
    IHttpCacheVaryByHeaders VaryByHeaders { get; set; }
}

// concrete HttpCachePolicy
public class HttpCachePolicyProxy : IHttpCachePolicy
{
    private HttpCachePolicy _httpCachePolicy;
    public HttpCachePolicyProxy(HttpCachePolicy httpCachePolicy)
    {
        _httpCachePolicy = httpCachePolicy;
    }

    public IHttpCacheVaryByHeaders VaryByHeaders
    {
        get { return new HttpCacheVaryByHeadersProxy(_httpCachePolicy.VaryByHeaders as HttpCacheVaryByHeaders); }
        set {  }
    }
}


public interface IHttpCacheVaryByHeaders
{
    IHttpCacheVaryByHeaders HttpCacheVaryByHeaders { get; set; } 
}

public class HttpCacheVaryByHeadersProxy : IHttpCacheVaryByHeaders
{
    private HttpCacheVaryByHeaders _httpCacheVaryByHeaders;
    public HttpCacheVaryByHeadersProxy(HttpCacheVaryByHeaders httpCacheVaryByHeaders)
    {
        _httpCacheVaryByHeaders = httpCacheVaryByHeaders;
    }

    public IHttpCacheVaryByHeaders HttpCacheVaryByHeaders
    {
        get { return new HttpCacheVaryByHeadersProxy(_httpCacheVaryByHeaders); }
        set {  }
    }
}

这是我实际测试的功能:

public static void CompressPage(IHttpRequestGetCompressionMode getCompressionMode, IHttpResponse httpResponse)
    {
        string supportedCompression = getCompressionMode.GetClientSupportedCompressionMode();
        if (supportedCompression != HttpHeaderValues.NoCompression)
        {
            switch (supportedCompression)
            {
                case HttpHeaderValues.DeflateCompression:
                    httpResponse.Filter = new DeflateStream(httpResponse.Filter, CompressionMode.Compress);
                    break;
                case HttpHeaderValues.GZipCompression:
                    httpResponse.Filter = new GZipStream(httpResponse.Filter, CompressionMode.Compress);
                    break;
            }

            httpResponse.AppendHeader(HttpHeaderValues.ContentEncodingHeader, supportedCompression);
            // this line is where i have the problem
            httpResponse.Cache.VaryByHeaders[HttpHeaderValues.AcceptEncodingHeader] = true;
        }
    }

我收到“无法将索引应用于 IHttpCacheVaryByHeaders 类型的表达式”错误。我有响应和缓存的接口,但是如何在接口中表示 VaryByHeaders,然后在具体类中使用它?

4

1 回答 1

0

The error seems to suggest that IHttpCacheVaryByHeaders does not have an indexer declared (e.g. bool this[string header] { get; set; }), but rather than implementing these wrappers yourself, try the HttpResponseWrapper and other System.Web.Abstractions. This will should make testing this stuff a lot easier. :)

于 2011-08-21T12:38:31.747 回答