0

I am doing some Unit testing with NUnit and NSubstitute on a function that uses HttpResponse, I know you can't mock these objects so I have created interfaces to represent them and some of there properties.

I'm having trouble understanding how to create an interface for 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 {  }
    }
}

This is the function i am actually testing:

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;
        }
    }

I'm getting "cannot apply index to an expression of type IHttpCacheVaryByHeaders" errors. I have the interface for the response and cache but how do I represent VaryByHeaders in an interface and then use it in a concrete class?

Dave
  • 3,812
  • 5
  • 31
  • 39

1 Answers1

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. :)

Community
  • 1
  • 1
David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • Hi David thank you very much, I'm using bool this[string header] { get; set; } to fix my tests for now but will look at the Abstractions when I get a chance, I'm not sure how bool this[string header] { get; set; } works but I'll look into it. Cheers. – Dave Aug 22 '11 at 12:05