/// <summary> /// Constructor for HttpClientRequestMessage. /// </summary> /// <param name="client">The client</param> /// <param name="args">The args</param> /// <param name="config">The config</param> public HttpClientRequestMessage(HttpClient client, DataServiceClientRequestMessageArgs args, DataServiceClientConfigurations config) : base(args.ActualMethod) { this.requestMessage = new HttpRequestMessage(); // TODO, avoid create Memory Stream each time, consider object pooling this.messageStream = new MemoryStream(); this.contentHeaderValueCache = new Dictionary <string, string>(); foreach (var header in args.Headers) { this.SetHeader(header.Key, header.Value); } this.Url = args.RequestUri; this.Method = args.Method; this.config = config; // link http and odata properties to share state between odata and http handlers /* * TODO: UNCOMMENT this after properties is supported * if (config.Properties != null) * { * foreach (var item in config.Properties) * { * this.requestMessage.Properties[item.Key] = item.Value; * } * } */ this.client = client; }
public void HeaderShouldBeCaseInsensitive() { HeaderCollection headers = new HeaderCollection(); headers.SetHeader("Accept", MimeType); headers.SetHeader("Content-Type", MimeType); BuildingRequestEventArgs requestEventArgs = new BuildingRequestEventArgs("GET", new Uri("http://localhost"), headers, null, HttpStack.Auto); DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs( requestEventArgs.Method, requestEventArgs.RequestUri, false, false, requestEventArgs.Headers); HttpWebRequestMessage request = new HttpWebRequestMessage(args); const int maxPageSize = 10; ODataPreferenceHeader preferHeader = new ODataPreferenceHeader(request); preferHeader.MaxPageSize = maxPageSize; Assert.Equal(maxPageSize, preferHeader.MaxPageSize); string expected = $"{MaxPageSizePreference}={maxPageSize}"; Assert.Equal(expected, request.GetHeader("pReFer")); Assert.Equal(MimeType, request.GetHeader("accepT")); }
public TestHttpWebRequestMessage(DataServiceClientRequestMessageArgs args, IDictionary <string, string> headers, int statusCode, Func <Stream> getResponseStream) : base(args) { this.headers = headers; this.statusCode = statusCode; this.getResponseStream = getResponseStream; }
internal DataServiceClientRequestMessage ShimDataServiceRequest(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; message = (this._dataServiceHandler == null) ? new HttpWebRequestMessage(args) : this._dataServiceHandler(args); InitializeMessage(message); return(message); }
public void TestHeaders(Dictionary <string, string> headers) { var client = new HttpClient(); var arg = new DataServiceClientRequestMessageArgs("Get", new Uri("Http://localhost"), true, true, headers); var message = new HttpClientRequestMessage(client, arg, new DataServiceContext().Configurations); message.Headers.Should().BeEquivalentTo(headers); }
public void QueryWithFormat() { Dictionary <string, string> testCases = new Dictionary <string, string>() { { "People?$format=application/json", "application/json" }, // ODL Bug: https://github.com/OData/odata.net/issues/313 ////{"People?$format=application/json;odata.metadata=full", "application/json; odata.metadata=full"}, { "People?$format=json", "application/json" }, }; ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings() { BaseUri = ServiceBaseUri, UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty }; foreach (var testCase in testCases) { DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs( "GET", new Uri(ServiceBaseUri.AbsoluteUri + testCase.Key, UriKind.Absolute), false, false, new Dictionary <string, string>() { }); var requestMessage = new HttpWebRequestMessage(args); using (var responseMessage = requestMessage.GetResponse() as HttpWebResponseMessage) { Assert.Equal(200, responseMessage.StatusCode); string contentType = responseMessage.Headers.FirstOrDefault(x => x.Key.Equals("Content-Type")).Value; Assert.True(contentType.StartsWith(testCase.Value)); using (var messageReader = new ODataMessageReader( responseMessage, readerSettings, this.TestClientContext.Format.LoadServiceModel())) { var reader = messageReader.CreateODataFeedReader(); while (reader.Read()) { if (reader.State == ODataReaderState.EntryEnd) { ODataEntry entry = reader.Item as ODataEntry; Assert.NotNull(entry.Properties.Single(p => p.Name == "PersonId").Value); } else if (reader.State == ODataReaderState.FeedEnd) { Assert.NotNull(reader.Item as ODataFeed); } } Assert.Equal(ODataReaderState.Completed, reader.State); } } } }
public void TestBasic(string method, string requestUri) { var client = new HttpClient(); var arg = new DataServiceClientRequestMessageArgs(method, new Uri(requestUri), true, true, new Dictionary <string, string>()); var message = new HttpClientRequestMessage(client, arg, new DataServiceContext().Configurations); message.Method.Should().Be(method); message.Url.Should().Be(requestUri); }
public ShimDataServiceClientRequestMessage(ShimController controller, DataServiceClientRequestMessageArgs args) : base(args.Method) { OriginalMessageArgs = args; ShimWebRequest = new ShimDataRequestMessage(args); _controller = controller; }
public void SetUserAgentShouldSucceed() { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.svc"); IDictionary <string, string> Headers = new Dictionary <string, string>(); DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs(request.Method.ToString(), request.RequestUri, true, false, Headers); new HttpClientRequestMessage(args).SetHeader(XmlConstants.HttpUserAgent, "MyUserAgent"); new HttpWebRequestMessage(args).SetHeader(XmlConstants.HttpUserAgent, "MyUserAgent"); }
public void SetContentLengthShouldSucceed() { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.svc"); IDictionary <string, string> Headers = new Dictionary <string, string>(); DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs(request.Method.ToString(), request.RequestUri, true, false, Headers); new HttpClientRequestMessage(args).SetHeader(XmlConstants.HttpContentLength, 1.ToString(CultureInfo.InvariantCulture.NumberFormat)); new HttpWebRequestMessage(args).SetHeader(XmlConstants.HttpContentLength, 1.ToString(CultureInfo.InvariantCulture.NumberFormat)); }
public ShimDataServiceClientRequestMessage(ShimController controller, DataServiceClientRequestMessageArgs args) : base(args.Method) { OriginalMessageArgs = args; ShimWebRequest = new ShimDataRequestMessage(args); _controller = controller; }
public void SetAcceptCharsetShouldNotBeSetOnSilverlightAndSetOnOtherPlatforms() { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.svc"); IDictionary <string, string> Headers = new Dictionary <string, string>(); DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs(request.Method.ToString(), request.RequestUri, true, false, Headers); new HttpClientRequestMessage(args).SetHeader(XmlConstants.HttpAcceptCharset, "utf8"); new HttpWebRequestMessage(args).SetHeader(XmlConstants.HttpAcceptCharset, "utf8"); }
private static HttpWebRequest CreateRequest(string method, Uri requestUrl, DataServiceClientRequestMessageArgs args) { HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(requestUrl); // KeepAlive not available httpRequest.KeepAlive = true; httpRequest.Method = method; return(httpRequest); }
/// <summary> /// A Factory class to use in selecting the implementation to use depending on the /// </summary> /// <param name="args"></param> /// <param name="dataServiceContext"></param> /// <returns></returns> public DataServiceClientRequestMessage CreateRequestMessage(DataServiceClientRequestMessageArgs args, DataServiceContext dataServiceContext) { if (dataServiceContext.HttpRequestTransportMode == HttpRequestTransportMode.HttpWebRequest) { return(new HttpWebRequestMessage(args)); } else { return(new HttpClientRequestMessage(args)); } }
public CustomizedRequestMessage(DataServiceClientRequestMessageArgs args, ConnectionProperties properties) : base(args) { HttpWebRequest.Proxy = properties.GetWebProxy(); HttpWebRequest.Headers.Add(properties.GetCustomHeaders()); var cert = properties.GetClientCertificate(); if (cert != null) { HttpWebRequest.ClientCertificates.Add(cert); } }
DataServiceClientRequestMessage ctx_OnMessageCreating(DataServiceClientRequestMessageArgs args) { if (_workaroundDateTimeQuery != null) { args = new DataServiceClientRequestMessageArgs("GET", new Uri(_workaroundDateTimeQuery), false, false, new Dictionary <string, string>()); _workaroundDateTimeQuery = null; } HttpClientRequestMessage clientRequestMessage = new HttpClientRequestMessage(args); clientRequestMessage.Credentials = CredentialCache.DefaultCredentials; return(clientRequestMessage); }
public TestHttpWebRequestMessage( DataServiceClientRequestMessageArgs args, Action <HttpWebRequestMessage> sendRequest, Action <HttpWebResponseMessage> sendResponse, Func <Stream, Stream> wrapRequestStream, Func <Stream, Stream> wrapResponseStream) : base(args) { this.SendRequest = sendRequest; this.SendResponse = sendResponse; this.WrapRequestStream = wrapRequestStream; this.WrapResponseStream = wrapResponseStream; }
DataServiceClientRequestMessage ctx_OnMessageCreating(DataServiceClientRequestMessageArgs args) { if (_workaroundDateTimeQuery != null) { args = new DataServiceClientRequestMessageArgs("GET", new Uri(_workaroundDateTimeQuery), false, false, new Dictionary <string, string>()); _workaroundDateTimeQuery = null; } HttpWebRequestMessage webRequestMessage = new HttpWebRequestMessage(args); webRequestMessage.HttpWebRequest.UseDefaultCredentials = true; return(webRequestMessage); }
protected virtual InMemoryMessage CreateRequestMessage(DataServiceClientRequestMessageArgs requestMessageArgs) { var requestMessage = new InMemoryMessage { Url = requestMessageArgs.RequestUri, Method = requestMessageArgs.Method, Stream = new MemoryStream() }; foreach (var header in requestMessageArgs.Headers) { requestMessage.SetHeader(header.Key, header.Value); } return(requestMessage); }
/// <summary> /// A Factory class to use in selecting the implementation to use depending on the /// </summary> /// <param name="args"></param> /// <param name="dataServiceContext"></param> /// <returns></returns> public DataServiceClientRequestMessage CreateRequestMessage(DataServiceClientRequestMessageArgs args, DataServiceContext dataServiceContext) { if (dataServiceContext.HttpRequestTransportMode == HttpRequestTransportMode.HttpWebRequest) { #pragma warning disable CS0618 // Type or member is obsolete return(new HttpWebRequestMessage(args)); #pragma warning restore CS0618 // Type or member is obsolete } else { return(new HttpClientRequestMessage(args)); } }
/// <summary> /// Creates an instance of the IODataRequestMessage /// </summary> /// <param name="requestMessageArgs">Arguments for creating the request message.</param> /// <returns>an instance of the IODataRequestMessage </returns> internal DataServiceClientRequestMessage CreateMessage(DataServiceClientRequestMessageArgs requestMessageArgs) { var requestMessage = this.getRequestMessage(); requestMessage.Url = requestMessageArgs.RequestUri; requestMessage.Method = requestMessageArgs.Method; foreach (var header in requestMessageArgs.Headers) { requestMessage.SetHeader(header.Key, header.Value); } return(new TestDataServiceClientRequestMessage(requestMessage, getResponseMessage)); }
internal DataServiceClientRequestMessage ShimDataServiceRequest(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; if (_dataServiceHandler != null) { message = _dataServiceHandler(args); } else { message = new HttpWebRequestMessage(args); } return(message); }
/// <summary> /// Constructor for HttpClientRequestMessage. /// Initializes the <see cref="HttpClientRequestMessage"/> class. /// The args.ActualMethod is the actual method. In post tunneling situations method will be POST instead of the specified verb method. /// The args.method is the specified verb method /// </summary> /// </summary> public HttpClientRequestMessage(DataServiceClientRequestMessageArgs args) : base(args.ActualMethod) { _messageStream = new MemoryStream(); _handler = new HttpClientHandler(); _client = new HttpClient(_handler, disposeHandler: true); _contentHeaderValueCache = new Dictionary <string, string>(); _effectiveHttpMethod = args.Method; _requestUrl = args.RequestUri; _requestMessage = new HttpRequestMessage(new HttpMethod(this.ActualMethod), _requestUrl); // Now set the headers. foreach (KeyValuePair <string, string> keyValue in args.Headers) { this.SetHeader(keyValue.Key, keyValue.Value); } }
internal DataServiceClientRequestMessage ShimDataServiceRequest(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; if (_dataServiceHandler != null) { message = _dataServiceHandler(args); } else { message = new HttpWebRequestMessage(args); } // apply proxy and credential settings on the core web request InitializeMessage(message); return message; }
internal DataServiceClientRequestMessage ShimDataServiceRequest(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; if (_dataServiceHandler != null) { message = _dataServiceHandler(args); } else { message = new HttpWebRequestMessage(args); } // apply proxy and credential settings on the core web request InitializeMessage(message); return(message); }
public void HttpMergeIsNoLongerSupported() { Func <DataServiceClientRequestMessageArgs, DataServiceClientRequestMessage> addHeader = (args) => { if (args.Method == "PATCH") { // use Merge var newArgs = new DataServiceClientRequestMessageArgs("MERGE", args.RequestUri, true, args.UsePostTunneling, args.Headers); // use V4 since Merge is removed only in V4 newArgs.Headers["DataServiceVersion"] = "4.0"; return(new HttpWebRequestMessage(newArgs)); } return(new HttpWebRequestMessage(args)); }; var ctx = this.CreateContext(addHeader); Product product = null; var query = ctx.Context.Product.Take(1) as DataServiceQuery <Product>; var ar = query.BeginExecute(null, null).EnqueueWait(this); product = query.EndExecute(ar).Single(); product.Description = "New Description " + Guid.NewGuid().ToString(); ctx.UpdateObject(product); try { var ar2 = ctx.BeginSaveChanges(null, null).EnqueueWait(this); ctx.EndSaveChanges(ar2); Assert.Fail("Expected error not thrown"); } catch (DataServiceRequestException e) { #if !PORTABLELIB && !SILVERLIGHT StringResourceUtil.VerifyDataServicesClientString(e.Message, "DataServiceException_GeneralError"); #else Assert.IsNotNull(e); #endif } }
public HttpWebRequestMessage(DataServiceClientRequestMessageArgs args) : base(args.ActualMethod) { Util.CheckArgumentNull(args, "args"); Debug.Assert(args.RequestUri.IsAbsoluteUri, "request uri is not absolute uri"); Debug.Assert( args.RequestUri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) || args.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase), "request uri is not for HTTP"); this.effectiveHttpMethod = args.Method; this.requestUrl = args.RequestUri; this.httpRequest = HttpWebRequestMessage.CreateRequest(this.ActualMethod, this.Url, args); // Now set the headers. foreach (var keyValue in args.Headers) { this.SetHeader(keyValue.Key, keyValue.Value); } }
public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri); if (dispatcher != null && dispatcher.Initialized == true) { // Let the interceptor handle this message = new ShimDataServiceClientRequestMessage(this, args); } // If no interceptors want the message create a normal HttpWebRequestMessage if (message == null) { V3InteropTraceSources.ShimController.Verbose("request", "{0} {1}", args.Method, args.RequestUri.AbsoluteUri); message = new HttpWebRequestMessage(args); } return(message); }
/// <summary> /// Entry point for requests from OData. /// </summary> public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri); if (dispatcher != null && dispatcher.Initialized == true) { // Let the interceptor handle this message = new ShimDataServiceClientRequestMessage(this, args); } // If no interceptors want the message create a normal HttpWebRequestMessage if (message == null) { Log(String.Format(CultureInfo.InvariantCulture, "[V2 REQ] {0}", args.RequestUri.AbsoluteUri), ConsoleColor.Gray); message = new HttpWebRequestMessage(args); } return(message); }
public void ChangeMergeToPatch() { Func <DataServiceClientRequestMessageArgs, DataServiceClientRequestMessage> addHeader = (args) => { if (args.Method == "MERGE") { var newArgs = new DataServiceClientRequestMessageArgs("PATCH", args.RequestUri, true, args.UsePostTunneling, args.Headers); // PATCH verb not supported in V1 or V2 newArgs.Headers.Remove("DataServiceVersion"); newArgs.Headers.Add("DataServiceVersion", "3.0"); return(new HttpWebRequestMessage(newArgs)); } return(new HttpWebRequestMessage(args)); }; var ctx = this.CreateContext(addHeader); Product product = null; var query = ctx.Context.Product.Take(1) as DataServiceQuery <Product>; var ar = query.BeginExecute(null, null).EnqueueWait(this); product = query.EndExecute(ar).Single(); this.lastRequestMessage = null; product.Description = "New Description " + Guid.NewGuid().ToString(); ctx.UpdateObject(product); var ar2 = ctx.BeginSaveChanges(null, null).EnqueueWait(this); ctx.EndSaveChanges(ar2); Assert.IsNotNull(this.lastRequestMessage, "No request sent"); Assert.AreEqual("PATCH", this.lastRequestMessage.Method); }
public ShimDataRequestMessage(DataServiceClientRequestMessageArgs args) { WebRequest = ShimWebHelpers.AddHeaders(HttpWebRequest.CreateHttp(args.RequestUri), args.Headers); WebRequest.Method = args.Method; }
public CustomizedRequestMessage(DataServiceClientRequestMessageArgs args, IWebProxy webProxy) : base(args) { HttpWebRequest.Proxy = webProxy; }
public AnnotationRequestMessage(DataServiceClientRequestMessageArgs args, string response, Dictionary<string, string> headers) : base(args) { this.Response = response; this.CutomizedHeaders = headers; }
public EntityParameterRequestMessage(DataServiceClientRequestMessageArgs args, byte[] buffer) : base(args) { this.stream = new MemoryStream(buffer); this.headers = new HeaderCollection(); }
public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri); if (dispatcher != null && dispatcher.Initialized == true) { // Let the interceptor handle this message = new ShimDataServiceClientRequestMessage(this, args); } // If no interceptors want the message create a normal HttpWebRequestMessage if (message == null) { V3InteropTraceSources.ShimController.Verbose("request", "{0} {1}", args.Method, args.RequestUri.AbsoluteUri); message = new HttpWebRequestMessage(args); } return message; }
/// <summary> /// Entry point for requests from OData. /// </summary> public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args) { DataServiceClientRequestMessage message = null; InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri); if (dispatcher != null && dispatcher.Initialized == true) { // Let the interceptor handle this message = new ShimDataServiceClientRequestMessage(this, args); } // If no interceptors want the message create a normal HttpWebRequestMessage if (message == null) { Log(String.Format(CultureInfo.InvariantCulture, "[V2 REQ] {0}", args.RequestUri.AbsoluteUri), ConsoleColor.Gray); message = new HttpWebRequestMessage(args); } return message; }