public Task ReadRequestBodyAsyncReadsDataButDoesNotCloseStreamWhenContentLengthIsNull() { // Arrange byte[] expectedSampleTypeByte = Encoding.UTF8.GetBytes( "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$entity\"," + "\"Number\":42" + "}"); ODataInputFormatter formatter = GetInputFormatter(); HttpContext httpContext = GetHttpContext(expectedSampleTypeByte); httpContext.Request.ContentType = "application/json;odata.metadata=minimal"; httpContext.Request.ContentLength = null; Stream memStream = httpContext.Request.Body; InputFormatterContext formatterContext = CreateInputFormatterContext(typeof(Customer), httpContext); // Act return(formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8).ContinueWith( readTask => { // Assert Assert.Equal(TaskStatus.RanToCompletion, readTask.Status); Assert.True(memStream.CanRead); InputFormatterResult result = Assert.IsType <InputFormatterResult>(readTask.Result); Assert.Null(result.Model); })); }
public async Task ReadRequestBodyAsyncReturnsDefaultTypeValueWhenContentLengthIsZero <T>(T value) { // Arrange ODataInputFormatter formatter = GetInputFormatter(); byte[] contentBytes = Encoding.UTF8.GetBytes(""); HttpContext httpContext = GetHttpContext(contentBytes); InputFormatterContext formatterContext = CreateInputFormatterContext(typeof(T), httpContext); // Act InputFormatterResult result = await formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8); // Assert Assert.False(result.HasError); Type valueType = value.GetType(); if (valueType.IsValueType) { T actualResult = Assert.IsType <T>(result.Model); Assert.Equal(default(T), actualResult); } else { Assert.Null(result.Model); } }
public async Task ReadRequestBodyAsyncFailsWhenContentLengthIsZero(Type type) { // Arrange ODataInputFormatter formatter = GetInputFormatter(); byte[] contentBytes = Encoding.UTF8.GetBytes(""); HttpContext httpContext = GetHttpContext(contentBytes); InputFormatterContext formatterContext = CreateInputFormatterContext(type, httpContext); // Act InputFormatterResult result = await formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8); // Assert Assert.True(result.HasError); }
public async Task ReadRequestBodyAsync_ThrowsArgumentNull_ForInputParameter() { // Arrange ODataInputFormatter formatter = GetInputFormatter(); InputFormatterContext formatterContext = null; // Act & Assert await ExceptionAssert.ThrowsArgumentNullAsync(() => formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8), "context"); //Mock<ModelMetadata> mock = new Mock<ModelMetadata>(); //mock.Setup(s => s.ModelType).Returns((Type)null); //formatterContext = new InputFormatterContext(new DefaultHttpContext(), // modelName: string.Empty, // modelState: new ModelStateDictionary(), // metadata: mock.Object, // readerFactory: (stream, encoding) => new StreamReader(stream, encoding)); //await ExceptionAssert.ThrowsArgumentNullAsync(() => formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8), "type"); }
public Task ReadFromStreamAsyncDoesNotCloseStreamWhenContentLengthIsZero() { // Arrange ODataInputFormatter formatter = GetInputFormatter(); Mock <Stream> mockStream = new Mock <Stream>(); byte[] contentBytes = Encoding.UTF8.GetBytes(string.Empty); HttpContext httpContext = GetHttpContext(contentBytes); InputFormatterContext formatterContext = CreateInputFormatterContext(typeof(Customer), httpContext); // Act return(formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8) .ContinueWith( readTask => { // Assert Assert.Equal(TaskStatus.RanToCompletion, readTask.Status); mockStream.Verify(s => s.Close(), Times.Never()); })); }
public Task ReadRequestBodyAsyncReadsDataButDoesNotCloseStreamWhenContentLengthl() { // Arrange byte[] expectedSampleTypeByte = Encoding.UTF8.GetBytes( "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$entity\"," + "\"Number\":42" + "}"); IEdmSingleton singleton = _edmModel.EntityContainer.FindSingleton("Me"); SingletonSegment singletonSeg = new SingletonSegment(singleton); ODataInputFormatter formatter = GetInputFormatter(); formatter.BaseAddressFactory = (request) => new Uri("http://localhost"); HttpContext httpContext = GetHttpContext(expectedSampleTypeByte, opt => opt.AddModel("odata", _edmModel)); httpContext.Request.ContentType = "application/json;odata.metadata=minimal"; httpContext.Request.ContentLength = expectedSampleTypeByte.Length; httpContext.ODataFeature().Model = _edmModel; httpContext.ODataFeature().PrefixName = "odata"; httpContext.ODataFeature().Path = new ODataPath(singletonSeg); Stream memStream = httpContext.Request.Body; InputFormatterContext formatterContext = CreateInputFormatterContext(typeof(Customer), httpContext); // Act return(formatter.ReadRequestBodyAsync(formatterContext, Encoding.UTF8).ContinueWith( readTask => { // Assert Assert.Equal(TaskStatus.RanToCompletion, readTask.Status); Assert.True(memStream.CanRead); var value = Assert.IsType <Customer>(readTask.Result.Model); Assert.Equal(42, value.Number); })); }