/// <summary> /// Sends a single OData batch request. /// </summary> /// <param name="invoker">The invoker.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="contentIdToLocationMapping">The Content-ID to Location mapping.</param> /// <returns></returns> public static async Task <HttpResponseMessage> SendMessageAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken, Dictionary <string, string> contentIdToLocationMapping) { if (invoker == null) { throw Error.ArgumentNull("invoker"); } if (request == null) { throw Error.ArgumentNull("request"); } if (contentIdToLocationMapping != null) { string resolvedRequestUrl = ContentIdHelpers.ResolveContentId(request.RequestUri.OriginalString, contentIdToLocationMapping); request.RequestUri = new Uri(resolvedRequestUrl); request.SetODataContentIdMapping(contentIdToLocationMapping); } HttpResponseMessage response = await invoker.SendAsync(request, cancellationToken); ContentIdHelpers.CopyContentIdToResponse(request, response); if (contentIdToLocationMapping != null) { ContentIdHelpers.AddLocationHeaderToMapping(response, contentIdToLocationMapping); } return(response); }
public static async Task SendRequestAsync(RequestDelegate handler, HttpContext context, Dictionary <string, string> contentIdToLocationMapping) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (contentIdToLocationMapping != null) { string queryString = context.Request.QueryString.HasValue ? context.Request.QueryString.Value : String.Empty; string resolvedRequestUrl = ContentIdHelpers.ResolveContentId(queryString, contentIdToLocationMapping); if (!string.IsNullOrEmpty(resolvedRequestUrl)) { Uri resolvedUri = new Uri(resolvedRequestUrl, UriKind.RelativeOrAbsolute); if (resolvedUri.IsAbsoluteUri) { context.Request.CopyAbsoluteUrl(resolvedUri); } else { context.Request.QueryString = new QueryString(resolvedRequestUrl); } } context.Request.SetODataContentIdMapping(contentIdToLocationMapping); } try { await handler(context).ConfigureAwait(false); string contentId = context.Request.GetODataContentId(); if (contentIdToLocationMapping != null && contentId != null) { AddLocationHeaderToMapping(context.Response, contentIdToLocationMapping, contentId); } } catch (Exception) { // Unlike AspNet, the exception handling is (by default) upstream of this middleware // so we need to trap exceptions on our own. This code is similar to the // ExceptionHandlerMiddleware class in AspNetCore. context.Response.Clear(); context.Response.StatusCode = StatusCodes.Status500InternalServerError; } }
public void ResolveContentId_CannotResolveUnmatchedContentIDInUrl(string key, string url) { // Arrange Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>(); contentIdToLocationMapping.Add(key, "http://localhost/OData/Customers(42)"); // Act string resolvedUrl = ContentIdHelpers.ResolveContentId(url, contentIdToLocationMapping); // Assert Assert.Equal(url, resolvedUrl); }
public void AddLocationHeaderToMapping_DoesNotAddContentIdToLocationMapping_IfLocationIsNull() { // Arrange var response = new HttpResponseMessage(); var contentIdToLocationMapping = new Dictionary <string, string>(); var contentId = Guid.NewGuid().ToString(); // Act ContentIdHelpers.AddLocationHeaderToMapping(response.Headers.Location, contentIdToLocationMapping, contentId); // Assert Assert.False(contentIdToLocationMapping.ContainsKey(contentId)); }
public void ResolveContentId_CannotResolveInvalidContentIDInUrl() { // Arrange string url = "$$/Orders"; Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>(); contentIdToLocationMapping.Add("$", "http://localhost/OData/Customers(42)"); // Act string resolvedUrl = ContentIdHelpers.ResolveContentId(url, contentIdToLocationMapping); // Assert Assert.Equal(url, resolvedUrl); }
public void ResolveContentId_ResolvesExactContentID(string url, string expectedResolvedUrl) { // Arrange Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>(); for (int id = 1; id < 101; id++) { contentIdToLocationMapping.Add(id.ToString(), string.Format("http://localhost/OData/Customers({0})", id)); } // Act string resolvedUrl = ContentIdHelpers.ResolveContentId(url, contentIdToLocationMapping); // Assert Assert.Equal(expectedResolvedUrl, resolvedUrl); }
public void AddLocationHeaderToMapping_AddsContentIdToLocationMapping() { // Arrange var response = new HttpResponseMessage(); response.Headers.Location = new Uri("http://any"); var contentIdToLocationMapping = new Dictionary <string, string>(); var contentId = Guid.NewGuid().ToString(); // Act ContentIdHelpers.AddLocationHeaderToMapping(response.Headers.Location, contentIdToLocationMapping, contentId); // Assert Assert.True(contentIdToLocationMapping.ContainsKey(contentId)); Assert.Equal(response.Headers.Location.AbsoluteUri, contentIdToLocationMapping[contentId]); }
/// <summary> /// Routes a single OData batch request. /// </summary> /// <param name="handler">The handler for processing a message.</param> /// <param name="context">The context.</param> /// <param name="contentIdToLocationMapping">The Content-ID to Location mapping.</param> /// <returns></returns> public static async Task SendRequestAsync(RequestDelegate handler, HttpContext context, Dictionary <string, string> contentIdToLocationMapping) { if (handler == null) { throw Error.ArgumentNull("handler"); } if (context == null) { throw Error.ArgumentNull("context"); } if (contentIdToLocationMapping != null) { string encodedUrl = context.Request.GetEncodedUrl(); string resolvedRequestUrl = ContentIdHelpers.ResolveContentId(encodedUrl, contentIdToLocationMapping); Uri resolvedUri; if (!string.IsNullOrEmpty(resolvedRequestUrl) && Uri.TryCreate(resolvedRequestUrl, UriKind.Absolute, out resolvedUri)) { context.Request.CopyAbsoluteUrl(resolvedUri); } context.Request.SetODataContentIdMapping(contentIdToLocationMapping); } try { await handler(context); string contentId = context.Request.GetODataContentId(); if (contentIdToLocationMapping != null && contentId != null) { AddLocationHeaderToMapping(context.Response, contentIdToLocationMapping, contentId); } } catch (Exception) { // Unlike AspNet, the exception handling is (by default) upstream of this middleware // so we need to trap exceptions on our own. This code is similar to the // ExceptionHandlerMiddleware class in AspNetCore. context.Response.Clear(); context.Response.StatusCode = 500; } }
public Uri ConvertPayloadUri(Uri baseUri, Uri payloadUri) { if (payloadUri == null) { throw new ArgumentNullException("payloadUri"); } string originalPayloadUri = payloadUri.OriginalString; if (ContentIdReferencePattern.IsMatch(originalPayloadUri)) { string resolvedUri = ContentIdHelpers.ResolveContentId(originalPayloadUri, _contentIdMapping); return(new Uri(resolvedUri, UriKind.RelativeOrAbsolute)); } // Returning null for default resolution. return(null); }
private static Uri ResolveContentId(Uri uri, ODataDeserializerContext readContext) { if (uri != null) { IDictionary <string, string> contentIDToLocationMapping = readContext.Request.GetODataContentIdMapping(); if (contentIDToLocationMapping != null) { Uri baseAddress = new Uri(readContext.Request.CreateODataLink()); string relativeUrl = uri.IsAbsoluteUri ? baseAddress.MakeRelativeUri(uri).OriginalString : uri.OriginalString; string resolvedUrl = ContentIdHelpers.ResolveContentId(relativeUrl, contentIDToLocationMapping); Uri resolvedUri = new Uri(resolvedUrl, UriKind.RelativeOrAbsolute); if (!resolvedUri.IsAbsoluteUri) { resolvedUri = new Uri(baseAddress, uri); } return(resolvedUri); } } return(uri); }