public async Task SerializeToStreamAsync_WritesODataBatchResponseItems() { HttpContext okContext = new DefaultHttpContext(); okContext.Response.StatusCode = (int)HttpStatusCode.OK; HttpContext acceptedContext = new DefaultHttpContext(); acceptedContext.Response.StatusCode = (int)HttpStatusCode.Accepted; HttpContext badRequestContext = new DefaultHttpContext(); badRequestContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; ODataBatchContent batchContent = CreateBatchContent(new ODataBatchResponseItem[] { new OperationResponseItem(okContext), new ChangeSetResponseItem(new HttpContext[] { acceptedContext, badRequestContext }) }); MemoryStream stream = new MemoryStream(); await batchContent.SerializeToStreamAsync(stream); stream.Position = 0; string responseString = await new StreamReader(stream).ReadToEndAsync(); Assert.Contains("changesetresponse", responseString); Assert.Contains("OK", responseString); Assert.Contains("Accepted", responseString); Assert.Contains("Bad Request", responseString); }
public void Dispose_DisposesODataBatchResponseItems() { MockHttpResponseMessage[] responses = new MockHttpResponseMessage[] { new MockHttpResponseMessage(), new MockHttpResponseMessage(), new MockHttpResponseMessage() }; ODataBatchContent batchContent = new ODataBatchContent(new ODataBatchResponseItem[] { new OperationResponseItem(responses[0]), new ChangeSetResponseItem(new HttpResponseMessage[] { responses[1], responses[2] }) }); HttpResponseMessage batchResponse = new HttpResponseMessage { Content = batchContent }; batchResponse.Dispose(); foreach (var response in responses) { Assert.True(response.IsDisposed); } }
public void ODataVersionInWriterSetting_IsPropagatedToTheHeader() { ODataBatchContent batchContent = CreateBatchContent(new ODataBatchResponseItem[0]); var odataVersion = batchContent.Headers.FirstOrDefault(h => String.Equals(h.Key, ODataVersionConstraint.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); Assert.Equal("4.0", odataVersion.Value.FirstOrDefault()); }
public void NoODataVersionSettingInWriterSetting_SetDefaultVersionInTheHeader() { // Arrange & Act ODataBatchContent batchContent = CreateBatchContent(Array.Empty <ODataBatchResponseItem>()); var odataVersion = batchContent.Headers .FirstOrDefault(h => string.Equals(h.Key, ODataVersionConstraint.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); // Assert Assert.Equal("4.0", odataVersion.Value.FirstOrDefault()); }
public void ODataVersionInWriterSetting_IsPropagatedToTheHeader() { ODataBatchContent batchContent = new ODataBatchContent(new ODataBatchResponseItem[0], new ODataMessageWriterSettings { Version = ODataVersion.V2 }); var odataVersion = batchContent.Headers.FirstOrDefault(h => String.Equals(h.Key, HttpRequestMessageProperties.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); Assert.NotNull(odataVersion); Assert.Equal("2.0", odataVersion.Value.FirstOrDefault()); }
public void Parameter_Constructor() { ODataBatchContent batchContent = CreateBatchContent(new ODataBatchResponseItem[0]); var contentType = batchContent.Headers.ContentType; var boundary = contentType.Parameters.FirstOrDefault(p => String.Equals(p.Name, "boundary", StringComparison.OrdinalIgnoreCase)); var odataVersion = batchContent.Headers.FirstOrDefault(h => String.Equals(h.Key, ODataVersionConstraint.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); Assert.NotNull(boundary); Assert.NotEmpty(boundary.Value); Assert.NotEmpty(odataVersion.Value); Assert.Equal("multipart/mixed", contentType.MediaType); }
public void ODataVersionInWriterSetting_IsPropagatedToTheHeader(ODataVersion version, string expect) { // Arrange & Act ODataBatchContent batchContent = CreateBatchContent(Array.Empty <ODataBatchResponseItem>(), s => s.AddSingleton(new ODataMessageWriterSettings { Version = version })); var odataVersion = batchContent.Headers .FirstOrDefault(h => string.Equals(h.Key, ODataVersionConstraint.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); // Assert Assert.Equal(expect, odataVersion.Value.FirstOrDefault()); }
public void Parameter_Constructor() { const string boundaryHeader = "boundary"; ODataBatchContent batchContent = CreateBatchContent(new ODataBatchResponseItem[0]); string contentTypeHeader = batchContent.Headers[HeaderNames.ContentType].FirstOrDefault(); string mediaType = contentTypeHeader.Substring(0, contentTypeHeader.IndexOf(';')); int boundaryParamStart = contentTypeHeader.IndexOf(boundaryHeader); string boundary = contentTypeHeader.Substring(boundaryParamStart + boundaryHeader.Length); var odataVersion = batchContent.Headers.FirstOrDefault(h => String.Equals(h.Key, ODataVersionConstraint.ODataServiceVersionHeader, StringComparison.OrdinalIgnoreCase)); Assert.NotEmpty(boundary); Assert.NotEmpty(odataVersion.Value); Assert.Equal("multipart/mixed", mediaType); }
public async Task SerializeToStreamAsync_WritesODataBatchResponseItems() { // Arrange HttpContext okContext = new DefaultHttpContext(); okContext.Response.StatusCode = StatusCodes.Status200OK; HttpContext acceptedContext = new DefaultHttpContext(); acceptedContext.Response.StatusCode = StatusCodes.Status202Accepted; HttpContext badRequestContext = new DefaultHttpContext(); badRequestContext.Response.StatusCode = StatusCodes.Status400BadRequest; // Act ODataBatchContent batchContent = new ODataBatchContent(new ODataBatchResponseItem[] { new OperationResponseItem(okContext), new ChangeSetResponseItem(new HttpContext[] { acceptedContext, badRequestContext }) }, new MockServiceProvider()); MemoryStream stream = new MemoryStream(); await batchContent.SerializeToStreamAsync(stream); stream.Position = 0; string responseString = await new StreamReader(stream).ReadToEndAsync(); // Assert Assert.Contains("changesetresponse", responseString); Assert.Contains("OK", responseString); Assert.Contains("Accepted", responseString); Assert.Contains("Bad Request", responseString); }
public void SerializeToStreamAsync_WritesODataBatchResponseItems() { ODataBatchContent batchContent = new ODataBatchContent(new ODataBatchResponseItem[] { new OperationResponseItem(new HttpResponseMessage(HttpStatusCode.OK)), new ChangeSetResponseItem(new HttpResponseMessage[] { new HttpResponseMessage(HttpStatusCode.Accepted), new HttpResponseMessage(HttpStatusCode.BadRequest) }) }); HttpResponseMessage response = new HttpResponseMessage { Content = batchContent }; string responseString = response.Content.ReadAsStringAsync().Result; Assert.Contains("changesetresponse", responseString); Assert.Contains("OK", responseString); Assert.Contains("Accepted", responseString); Assert.Contains("Bad Request", responseString); }