public void Equals_CorrectlyHandlesComparisonToDifferentType() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo');", DateTime.UtcNow, new JsTypeElement()); var result = combination.Equals(new string[1]); Assert.IsFalse(result); }
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 GetResponseBodyBytes_CorrectlyCompressesCombination(ResponseBodyBytes responseBodyBytes) { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, responseBodyBytes.Content, DateTime.UtcNow, new JsTypeElement()); byte[] result = combination.Bytes[responseBodyBytes.Compression]; Assert.AreEqual(responseBodyBytes.ExpectedBytes, result); }
public void Equals_CorrectlyHandlesNullObject() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo');", DateTime.UtcNow, new JsTypeElement()); var result = combination.Equals((object)null); Assert.IsFalse(result); }
public void GetCombination_WhenCombinationExists_DoesNotThrow() { _stubKeyGen.Expect(kg => kg.Generate(_combination.Sources)).Return("foo"); var key = _storage.Store(_combination); IncludeCombination result = _storage.GetCombination(key); Assert.AreEqual(_combination.Content, result.Content); }
public void TestSetup() { _mocks = new MockRepository(); _stubKeyGen = _mocks.Stub <IKeyGenerator>(); _storage = new StaticIncludeStorage(_stubKeyGen); _combination = new IncludeCombination(IncludeType.Css, new[] { "~/content/css/foo.css" }, "#foo {color:red}", DateTime.UtcNow, new CssTypeElement()); _mocks.ReplayAll(); }
public void GetCombination_ShouldAskStorageForCombination() { _mockStorage.Expect(s => s.GetCombination("foo")).Return(new IncludeCombination(IncludeType.Css, new[] { "~/content/css/foo.css" }, ".foo{}", DateTime.UtcNow, new CssTypeElement())); IncludeCombination combination = _combiner.GetCombination("foo"); Assert.IsNotNull(combination); _mocks.VerifyAll(); }
public IncludeStorageFacts() { _mocks = new MockRepository(); _stubKeyGen = _mocks.Stub<IKeyGenerator>(); _storage = new StaticIncludeStorage(_stubKeyGen); _combination = new IncludeCombination(IncludeType.Css, new[] { "~/content/css/foo.css" }, "#foo {color:red}", Clock.UtcNow, new CssTypeElement()); _mocks.ReplayAll(); }
public void SetCombination_ShouldTellStorageToStore() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert();", DateTime.UtcNow, new JsTypeElement()); _mockStorage.Expect(s => s.Store(combination)).Return("foo"); _combiner.UpdateCombination(combination); _mocks.VerifyAll(); }
public void Js_ShouldAskCombinerForCombinationMatchingKey() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo!');", DateTime.UtcNow, new JsTypeElement()); _mockSettings.Expect(s => s.Types[IncludeType.Js]).Return(new JsTypeElement()); _mockCombiner.Expect(c => c.GetCombination("foo")).Return(combination); ActionResult result = _controller.Js("foo"); Assert.IsInstanceOf<IncludeCombinationResult>(result); Assert.AreEqual(combination, ((IncludeCombinationResult)result).Combination); _mocks.VerifyAll(); }
public void Css_ShouldAskCombinerForCombinationMatchingKey() { var combination = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#Foo{color:red;}", DateTime.UtcNow, new CssTypeElement()); _mockSettings.Expect(s => s.Types[IncludeType.Css]).Return(new CssTypeElement()); _mockCombiner.Expect(c => c.GetCombination("foo")).Return(combination); ActionResult result = null; Assert.DoesNotThrow(() => result = _controller.Css("foo")); Assert.IsType<IncludeCombinationResult>(result); Assert.Equal(combination, ((IncludeCombinationResult) result).Combination); _mocks.VerifyAll(); }
public void Js_ShouldAskCombinerForCombinationMatchingKey() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo!');", DateTime.UtcNow, new JsTypeElement()); _mockSettings.Expect(s => s.Types[IncludeType.Js]).Return(new JsTypeElement()); _mockCombiner.Expect(c => c.GetCombination("foo")).Return(combination); ActionResult result = _controller.Js("foo"); Assert.IsInstanceOf <IncludeCombinationResult>(result); Assert.AreEqual(combination, ((IncludeCombinationResult)result).Combination); _mocks.VerifyAll(); }
public string RegisterCombination(IEnumerable<string> sources, IncludeType type, DateTime now) { var combinedContent = new StringBuilder(); foreach (var source in sources) { var include = RegisterInclude(source, type); combinedContent.Append(include.Content).AppendLine(); } var combination = new IncludeCombination(type, sources, combinedContent.ToString(), now, _settings.Types[type]); var key = _storage.Store(combination); return key; }
public void TestSetup() { _mocks = new MockRepository(); _mockHttpContext = _mocks.StrictMock<HttpContextBase>(); _mockController = _mocks.StrictMock<ControllerBase>(); _mockResponse = _mocks.StrictMock<HttpResponseBase>(); _mockRequest = _mocks.StrictMock<HttpRequestBase>(); _mockCachePolicy = _mocks.StrictMock<HttpCachePolicyBase>(); _controllerContext = new ControllerContext(_mockHttpContext, new RouteData(), _mockController); _stubCombiner = _mocks.Stub<IIncludeCombiner>(); _mocks.ReplayAll(); _cssCombination = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#foo{color:red}", DateTime.UtcNow, new CssTypeElement()); }
public void TestSetup() { _mocks = new MockRepository(); _mockHttpContext = _mocks.StrictMock <HttpContextBase>(); _mockController = _mocks.StrictMock <ControllerBase>(); _mockResponse = _mocks.StrictMock <HttpResponseBase>(); _mockRequest = _mocks.StrictMock <HttpRequestBase>(); _mockCachePolicy = _mocks.StrictMock <HttpCachePolicyBase>(); _controllerContext = new ControllerContext(_mockHttpContext, new RouteData(), _mockController); _stubCombiner = _mocks.Stub <IIncludeCombiner>(); _mocks.ReplayAll(); _cssCombination = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#foo{color:red}", DateTime.UtcNow, new CssTypeElement()); }
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 IncludeCombinationTester() { var now = DateTime.UtcNow; var sources = new[] { "foo.js" }; var ic = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()); IdenticalCombinationsDifferentReferences = new EqualsData { A = ic, B = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()), AreEqual = true }; IdenticalCombinationsSameReferences = new EqualsData { A = ic, B = ic, AreEqual = true }; DifferentCombinations = new EqualsData { A = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()), B = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#foo{color:red;}", now, new CssTypeElement()), AreEqual = false }; NoCompression = new ResponseBodyBytes { Compression = ResponseCompression.None, Content = "alert('foo');", ExpectedBytes = new byte[] { 97, 108, 101, 114, 116, 40, 34, 102, 111, 111, 34, 41, 59 } }; Gzip = new ResponseBodyBytes { Compression = ResponseCompression.Gzip, Content = "alert('foo');", ExpectedBytes = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 237, 189, 7, 96, 28, 73, 150, 37, 38, 47, 109, 202, 123, 127, 74, 245, 74, 215, 224, 116, 161, 8, 128, 96, 19, 36, 216, 144, 64, 16, 236, 193, 136, 205, 230, 146, 236, 29, 105, 71, 35, 41, 171, 42, 129, 202, 101, 86, 101, 93, 102, 22, 64, 204, 237, 157, 188, 247, 222, 123, 239, 189, 247, 222, 123, 239, 189, 247, 186, 59, 157, 78, 39, 247, 223, 255, 63, 92, 102, 100, 1, 108, 246, 206, 74, 218, 201, 158, 33, 128, 170, 200, 31, 63, 126, 124, 31, 63, 34, 178, 50, 175, 219, 173, 143, 206, 171, 234, 163, 59, 135, 255, 15, 79, 199, 134, 149, 13, 0, 0, 0 } }; Deflate = new ResponseBodyBytes { Compression = ResponseCompression.Deflate, Content = "alert('foo');", ExpectedBytes = new byte[] { 237, 189, 7, 96, 28, 73, 150, 37, 38, 47, 109, 202, 123, 127, 74, 245, 74, 215, 224, 116, 161, 8, 128, 96, 19, 36, 216, 144, 64, 16, 236, 193, 136, 205, 230, 146, 236, 29, 105, 71, 35, 41, 171, 42, 129, 202, 101, 86, 101, 93, 102, 22, 64, 204, 237, 157, 188, 247, 222, 123, 239, 189, 247, 222, 123, 239, 189, 247, 186, 59, 157, 78, 39, 247, 223, 255, 63, 92, 102, 100, 1, 108, 246, 206, 74, 218, 201, 158, 33, 128, 170, 200, 31, 63, 126, 124, 31, 63, 34, 178, 50, 175, 219, 173, 143, 206, 171, 234, 163, 59, 135, 255, 15 } }; }
public string Store(IncludeCombination combination) { if (combination == null) { throw new ArgumentNullException("combination"); } var key = _keyGen.Generate(combination.Sources); if (!_combinations.ContainsKey(key)) { _combinations.Add(key, combination); } else { _combinations[key] = combination; } return key; }
public IncludeCombinationTester() { var now = DateTime.UtcNow; var sources = new[] { "foo.js" }; var ic = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()); IdenticalCombinationsDifferentReferences = new EqualsData { A = ic, B = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()), AreEqual = true }; IdenticalCombinationsSameReferences = new EqualsData { A = ic, B = ic, AreEqual = true }; DifferentCombinations = new EqualsData { A = new IncludeCombination(IncludeType.Js, sources, "alert('foo');", now, new JsTypeElement()), B = new IncludeCombination(IncludeType.Css, new[] { "foo.css" }, "#foo{color:red;}", now, new CssTypeElement()), AreEqual= false }; NoCompression = new ResponseBodyBytes { Compression = ResponseCompression.None, Content = "alert('foo');", ExpectedBytes = new byte[] { 97, 108, 101, 114, 116, 40, 34, 102, 111, 111, 34, 41, 59 } }; Gzip = new ResponseBodyBytes { Compression = ResponseCompression.Gzip, Content = "alert('foo');", ExpectedBytes = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 237, 189, 7, 96, 28, 73, 150, 37, 38, 47, 109, 202, 123, 127, 74, 245, 74, 215, 224, 116, 161, 8, 128, 96, 19, 36, 216, 144, 64, 16, 236, 193, 136, 205, 230, 146, 236, 29, 105, 71, 35, 41, 171, 42, 129, 202, 101, 86, 101, 93, 102, 22, 64, 204, 237, 157, 188, 247, 222, 123, 239, 189, 247, 222, 123, 239, 189, 247, 186, 59, 157, 78, 39, 247, 223, 255, 63, 92, 102, 100, 1, 108, 246, 206, 74, 218, 201, 158, 33, 128, 170, 200, 31, 63, 126, 124, 31, 63, 34, 178, 50, 175, 219, 173, 143, 206, 171, 234, 163, 59, 135, 255, 15, 79, 199, 134, 149, 13, 0, 0, 0 } }; Deflate = new ResponseBodyBytes { Compression = ResponseCompression.Deflate, Content = "alert('foo');", ExpectedBytes = new byte[] { 237, 189, 7, 96, 28, 73, 150, 37, 38, 47, 109, 202, 123, 127, 74, 245, 74, 215, 224, 116, 161, 8, 128, 96, 19, 36, 216, 144, 64, 16, 236, 193, 136, 205, 230, 146, 236, 29, 105, 71, 35, 41, 171, 42, 129, 202, 101, 86, 101, 93, 102, 22, 64, 204, 237, 157, 188, 247, 222, 123, 239, 189, 247, 222, 123, 239, 189, 247, 186, 59, 157, 78, 39, 247, 223, 255, 63, 92, 102, 100, 1, 108, 246, 206, 74, 218, 201, 158, 33, 128, 170, 200, 31, 63, 126, 124, 31, 63, 34, 178, 50, 175, 219, 173, 143, 206, 171, 234, 163, 59, 135, 255, 15 } }; }
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 SetCombination_ShouldTellStorageToStore() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert();", Clock.UtcNow, new JsTypeElement()); _mockStorage.Expect(s => s.Store(combination)).Return("foo"); Assert.DoesNotThrow(() => _combiner.UpdateCombination(combination)); _mocks.VerifyAll(); }
public void StoreCombination_ShouldThrowWhenNull() { const IncludeCombination combination = null; Assert.Throws <ArgumentNullException>(() => _storage.Store(combination)); }
public void Equals_CorrectlyHandlesNullObject() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo');", DateTime.UtcNow, new JsTypeElement()); var result = combination.Equals((object) null); Assert.IsFalse(result); }
public void Equals_CorrectlyComparesTwoCombinations(IncludeCombination a, IncludeCombination b, bool expected) { var result = a.Equals(b); Assert.Equal(expected, result); }
public void Equals_CorrectlyComparesCombinationToObject(IncludeCombination a, object b, bool expected) { var result = a.Equals(b); Assert.Equal(expected, result); }
public void GetResponseBodyBytes_CorrectlyCompressesCombination(ResponseCompression compression, string content, byte[] expected) { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, content, Clock.UtcNow, new JsTypeElement()); byte[] result = null; Assert.DoesNotThrow(() => result = combination.Bytes[compression]); Assert.Equal(expected, result); }
public void Equals_CorrectlyHandlesNullCombination() { var combination = new IncludeCombination(IncludeType.Js, new[] { "foo.js" }, "alert('foo');", Clock.UtcNow, new JsTypeElement()); var result = combination.Equals(null); Assert.False(result); }
public void GetCombination_WhenCombinationDoesNotExist_ReturnsNull() { IncludeCombination result = _storage.GetCombination("flsihjdf"); Assert.IsNull(result); }
public void UpdateCombination(IncludeCombination combination) { _storage.Store(combination); }