Nunit测试Response.Cache.VaryByHeader

时间:2011-08-19 17:00:02

标签: asp.net nunit httpresponse nsubstitute

我正在使用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然后在一个具体的类中使用它?

1 个答案:

答案 0 :(得分:0)

错误似乎表明IHttpCacheVaryByHeaders没有声明索引器(例如bool this[string header] { get; set; }),但是不是自己实现这些包装器,请尝试HttpResponseWrapper和其他{{3} }。这将使测试这些东西更容易。 :)