public async Task CopyToAsync_ThrowCustomExceptionInOverriddenMethod_ThrowsMockException() { var content = new MockContent(new MockException(), MockOptions.ThrowInSerializeMethods); var m = new MemoryStream(); await Assert.ThrowsAsync<MockException>(() => content.CopyToAsync(m)); }
public void CopyToAsync_ThrowCustomExceptionInOverriddenAsyncMethod_ExceptionBubblesUp() { var content = new MockContent(new MockException(), MockOptions.ThrowInAsyncSerializeMethods); var m = new MemoryStream(); Assert.Throws<MockException>(() => { Task t = content.CopyToAsync(m); }); }
public async Task CopyToAsync_ThrowIOExceptionInOverriddenMethod_ThrowsWrappedHttpRequestException() { var content = new MockContent(new IOException(), MockOptions.ThrowInSerializeMethods); Task t = content.CopyToAsync(new MemoryStream()); HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => t); Assert.IsType<IOException>(ex.InnerException); }
public async Task CopyToAsync_ThrowObjectDisposedExceptionInOverriddenAsyncMethod_ThrowsWrappedHttpRequestException() { var content = new MockContent(new ObjectDisposedException(""), MockOptions.ThrowInAsyncSerializeMethods); var m = new MemoryStream(); HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => content.CopyToAsync(m)); Assert.IsType<ObjectDisposedException>(ex.InnerException); }
public async Task CopyToAsync_CallWithMockContent_MockContentMethodCalled() { var content = new MockContent(MockOptions.CanCalculateLength); var m = new MemoryStream(); await content.CopyToAsync(m); Assert.Equal(1, content.SerializeToStreamAsyncCount); Assert.Equal(content.GetMockData(), m.ToArray()); }
public void Dispose_DisposeObject_ContentGetsDisposedAndSettersWillThrowButGettersStillWork() { var rm = new HttpResponseMessage(HttpStatusCode.OK); var content = new MockContent(); rm.Content = content; Assert.False(content.IsDisposed); rm.Dispose(); rm.Dispose(); // Multiple calls don't throw. Assert.True(content.IsDisposed); Assert.Throws<ObjectDisposedException>(() => { rm.StatusCode = HttpStatusCode.BadRequest; }); Assert.Throws<ObjectDisposedException>(() => { rm.ReasonPhrase = "Bad Request"; }); Assert.Throws<ObjectDisposedException>(() => { rm.Version = new Version(1, 0); }); Assert.Throws<ObjectDisposedException>(() => { rm.Content = null; }); // Property getters should still work after disposing. Assert.Equal(HttpStatusCode.OK, rm.StatusCode); Assert.Equal("OK", rm.ReasonPhrase); Assert.Equal(new Version(1, 1), rm.Version); Assert.Equal(content, rm.Content); }
public async Task LoadIntoBufferAsync_ThrowObjectDisposedExceptionInOverriddenMethod_ThrowsWrappedHttpRequestException() { var content = new MockContent(new ObjectDisposedException(""), MockOptions.ThrowInSerializeMethods); HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync()); Assert.IsType<ObjectDisposedException>(ex.InnerException); }
public async Task LoadIntoBufferAsync_CallMultipleTimesWithNullContentLength_CopyToAsyncMemoryStreamCalledOnce() { var content = new MockContent(); await content.LoadIntoBufferAsync(); await content.LoadIntoBufferAsync(); Assert.Equal(1, content.SerializeToStreamAsyncCount); Stream stream = await content.ReadAsStreamAsync(); Assert.False(stream.CanWrite); }
public async Task CopyToAsync_BufferContentFirst_UseBufferedStreamAsSource() { var data = new byte[10]; var content = new MockContent(data); content.LoadIntoBufferAsync().Wait(); Assert.Equal(1, content.SerializeToStreamAsyncCount); var destination = new MemoryStream(); await content.CopyToAsync(destination); // Our MockContent should not be called for the CopyTo() operation since the buffered stream should be // used. Assert.Equal(1, content.SerializeToStreamAsyncCount); Assert.Equal(data.Length, destination.Length); }
public async Task ReadAsStringAsync_SetNoCharset_DefaultCharsetUsed() { // Use content with umlaut characters. string sourceString = "ÄäüÜ"; // c4 e4 fc dc Encoding defaultEncoding = Encoding.GetEncoding("utf-8"); byte[] contentBytes = defaultEncoding.GetBytes(sourceString); var content = new MockContent(contentBytes); // Reading the string should consider the charset of the 'Content-Type' header. string result = await content.ReadAsStringAsync(); Assert.Equal(sourceString, result); }
public async Task ReadAsStringAsync_EmptyContent_EmptyString() { var content = new MockContent(new byte[0]); string actualContent = await content.ReadAsStringAsync(); Assert.Equal(string.Empty, actualContent); }
public void Dispose_DisposeContentThenAccessContentLength_Throw() { var content = new MockContent(); // This is not really typical usage of the type, but let's make sure we consider also this case: The user // keeps a reference to the Headers property before disposing the content. Then after disposing, the user // accesses the ContentLength property. var headers = content.Headers; content.Dispose(); Assert.Throws<ObjectDisposedException>(() => headers.ContentLength.ToString()); }
public async Task Dispose_GetReadStreamThenDispose_ReadStreamGetsDisposed() { var content = new MockContent(); MockMemoryStream s = (MockMemoryStream) await content.ReadAsStreamAsync(); Assert.Equal(1, content.CreateContentReadStreamCount); Assert.Equal(0, s.DisposeCount); content.Dispose(); Assert.Equal(1, s.DisposeCount); }
public async Task ReadAsStreamAsync_FirstGetFromUnbufferedContentThenGetFromBufferedContent_SameStream() { var content = new MockContent(MockOptions.CanCalculateLength); Stream before = await content.ReadAsStreamAsync(); Assert.Equal(1, content.CreateContentReadStreamCount); await content.LoadIntoBufferAsync(); Stream after = await content.ReadAsStreamAsync(); Assert.Equal(1, content.CreateContentReadStreamCount); // Note that ContentReadStream returns always the same stream. If the user gets the stream, buffers content, // and gets the stream again, the same instance is returned. Returning a different instance could be // confusing, even though there shouldn't be any real world scenario for retrieving the read stream both // before and after buffering content. Assert.Equal(before, after); }
public async Task ReadAsStreamAsync_GetFromBufferedContent_CreateContentReadStreamCalled() { var content = new MockContent(MockOptions.CanCalculateLength); await content.LoadIntoBufferAsync(); Stream stream = await content.ReadAsStreamAsync(); Assert.Equal(0, content.CreateContentReadStreamCount); Assert.Equal(content.GetMockData().Length, stream.Length); Stream stream2 = await content.ReadAsStreamAsync(); Assert.Same(stream, stream2); Assert.Equal(0, stream.Position); Assert.Equal((byte)'d', stream.ReadByte()); }
public async Task ReadAsStreamAsync_GetFromUnbufferedContent_CreateContentReadStreamCalledOnce() { var content = new MockContent(MockOptions.CanCalculateLength); // Call multiple times: CreateContentReadStreamAsync() should be called only once. Stream stream = await content.ReadAsStreamAsync(); stream = await content.ReadAsStreamAsync(); stream = await content.ReadAsStreamAsync(); Assert.Equal(1, content.CreateContentReadStreamCount); Assert.Equal(content.GetMockData().Length, stream.Length); Stream stream2 = await content.ReadAsStreamAsync(); Assert.Same(stream, stream2); }
public void TryComputeLength_ThrowCustomExceptionInOverriddenMethod_ExceptionBubblesUpToCaller() { var content = new MockContent(MockOptions.ThrowInTryComputeLength); var m = new MemoryStream(); Assert.Throws<MockException>(() => content.Headers.ContentLength); }
public async Task TryComputeLength_RetrieveContentLengthFromBufferedContent_ComputeLengthIsNotCalled() { var content = new MockContent(); await content.LoadIntoBufferAsync(); Assert.Equal(content.GetMockData().Length, content.Headers.ContentLength); // Called once to determine the size of the buffer. Assert.Equal(1, content.TryComputeLengthCount); }
public void TryComputeLength_RetrieveContentLength_ComputeLengthShouldBeCalled() { var content = new MockContent(MockOptions.CanCalculateLength); Assert.Equal(content.GetMockData().Length, content.Headers.ContentLength); Assert.Equal(1, content.TryComputeLengthCount); }
public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenAsyncMethod_ExceptionBubblesUpToCaller() { var content = new MockContent(new MockException(), MockOptions.ThrowInAsyncSerializeMethods); await Assert.ThrowsAsync<MockException>(() => content.LoadIntoBufferAsync()); }
public async Task LoadIntoBufferAsync_ThrowIOExceptionInOverriddenAsyncMethod_ThrowsHttpRequestException() { var content = new MockContent(new IOException(), MockOptions.ThrowInAsyncSerializeMethods); HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync()); Assert.IsType<IOException>(ex.InnerException); }
public async Task ReadAsStreamAsync_UseBaseImplementation_ContentGetsBufferedThenMemoryStreamReturned() { var content = new MockContent(MockOptions.DontOverrideCreateContentReadStream); Stream stream = await content.ReadAsStreamAsync(); Assert.NotNull(stream); Assert.Equal(1, content.SerializeToStreamAsyncCount); Stream stream2 = await content.ReadAsStreamAsync(); Assert.Same(stream, stream2); Assert.Equal(0, stream.Position); Assert.Equal((byte)'d', stream.ReadByte()); }
public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithNullContentLength_ThrowsHttpRequestException() { var content = new MockContent(); await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1)); }
public async Task Dispose_DisposedObjectThenAccessMembers_ThrowsObjectDisposedException() { var content = new MockContent(); content.Dispose(); var m = new MemoryStream(); await Assert.ThrowsAsync<ObjectDisposedException>(() => content.CopyToAsync(m)); await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsByteArrayAsync()); await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsStringAsync()); await Assert.ThrowsAsync<ObjectDisposedException>(() => content.ReadAsStreamAsync()); await Assert.ThrowsAsync<ObjectDisposedException>(() => content.LoadIntoBufferAsync()); // Note that we don't throw when users access the Headers property. This is useful e.g. to be able to // read the headers of a content, even though the content is already disposed. Note that the .NET guidelines // only require members to throw ObjectDisposedExcpetion for members "that cannot be used after the object // has been disposed of". _output.WriteLine(content.Headers.ToString()); }
public async Task CopyToAsync_UseStreamWriteByteWithBufferSizeSmallerThanContentSize_ThrowsHttpRequestException() { // MockContent uses stream.WriteByte() rather than stream.Write(): Verify that the max. buffer size // is also checked when using WriteByte(). var content = new MockContent(MockOptions.UseWriteByteInCopyTo); await Assert.ThrowsAsync<HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1)); }
public async Task LoadIntoBufferAsync_ThrowCustomExceptionInOverriddenAsyncMethod_ExceptionBubblesUpToCaller() { var content = new MockContent(new MockException(), MockOptions.ThrowInAsyncSerializeMethods); await Assert.ThrowsAsync <MockException>(() => content.LoadIntoBufferAsync()); }
public async Task ReadAsStringAsync_SetInvalidCharset_ThrowsInvalidOperationException() { string sourceString = "some string"; byte[] contentBytes = Encoding.UTF8.GetBytes(sourceString); var content = new MockContent(contentBytes); content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); content.Headers.ContentType.CharSet = "invalid"; // This will throw because we have an invalid charset. await Assert.ThrowsAsync<InvalidOperationException>(() => content.ReadAsStringAsync()); }
public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithCalculatedContentLength_ThrowsHttpRequestException() { var content = new MockContent(MockOptions.CanCalculateLength); await Assert.ThrowsAsync <HttpRequestException>(() => content.LoadIntoBufferAsync(content.GetMockData().Length - 1)); }
public async Task ReadAsByteArrayAsync_EmptyContent_EmptyArray() { var content = new MockContent(new byte[0]); byte[] bytes = await content.ReadAsByteArrayAsync(); Assert.Equal(0, bytes.Length); }
public async Task LoadIntoBufferAsync_BufferSizeSmallerThanContentSizeWithNullContentLength_ThrowsHttpRequestException() { var content = new MockContent(); Task t = content.LoadIntoBufferAsync(content.GetMockData().Length - 1); await Assert.ThrowsAsync <HttpRequestException>(() => t); }
public async Task LoadIntoBufferAsync_CallOnMockContentWithNullContentLength_CopyToAsyncMemoryStreamCalled() { var content = new MockContent(); Assert.Null(content.Headers.ContentLength); await content.LoadIntoBufferAsync(); Assert.NotNull(content.Headers.ContentLength); Assert.Equal(content.MockData.Length, content.Headers.ContentLength); Assert.Equal(1, content.SerializeToStreamAsyncCount); Stream stream = await content.ReadAsStreamAsync(); Assert.False(stream.CanWrite); }
public async Task LoadIntoBufferAsync_CallOnMockContentWithLessLengthThanContentLengthHeader_BufferedStreamLengthMatchesActualLengthNotContentLengthHeaderValue() { byte[] data = Encoding.UTF8.GetBytes("16 bytes of data"); var content = new MockContent(data); content.Headers.ContentLength = 32; // Set the Content-Length header to a value > actual data length. Assert.Equal(32, content.Headers.ContentLength); await content.LoadIntoBufferAsync(); Assert.Equal(1, content.SerializeToStreamAsyncCount); Assert.NotNull(content.Headers.ContentLength); Assert.Equal(32, content.Headers.ContentLength); Stream stream = await content.ReadAsStreamAsync(); Assert.Equal(data.Length, stream.Length); }
public void CopyToAsync_MockContentReturnsNull_ThrowsInvalidOperationException() { // return 'null' when CopyToAsync() is called. var content = new MockContent(MockOptions.ReturnNullInCopyToAsync); var m = new MemoryStream(); // The HttpContent derived class (MockContent in our case) must return a Task object when WriteToAsync() // is called. If not, HttpContent will throw. Assert.Throws<InvalidOperationException>(() => { Task t = content.CopyToAsync(m); }); }
public void CanCheckOUtAndCreateOrder() { IRepository <Customer> customers = new MockContent <Customer>(); IRepository <Product> products = new MockContent <Product>(); products.Insert(new Product() { Id = "1", Price = 10.00m }); products.Insert(new Product() { Id = "2", Price = 20.00m }); IRepository <Basket> baskets = new MockContent <Basket>(); Basket basket = new Basket(); basket.BasketItems.Add(new BasketItem() { ProductId = "1", Quantity = 2, BasketId = basket.Id }); basket.BasketItems.Add(new BasketItem() { ProductId = "1", Quantity = 1, BasketId = basket.Id }); baskets.Insert(basket); IBasketService basketService = new BasketService(products, baskets); IRepository <Order> orders = new MockContent <Order>(); IOrderService orderService = new OrderService(orders); customers.Insert(new Customer() { Id = "1", Email = "*****@*****.**", ZipCode = "90000" }); IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "Forms"), null); var controller = new BasketController(basketService, orderService, customers); var httpContext = new MockHttpContext(); httpContext.User = FakeUser; httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket") { Value = basket.Id }); controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller); Order order = new Order(); controller.Checkout(order); Assert.AreEqual(2, order.OrderItems.Count); Assert.AreEqual(0, basket.BasketItems.Count); //Order orderInRep = orders.Find(order.Id); //Assert.AreEqual(2, orderInRep.OrderItems.Count); }