public void Assign_Ampersand_In_Query_Test() { var uri = new SDataUri("http://localhost/sdata/aw/dynamic/-/accounts") {Query = "a=%26&b=%26"}; Assert.That(uri.Query, Is.EqualTo("a=%26&b=%26")); Assert.That(uri["a"], Is.EqualTo("&")); Assert.That(uri["b"], Is.EqualTo("&")); }
public void Appending_Segments_To_Specific_Service_Urls_Test() { var uri = new SDataUri("http://test.com/sdata/-/-/-/resource/$service/name"); uri.AppendPath("test"); Assert.AreEqual("resource", uri.GetPathSegment(4).Text); Assert.AreEqual("name", uri.GetPathSegment(6).Text); Assert.AreEqual("sdata/-/-/-/resource/$service/name/test", uri.DirectPath); Assert.AreEqual("http://test.com/sdata/-/-/-/resource/$service/name/test", uri.ToString()); }
private void UpdateUrl() { var uri = new SDataUri(Client.Uri); if (!string.IsNullOrEmpty(tbTemplateResourceKind.Text)) { uri.AppendPath(tbTemplateResourceKind.Text); } uri.AppendPath("$template"); tbTemplateURL.Text = uri.ToString(); }
public override void Save(Stream stream) { if (stream == null) { _attachment = string.IsNullOrEmpty(_attachment.Key) ? _client.Post(_attachment) : _client.Put(_attachment); return; } var parms = new SDataParameters { Files = {new AttachedFile(null, _attachment.FileName, stream)} }; if (string.IsNullOrEmpty(_attachment.Key)) { parms.Method = HttpMethod.Post; parms.Path = "attachments"; } else { parms.Method = HttpMethod.Put; parms.Path = "attachments(" + SDataUri.FormatConstant(_attachment.Key) + ")"; parms.ETag = _attachment.ETag; } if (_formMode) { foreach (var prop in typeof (Attachment).GetProperties()) { var name = _client.NamingScheme.GetName(prop); if (!name.StartsWith("$", StringComparison.Ordinal) && !new[] {"physicalFileName", "fileExists", "fileSize", "createDate", "createUser", "modifyDate", "modifyUser"}.Contains(name, StringComparer.Ordinal)) { var value = prop.GetValue(_attachment, null); if (value != null) { parms.Form[name] = value.ToString(); } } } parms.Path += "/file"; var results = _client.Execute(parms); if (!string.IsNullOrEmpty(results.Location)) { var selector = new SDataUri(results.Location).GetPathSegment(4).Selector; _attachment.Key = selector.Substring(1, selector.Length - 2); _attachment.ETag = results.ETag; } } else { parms.Content = _attachment; _attachment = _client.Execute<Attachment>(parms).Content; } }
private void UpdateUrl() { var uri = new SDataUri(Client.Uri); if (!string.IsNullOrEmpty(tbSingleResourceKind.Text)) { var selector = tbSingleResourceSelector.Text; if (!string.IsNullOrEmpty(selector)) { selector = SDataUri.FormatConstant(selector); } uri.AppendPath(new UriPathSegment(tbSingleResourceKind.Text, selector)); } if (!string.IsNullOrEmpty(tbSingleResourceInclude.Text)) { uri.Include = tbSingleResourceInclude.Text; } tbSingleURL.Text = uri.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="SDataUri"/> class with /// the specified <see cref="Uri"/>. /// </summary> /// <param name="uri">The <see cref="Uri"/> to assign.</param> public SDataUri(SDataUri uri) : base(uri) { }
private void UpdateUrl() { var uri = new SDataUri(Client.Uri) { StartIndex = (int) numStartIndex.Value, Count = (int) numCount.Value }; if (!string.IsNullOrEmpty(tbCollectionResourceKind.Text)) { uri.AppendPath(tbCollectionResourceKind.Text); } tbCollectionURL.Text = uri.ToString(); }
public void Non_Standard_Parameters_Should_Have_Underscore_Prefix_Test() { var uri = new SDataUri("http://test.com/sdata/-/-/-/resource?_includeContent=true"); Assert.IsTrue(uri["_includeContent"] == "true"); }
public void Execute_Batch_Test() { var requestMock = new Mock<SDataRequest>(null, null, null); requestMock.Setup(x => x.GetResponse()).Returns(new SDataResponse(HttpStatusCode.OK, null, null, null, null, null, null, null, null)); SDataUri requestUri = null; var requestFactory = new Func<string, SDataRequest>(uri => { requestUri = new SDataUri(uri); return requestMock.Object; }); var client = new SDataClient("test://dummy", requestFactory); var file1 = new AttachedFile(null, null, null); var params1 = new SDataParameters { Include = "include1", Select = "select1", Precedence = 1, Format = MediaType.ImagePng, Language = "language", Version = "version", Path = "path", ExtensionArgs = {{"foo1", "bar1"}}, Method = HttpMethod.Post, Content = new object(), ContentType = MediaType.ImageTiff, ETag = "etag1", Form = {{"hello1", "world1"}}, Files = {file1}, Accept = new[] {MediaType.ImageJpeg} }; var file2 = new AttachedFile(null, null, null); var resource2 = new SDataResource {Key = "key2"}; resource2["foo"] = "bar"; var params2 = new SDataParameters { Include = "include2", Select = "select2", Precedence = 2, Format = MediaType.ImagePng, Language = "language", Version = "version", Path = "path", ExtensionArgs = {{"foo2", "bar2"}}, Method = HttpMethod.Put, Content = resource2, ContentType = MediaType.ImageTiff, ETag = "etag2", Form = {{"hello2", "world2"}}, Files = {file2}, Accept = new[] {MediaType.Css} }; client.ExecuteBatch<SDataResource>(new[] {params1, params2}); var resources = requestMock.Object.Content as IList<SDataResource>; Assert.That(resources, Is.Not.Null); Assert.That(requestUri, Is.Not.Null); Assert.That(requestUri.Include, Is.EqualTo("include1,include2")); Assert.That(requestUri.Select, Is.EqualTo("select1,select2")); Assert.That(requestUri.Precedence, Is.EqualTo(2)); Assert.That(requestUri.Format, Is.EqualTo(MediaType.ImagePng)); Assert.That(requestUri.Language, Is.EqualTo("language")); Assert.That(requestUri.Version, Is.EqualTo("version")); Assert.That(requestUri.Path, Is.EqualTo("path/$batch")); Assert.That(requestUri["_foo1"], Is.EqualTo("bar1")); Assert.That(requestUri["_foo2"], Is.EqualTo("bar2")); Assert.That(requestMock.Object.Method, Is.EqualTo(HttpMethod.Post)); Assert.That(resources[0].HttpMethod, Is.EqualTo(HttpMethod.Post)); Assert.That(resources[1].HttpMethod, Is.EqualTo(HttpMethod.Put)); Assert.That(resources[0].Url, Is.Null); Assert.That(resources[1].Url, Is.EqualTo(new Uri("test://dummy/path('key2')?include=include2&select=select2&precedence=2"))); Assert.That(requestMock.Object.ContentType, Is.EqualTo(MediaType.ImageTiff)); Assert.That(resources[0].IfMatch, Is.EqualTo("etag1")); Assert.That(resources[1].IfMatch, Is.EqualTo("etag2")); Assert.That(requestMock.Object.Form["hello1"], Is.EqualTo("world1")); Assert.That(requestMock.Object.Form["hello2"], Is.EqualTo("world2")); Assert.That(requestMock.Object.Files[0], Is.EqualTo(file1)); Assert.That(requestMock.Object.Files[1], Is.EqualTo(file2)); Assert.That(requestMock.Object.Accept, Is.EqualTo(new[] {MediaType.ImageJpeg, MediaType.Css})); Assert.That(requestMock.Object.AcceptLanguage, Is.EqualTo("language")); }
public void Execute_Test() { var requestMock = new Mock<SDataRequest>(null, null, null); requestMock.Setup(x => x.GetResponse()).Returns(new SDataResponse(HttpStatusCode.OK, null, null, null, null, null, null, null, null)); SDataUri requestUri = null; var requestFactory = new Func<string, SDataRequest>(uri => { requestUri = new SDataUri(uri); return requestMock.Object; }); var client = new SDataClient("test://dummy", requestFactory); var content = new object(); var file = new AttachedFile(null, null, null); var parms = new SDataParameters { StartIndex = 1, Count = 2, Where = "where", OrderBy = "order_by", Search = "search", Include = "include", Select = "select", Precedence = 3, IncludeSchema = true, ReturnDelta = true, TrackingId = "tracking_id", Format = MediaType.ImagePng, Language = "language", Version = "version", Path = "path", ExtensionArgs = {{"foo", "bar"}}, Method = HttpMethod.Put, Content = content, ContentType = MediaType.ImageTiff, ETag = "etag", Form = {{"hello", "world"}}, Files = {file}, Accept = new[] {MediaType.ImageJpeg} }; client.Execute(parms); Assert.That(requestUri, Is.Not.Null); Assert.That(requestUri.StartIndex, Is.EqualTo(1)); Assert.That(requestUri.Count, Is.EqualTo(2)); Assert.That(requestUri.Where, Is.EqualTo("where")); Assert.That(requestUri.OrderBy, Is.EqualTo("order_by")); Assert.That(requestUri.Search, Is.EqualTo("search")); Assert.That(requestUri.Include, Is.EqualTo("include")); Assert.That(requestUri.Select, Is.EqualTo("select")); Assert.That(requestUri.Precedence, Is.EqualTo(3)); Assert.That(requestUri.IncludeSchema, Is.EqualTo(true)); Assert.That(requestUri.ReturnDelta, Is.EqualTo(true)); Assert.That(requestUri.TrackingId, Is.EqualTo("tracking_id")); Assert.That(requestUri.Format, Is.EqualTo(MediaType.ImagePng)); Assert.That(requestUri.Language, Is.EqualTo("language")); Assert.That(requestUri.Version, Is.EqualTo("version")); Assert.That(requestUri.Path, Is.EqualTo("path")); Assert.That(requestUri["_foo"], Is.EqualTo("bar")); Assert.That(requestMock.Object.Method, Is.EqualTo(HttpMethod.Put)); Assert.That(requestMock.Object.Content, Is.EqualTo(content)); Assert.That(requestMock.Object.ContentType, Is.EqualTo(MediaType.ImageTiff)); Assert.That(requestMock.Object.ETag, Is.EqualTo("etag")); Assert.That(requestMock.Object.Form["hello"], Is.EqualTo("world")); Assert.That(requestMock.Object.Files[0], Is.EqualTo(file)); Assert.That(requestMock.Object.Accept, Is.EqualTo(new[] {MediaType.ImageJpeg})); Assert.That(requestMock.Object.AcceptLanguage, Is.EqualTo("language")); }
private WebRequest CreateRequest(string uri, out MediaType?contentType, out object content) { bool methodOverride; if (Method == HttpMethod.Get && uri.Length > MaxGetUriLength) { methodOverride = true; contentType = MediaType.Text; content = uri; uri = new SDataUri(uri) { Query = null }.ToString(); } else { methodOverride = (UseHttpMethodOverride && Method != HttpMethod.Get && Method != HttpMethod.Post); contentType = ContentType; content = Content; } var request = WebRequest.Create(uri); if (methodOverride) { request.Method = "POST"; request.Headers["X-HTTP-Method-Override"] = Method.ToString().ToUpperInvariant(); } else { request.Method = Method.ToString().ToUpperInvariant(); } #if PCL try { var preAuthProp = request.GetType().GetProperty("PreAuthenticate"); if (preAuthProp != null && preAuthProp.CanWrite) { preAuthProp.SetValue(request, true, null); } } catch { } #endif #if !PCL && !NETFX_CORE && !SILVERLIGHT request.Timeout = Timeout; request.PreAuthenticate = true; #endif #if !PCL && !SILVERLIGHT if (_proxySet) { request.Proxy = Proxy; } #endif var httpRequest = request as HttpWebRequest; if (httpRequest != null) { #if !PCL && !NETFX_CORE && !SILVERLIGHT httpRequest.AllowAutoRedirect = false; httpRequest.ReadWriteTimeout = Timeout; httpRequest.KeepAlive = false; httpRequest.ProtocolVersion = HttpVersion.Version10; if (UserAgent != null) { httpRequest.UserAgent = UserAgent; } #endif if (Accept != null) { httpRequest.Accept = string.Join(",", Accept.Select(MediaTypeNames.GetMediaType).ToArray()); } if (Cookies != null) { httpRequest.CookieContainer = Cookies; } } if (AcceptLanguage != null) { request.Headers[HttpRequestHeader.AcceptLanguage] = AcceptLanguage; } if (Authenticator != null) { Authenticator.Authenticate(request); } else if (Credentials != null) { request.Credentials = Credentials; } else if (UserName != null || Password != null) { request.Credentials = new NetworkCredential(UserName, Password); } else { request.UseDefaultCredentials = true; } if (ETag != null) { var header = Method == HttpMethod.Get ? HttpRequestHeader.IfNoneMatch : HttpRequestHeader.IfMatch; request.Headers[header] = ETag; } return(request); }