public void WhenCombinationContainsNoContent_ShouldNotThrow()
        {
            _stubHttpContext.Expect(hc => hc.Response).Return(_stubResponse);
            _stubHttpContext.Expect(hc => hc.Request).Return(_stubRequest);
            _stubRequest.Expect(r => r.Headers).Return(new NameValueCollection { { HttpHeaders.AcceptEncoding, "" } });
            _stubResponse.ContentEncoding = Encoding.UTF8;
            _stubResponse.ContentType = MimeTypes.TextCss;
            _stubResponse.AddHeader(HttpHeaders.ContentLength, "15");
            _stubResponse.Expect(r => r.OutputStream).Return(new MemoryStream(8092)).Repeat.Twice();
            _stubResponse.Expect(r => r.Cache).Return(_stubCache);

            var emptyCombination = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "", DateTime.UtcNow, new CssTypeElement());
            _stubCombiner.Expect(c => c.GetCombination("foo")).Return(emptyCombination);
            var result = new IncludeCombinationResult(_stubCombiner, "foo", DateTime.UtcNow);
            result.ExecuteResult(_controllerContext);
        }
 public void WhenConstructedViaCombinerAndKey_CombinationIsSet()
 {
     var mockCombiner = _mocks.StrictMock<IIncludeCombiner>();
     _mocks.ReplayAll();
     mockCombiner.Expect(c => c.GetCombination("foo")).Return(_cssCombination);
     IncludeCombinationResult result = new IncludeCombinationResult(mockCombiner, "foo", DateTime.UtcNow);
     Assert.AreEqual(_cssCombination, result.Combination);
 }
        public void WhenCacheForIsSet_ShouldAppendCacheHeaders()
        {
            var lastModifiedAt = DateTime.UtcNow;
            var combination = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#Foo{color:red;}", lastModifiedAt, new CssTypeElement());
            _mockHttpContext.Expect(hc => hc.Response).Return(_mockResponse);
            _mockHttpContext.Expect(hc => hc.Request).Return(_mockRequest);
            _mockRequest.Expect(r => r.Headers[HttpHeaders.AcceptEncoding]).Return("");
            _mockResponse.Expect(r => r.ContentEncoding = Encoding.UTF8);
            _mockResponse.Expect(r => r.ContentType = MimeTypes.TextCss);
            _mockResponse.Expect(r => r.AddHeader(HttpHeaders.ContentLength, "16"));
            _mockResponse.Expect(r => r.OutputStream).Return(new MemoryStream(8092)).Repeat.Twice();
            _mockResponse.Expect(r => r.Cache).Return(_mockCachePolicy);
            _mockCachePolicy.Expect(cp => cp.SetETag(Arg<string>.Matches(etag => etag.StartsWith("foo") && etag.EndsWith(combination.LastModifiedAt.Ticks.ToString()))));
            _stubCombiner.Expect(c => c.GetCombination("foo")).Return(combination);

            var cacheFor = TimeSpan.FromMinutes(30);
            _mockCachePolicy.Expect(cp => cp.SetCacheability(HttpCacheability.Public));
            _mockCachePolicy.Expect(cp => cp.SetExpires(lastModifiedAt.Add(cacheFor)));
            _mockCachePolicy.Expect(cp => cp.SetMaxAge(cacheFor));
            _mockCachePolicy.Expect(cp => cp.SetValidUntilExpires(true));
            _mockCachePolicy.Expect(cp => cp.SetLastModified(lastModifiedAt));
            var stubSettings = _mocks.Stub<IIncludeHandlingSettings>();
            var stubCss = _mocks.Stub<IIncludeTypeSettings>();
            stubSettings.Replay();
            stubCss.Replay();
            stubSettings.Expect(s => s.Types[IncludeType.Css]).Return(stubCss);
            stubCss.Expect(s => s.CacheFor).Return(cacheFor);

            var result = new IncludeCombinationResult(_stubCombiner, "foo", lastModifiedAt, stubSettings);
            result.ExecuteResult(_controllerContext);

            _mocks.VerifyAll();
        }
        public void WhenRequestAcceptsCompression_ShouldAppendContentEncodingHeader(Req data)
        {
            var lastModifiedAt = DateTime.UtcNow;
            _mockHttpContext.Expect(hc => hc.Response).Return(_mockResponse);
            _mockHttpContext.Expect(hc => hc.Request).Return(_mockRequest);
            _mockRequest.Expect(r => r.Headers[HttpHeaders.AcceptEncoding]).Return(data.AcceptEncoding);
            var stubBrowser = MockRepository.GenerateStub<HttpBrowserCapabilitiesBase>();
            stubBrowser.Expect(b => b.Type).Return(data.Browser);
            stubBrowser.Expect(b => b.MajorVersion).Return(data.BrowserMajorVersion);
            stubBrowser.Replay();
            _mockRequest.Expect(r => r.Browser).Return(stubBrowser);
            _mockResponse.Expect(r => r.ContentEncoding = Encoding.UTF8);
            _mockResponse.Expect(r => r.ContentType = MimeTypes.TextCss);
            _mockResponse.Expect(r => r.AddHeader(Arg<string>.Is.Equal(HttpHeaders.ContentLength), Arg<string>.Is.NotNull));
            _mockResponse.Expect(r => r.OutputStream).Return(new MemoryStream(8092)).Repeat.Twice();
            _mockResponse.Expect(r => r.Cache).Return(_mockCachePolicy);
            if (data.ExpectedContentEncoding != null)
            {
                _mockResponse.Expect(r => r.AppendHeader(HttpHeaders.ContentEncoding, data.ExpectedContentEncoding));
            }
            _mockCachePolicy.Expect(cp => cp.SetETag(Arg<string>.Matches(etag => etag.StartsWith("foo") && etag.EndsWith(_cssCombination.LastModifiedAt.Ticks.ToString()))));
            var cacheFor = TimeSpan.FromMinutes(30);
            _mockCachePolicy.Expect(cp => cp.SetCacheability(HttpCacheability.Public));
            _mockCachePolicy.Expect(cp => cp.SetExpires(DateTime.UtcNow.Add(cacheFor))).IgnoreArguments();
            _mockCachePolicy.Expect(cp => cp.SetMaxAge(cacheFor)).IgnoreArguments();
            _mockCachePolicy.Expect(cp => cp.SetValidUntilExpires(true));
            _mockCachePolicy.Expect(cp => cp.SetLastModified(lastModifiedAt)).IgnoreArguments();
            _stubCombiner.Expect(c => c.GetCombination("foo")).Return(_cssCombination);

            var stubSettings = _mocks.Stub<IIncludeHandlingSettings>();
            stubSettings.Expect(x => x.Types[IncludeType.Css]).Return(new CssTypeElement());
            stubSettings.Replay();
            var result = new IncludeCombinationResult(_stubCombiner, "foo", DateTime.UtcNow, stubSettings);
            result.ExecuteResult(_controllerContext);

            _mocks.VerifyAll();
        }
        public void WhenNoCombinationExists_ResponseCodeShouldBe404()
        {
            _mockHttpContext.Expect(hc => hc.Response).Return(_mockResponse);
            _mockResponse.Expect(r => r.ContentEncoding = Encoding.UTF8);
            _mockResponse.Expect(r => r.StatusCode = (int) HttpStatusCode.NotFound);
            _stubCombiner.Expect(c => c.GetCombination("foo")).Return(null);

            var result = new IncludeCombinationResult(_stubCombiner, "foo", DateTime.UtcNow);
            result.ExecuteResult(_controllerContext);

            _mocks.VerifyAll();
        }
        public void WhenCombinationExists_ShouldCorrectlySetUpResponse()
        {
            _mockHttpContext.Expect(hc => hc.Response).Return(_mockResponse);
            _mockHttpContext.Expect(hc => hc.Request).Return(_mockRequest);
            _mockRequest.Expect(r => r.Headers[HttpHeaders.AcceptEncoding]).Return("");
            _mockResponse.Expect(r => r.ContentEncoding = Encoding.UTF8);
            _mockResponse.Expect(r => r.ContentType = MimeTypes.TextCss);
            _mockResponse.Expect(r => r.AddHeader(HttpHeaders.ContentLength, "16"));
            _mockResponse.Expect(r => r.OutputStream).Return(new MemoryStream(8092)).Repeat.Twice();
            _mockResponse.Expect(r => r.Cache).Return(_mockCachePolicy);
            _mockCachePolicy.Expect(cp => cp.SetETag(Arg<string>.Matches(etag => etag.StartsWith("foo") && etag.EndsWith(_cssCombination.LastModifiedAt.Ticks.ToString()))));
            _stubCombiner.Expect(c => c.GetCombination("foo")).Return(_cssCombination);

            var result = new IncludeCombinationResult(_stubCombiner, "foo", DateTime.UtcNow);
            result.ExecuteResult(_controllerContext);

            _mocks.VerifyAll();
        }