public static void GetMatchNonMatchTest( string headerName, string[] values, bool existsInStore, bool expectReturnNull, HttpStatusCode expectedStatus = HttpStatusCode.Unused) { // setup var mocks = new MockRepository(); var entityTagStore = mocks.StrictMock<IEntityTagStore>(); var entityTagHandler = new CachingHandler(entityTagStore); var request = new HttpRequestMessage(HttpMethod.Get, TestUrl); request.Headers.Add(headerName, values); TimedEntityTagHeaderValue entityTagHeaderValue = new TimedEntityTagHeaderValue("\"12345678\""); if(values.Length>0) // if entityTagStore.Expect(x => x.TryGetValue(Arg<EntityTagKey>.Matches(etg => etg.ResourceUri == entityTagHandler.UriTrimmer(new Uri(TestUrl))), out Arg<TimedEntityTagHeaderValue>.Out(entityTagHeaderValue).Dummy)).Return(existsInStore); mocks.ReplayAll(); // run var matchNoneMatch = entityTagHandler.GetIfMatchNoneMatch(); // verify Task<HttpResponseMessage> resultTask = matchNoneMatch(request); Assert.That(expectReturnNull ^ resultTask != null, "result was not as expected"); if(resultTask!=null && expectedStatus != HttpStatusCode.Unused) { Assert.AreEqual(expectedStatus, resultTask.Result.StatusCode, "Status code"); } mocks.VerifyAll(); }
public static void GetModifiedNotModifiedTest( string headerName, bool resourceHasChanged, bool expectReturnNull, HttpStatusCode expectedStatus = HttpStatusCode.Unused) { // setup var mocks = new MockRepository(); var entityTagStore = mocks.StrictMock<IEntityTagStore>(); var entityTagHandler = new CachingHandler(entityTagStore); var request = new HttpRequestMessage(HttpMethod.Get, TestUrl); DateTimeOffset lastChanged = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(7)); DateTimeOffset lastModifiedInQuestion = resourceHasChanged ? lastChanged.Subtract(TimeSpan.FromDays(1)) : lastChanged.Add(TimeSpan.FromDays(1)); request.Headers.Add(headerName, lastModifiedInQuestion.ToString("r")); var entityTagHeaderValue = new TimedEntityTagHeaderValue("\"12345678\"") {LastModified = lastChanged}; entityTagStore.Expect(x => x.TryGetValue(Arg<EntityTagKey>.Matches(etg => etg.ResourceUri == entityTagHandler.UriTrimmer(new Uri(TestUrl))), out Arg<TimedEntityTagHeaderValue>.Out(entityTagHeaderValue).Dummy)).Return(true); mocks.ReplayAll(); // run var modifiedUnmodifiedSince = entityTagHandler.GetIfModifiedUnmodifiedSince(); var task = modifiedUnmodifiedSince(request); HttpResponseMessage response = task == null ? null : task.Result; // verify Assert.That(expectReturnNull ^ task != null, "result was not as expected"); if (task != null && expectedStatus != HttpStatusCode.Unused) { Assert.AreEqual(expectedStatus, response.StatusCode, "Status code"); } mocks.VerifyAll(); }