private static BatchWriterStatesTestSetupResult GetOperationStream(object message, WriterTestConfiguration testConfiguration) { BatchWriterStatesTestSetupResult result = new BatchWriterStatesTestSetupResult { Message = message }; ODataBatchOperationRequestMessage requestMessage = message as ODataBatchOperationRequestMessage; if (requestMessage != null) { if (testConfiguration.Synchronous) { result.MessageStream = requestMessage.GetStream(); return(result); } else { // TODO: 191417: Enable async Tests on Phone and Silverlight when Product Supports them #if SILVERLIGHT || WINDOWS_PHONE throw new TaupoNotSupportedException("This test is not supported in aSynchronous mode in Silverlight or Phone"); #else var t = requestMessage.GetStreamAsync(); t.Wait(); result.MessageStream = t.Result; return(result); #endif } } ODataBatchOperationResponseMessage responseMessage = message as ODataBatchOperationResponseMessage; if (responseMessage != null) { if (testConfiguration.Synchronous) { result.MessageStream = responseMessage.GetStream(); return(result); } else { // TODO: Enable async Tests on Phone and Silverlight when Product Supports them #if SILVERLIGHT || WINDOWS_PHONE throw new TaupoNotSupportedException("This test is not supported in aSynchronous mode in Silverlight or Phone"); #else var t = responseMessage.GetStreamAsync(); t.Wait(); result.MessageStream = t.Result; return(result); #endif } } return(null); }
private static Stream GetMessageStream(ODataBatchOperationResponseMessage responseMessage, WriterTestConfiguration testConfiguration) { if (testConfiguration.Synchronous) { return(responseMessage.GetStream()); } else { var t = responseMessage.GetStreamAsync(); t.Wait(); return(t.Result); } }
private async Task WriteOperation(ODataBatchWriter writer, OeOperationMessage operation) { ODataBatchOperationResponseMessage operationMessage = await writer.CreateOperationResponseMessageAsync(operation.ContentId); operationMessage.SetHeader("Location", operation.RequestUrl.AbsoluteUri); operationMessage.SetHeader(ODataConstants.ContentTypeHeader, operation.ContentType); operationMessage.StatusCode = (int)operation.StatusCode; if (operation.StatusCode != HttpStatusCode.NoContent) { using (Stream stream = await operationMessage.GetStreamAsync()) await WriteEntity(operation.EntitySet, operation.Entry, stream); } }
/// <summary> /// Writes a single OData batch response. /// </summary> /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param> /// <param name="context">The message context.</param> /// <param name="asyncWriter">Whether or not the writer is in async mode. </param> public static async Task WriteMessageAsync(ODataBatchWriter writer, HttpContext context, bool asyncWriter) { if (writer == null) { throw Error.ArgumentNull("writer"); } if (context == null) { throw Error.ArgumentNull("context"); } string contentId = (context.Request != null) ? context.Request.GetODataContentId() : String.Empty; ODataBatchOperationResponseMessage batchResponse = asyncWriter ? await writer.CreateOperationResponseMessageAsync(contentId) : writer.CreateOperationResponseMessage(contentId); batchResponse.StatusCode = context.Response.StatusCode; foreach (KeyValuePair <string, StringValues> header in context.Response.Headers) { batchResponse.SetHeader(header.Key, String.Join(",", header.Value.ToArray())); } if (context.Response.Body != null && context.Response.Body.Length != 0) { using (Stream stream = asyncWriter ? await batchResponse.GetStreamAsync() : batchResponse.GetStream()) { context.RequestAborted.ThrowIfCancellationRequested(); context.Response.Body.Seek(0L, SeekOrigin.Begin); await context.Response.Body.CopyToAsync(stream); // Close and release the stream for the individual response ODataBatchStream batchStream = context.Response.Body as ODataBatchStream; if (batchStream != null) { if (asyncWriter) { await batchStream.InternalDisposeAsync(); } else { batchStream.InternalDispose(); } } } } }
private static BatchWriterStatesTestSetupResult GetOperationStream(object message, WriterTestConfiguration testConfiguration) { BatchWriterStatesTestSetupResult result = new BatchWriterStatesTestSetupResult { Message = message }; ODataBatchOperationRequestMessage requestMessage = message as ODataBatchOperationRequestMessage; if (requestMessage != null) { if (testConfiguration.Synchronous) { result.MessageStream = requestMessage.GetStream(); return(result); } else { var t = requestMessage.GetStreamAsync(); t.Wait(); result.MessageStream = t.Result; return(result); } } ODataBatchOperationResponseMessage responseMessage = message as ODataBatchOperationResponseMessage; if (responseMessage != null) { if (testConfiguration.Synchronous) { result.MessageStream = responseMessage.GetStream(); return(result); } else { var t = responseMessage.GetStreamAsync(); t.Wait(); result.MessageStream = t.Result; return(result); } } return(null); }
/// <summary> /// Writes a single OData batch response. /// </summary> /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param> /// <param name="response">The response message.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="asyncWriter">Whether or not the writer is writing asynchronously.</param> /// <returns>A task object representing writing the given batch response using the given writer.</returns> public static async Task WriteMessageAsync(ODataBatchWriter writer, HttpResponseMessage response, CancellationToken cancellationToken, bool asyncWriter) { if (writer == null) { throw Error.ArgumentNull("writer"); } if (response == null) { throw Error.ArgumentNull("response"); } HttpRequestMessage request = response.RequestMessage; string contentId = (request != null) ? request.GetODataContentId() : String.Empty; ODataBatchOperationResponseMessage batchResponse = asyncWriter ? await writer.CreateOperationResponseMessageAsync(contentId) : writer.CreateOperationResponseMessage(contentId); batchResponse.StatusCode = (int)response.StatusCode; foreach (KeyValuePair <string, IEnumerable <string> > header in response.Headers) { batchResponse.SetHeader(header.Key, String.Join(",", header.Value)); } if (response.Content != null) { foreach (KeyValuePair <string, IEnumerable <string> > header in response.Content.Headers) { batchResponse.SetHeader(header.Key, String.Join(",", header.Value)); } using (Stream stream = asyncWriter ? await batchResponse.GetStreamAsync() : batchResponse.GetStream()) { cancellationToken.ThrowIfCancellationRequested(); await response.Content.CopyToAsync(stream); } } }
private static Stream GetMessageStream(ODataBatchOperationResponseMessage responseMessage, WriterTestConfiguration testConfiguration) { if (testConfiguration.Synchronous) { return responseMessage.GetStream(); } else { var t = responseMessage.GetStreamAsync(); t.Wait(); return t.Result; } }