Skip to content

Commit

Permalink
#30: added support for PROPFIND request with the 'prop' element
Browse files Browse the repository at this point in the history
  • Loading branch information
skazantsev committed Aug 18, 2018
1 parent 45b4db6 commit a333bd6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/WebDav.Client.Tests/Methods/PropfindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,29 @@ public async void When_IsCalledWithPrefixedNamespaces_Should_IncludeThemInReques
await dispatcher.Received(1)
.Send(Arg.Any<Uri>(), WebDavMethod.Propfind, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
}

[Fact]
public async void When_NamedRequestIsCalled_Should_SendPropRequest()
{
const string expectedContent =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<D:propfind xmlns:D=""DAV:"">
<D:prop xmlns:P1=""http://p1.example.com"">
<D:displayname />
<P1:myprop1 />
</D:prop>
</D:propfind>";
var dispatcher = Dispatcher.Mock();
var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

await client.Propfind("http://example.com", new PropfindParameters
{
RequestType = PropfindRequestType.NamedProperties,
CustomProperties = new XName[] { "{DAV:}displayname", "{http://p1.example.com}myprop1" },
Namespaces = new[] { new NamespaceAttr("P1", "http://p1.example.com") }
});
await dispatcher.Received(1)
.Send(Arg.Any<Uri>(), WebDavMethod.Propfind, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
}
}
}
13 changes: 13 additions & 0 deletions src/WebDav.Client/Core/PropfindRequestType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace WebDav
{
/// <summary>
/// Specifies a type of PROPFIND request.
/// AllProperties: 'allprop' + 'include'.
/// NamedProperties: 'prop'.
/// </summary>
public enum PropfindRequestType
{
AllProperties,
NamedProperties
}
}
8 changes: 8 additions & 0 deletions src/WebDav.Client/Request/PropfindParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ public class PropfindParameters
/// </summary>
public PropfindParameters()
{
RequestType = PropfindRequestType.AllProperties;
CustomProperties = new List<XName>();
Namespaces = new List<NamespaceAttr>();
Headers = new List<KeyValuePair<string, string>>();
CancellationToken = CancellationToken.None;
}

/// <summary>
/// Gets or sets a type of PROPFIND request.
/// AllProperties: 'allprop' + 'include'.
/// NamedProperties: 'prop'.
/// </summary>
public PropfindRequestType RequestType { get; set; }

/// <summary>
/// Gets or sets the collection of custom properties (or dead properties in terms of WebDav).
/// </summary>
Expand Down
36 changes: 34 additions & 2 deletions src/WebDav.Client/Request/PropfindRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ namespace WebDav
{
internal static class PropfindRequestBuilder
{
public static string BuildRequestBody(IReadOnlyCollection<XName> customProperties, IReadOnlyCollection<NamespaceAttr> namespaces)
{
public static string BuildRequest(
PropfindRequestType requestType,
IReadOnlyCollection<XName> customProperties,
IReadOnlyCollection<NamespaceAttr> namespaces)
{
return requestType == PropfindRequestType.NamedProperties
? BuildNamedPropRequest(customProperties, namespaces)
: BuildAllPropRequest(customProperties, namespaces);
}

private static string BuildAllPropRequest(IReadOnlyCollection<XName> customProperties, IReadOnlyCollection<NamespaceAttr> namespaces)
{
var doc = new XDocument(new XDeclaration("1.0", "utf-8", null));
var propfind = new XElement("{DAV:}propfind", new XAttribute(XNamespace.Xmlns + "D", "DAV:"));
propfind.Add(new XElement("{DAV:}allprop"));
Expand All @@ -28,5 +38,27 @@ public static string BuildRequestBody(IReadOnlyCollection<XName> customPropertie
doc.Add(propfind);
return doc.ToStringWithDeclaration();
}

private static string BuildNamedPropRequest(IReadOnlyCollection<XName> customProperties, IReadOnlyCollection<NamespaceAttr> namespaces)
{
var doc = new XDocument(new XDeclaration("1.0", "utf-8", null));
var propfind = new XElement("{DAV:}propfind", new XAttribute(XNamespace.Xmlns + "D", "DAV:"));
if (customProperties.Any())
{
var propEl = new XElement("{DAV:}prop");
foreach (var ns in namespaces)
{
var nsAttr = string.IsNullOrEmpty(ns.Prefix) ? "xmlns" : XNamespace.Xmlns + ns.Prefix;
propEl.SetAttributeValue(nsAttr, ns.Namespace);
}
foreach (var prop in customProperties)
{
propEl.Add(new XElement(prop));
}
propfind.Add(propEl);
}
doc.Add(propfind);
return doc.ToStringWithDeclaration();
}
}
}
2 changes: 1 addition & 1 deletion src/WebDav.Client/WebDavClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task<PropfindResponse> Propfind(Uri requestUri, PropfindParameters
.AddWithOverwrite(parameters.Headers)
.Build();

var requestBody = PropfindRequestBuilder.BuildRequestBody(parameters.CustomProperties, parameters.Namespaces);
var requestBody = PropfindRequestBuilder.BuildRequest(parameters.RequestType, parameters.CustomProperties, parameters.Namespaces);
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody) };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Propfind, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
Expand Down

0 comments on commit a333bd6

Please sign in to comment.