Skip to content

Commit

Permalink
Fixing #56: (#57)
Browse files Browse the repository at this point in the history
- making application/xml a default content-type for PROPFIND, PROPPATCH, LOCK and allowing to override it.
- changing ContentType type of PutFileParameters from string to MediaTypeHeaderValue.
  • Loading branch information
skazantsev authored May 31, 2020
1 parent b1886fe commit f930f8c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/WebDav.Client.Tests/Methods/PutFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public async void When_IsCalledWithContentType_Should_SetPassItToDispatcher()
var dispatcher = Dispatcher.Mock();
var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

await client.PutFile(requestUri, stream, new PutFileParameters { ContentType = "text/xml" });
await client.PutFile(requestUri, stream, new PutFileParameters { ContentType = new MediaTypeHeaderValue("text/xml") });
await dispatcher.Received(1)
.Send(requestUri, HttpMethod.Put, Arg.Is<RequestParameters>(x => x.ContentType == "text/xml"), CancellationToken.None);
.Send(requestUri, HttpMethod.Put, Arg.Is<RequestParameters>(x => x.ContentType.MediaType == "text/xml"), CancellationToken.None);
}
}
}
15 changes: 15 additions & 0 deletions src/WebDav.Client/Core/MediaTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Net.Http.Headers;
using System.Text;

namespace WebDav.Client.Core
{
internal static class MediaTypes
{
internal static readonly MediaTypeHeaderValue XmlMediaType = new MediaTypeHeaderValue("application/xml")
{
CharSet = Encoding.UTF8.WebName
};

internal static readonly MediaTypeHeaderValue BinaryDataMediaType = new MediaTypeHeaderValue("application/octet-stream");
}
}
7 changes: 5 additions & 2 deletions src/WebDav.Client/Core/WebDavDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -47,8 +48,10 @@ public async Task<HttpResponseMessage> Send(
if (requestParams.Content != null)
{
request.Content = requestParams.Content;
if (!string.IsNullOrEmpty(requestParams.ContentType))
request.Content.Headers.ContentType = new MediaTypeHeaderValue(requestParams.ContentType);
if (requestParams.ContentType != null)
{
request.Content.Headers.ContentType = requestParams.ContentType;
}
}

var response = await _httpClient.SendAsync(request, httpCompletionOption, cancellationToken).ConfigureAwait(false);
Expand Down
9 changes: 9 additions & 0 deletions src/WebDav.Client/Request/LockParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
using WebDav.Client.Core;

namespace WebDav
{
Expand All @@ -14,6 +16,7 @@ public class LockParameters
/// </summary>
public LockParameters()
{
ContentType = MediaTypes.XmlMediaType;
Headers = new List<KeyValuePair<string, string>>();
CancellationToken = CancellationToken.None;
}
Expand All @@ -39,6 +42,12 @@ public LockParameters()
/// </summary>
public LockOwner Owner { get; set; }

/// <summary>
/// Gets or sets the content type of the request body.
/// The default value is application/xml; charset=utf-8.
/// </summary>
public MediaTypeHeaderValue ContentType { get; set; }

/// <summary>
/// Gets or sets the collection of http request headers.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/WebDav.Client/Request/PropfindParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
using System.Xml.Linq;
using WebDav.Client.Core;

namespace WebDav
{
Expand All @@ -17,6 +19,7 @@ public PropfindParameters()
RequestType = PropfindRequestType.AllProperties;
CustomProperties = new List<XName>();
Namespaces = new List<NamespaceAttr>();
ContentType = MediaTypes.XmlMediaType;
Headers = new List<KeyValuePair<string, string>>();
CancellationToken = CancellationToken.None;
}
Expand Down Expand Up @@ -44,6 +47,12 @@ public PropfindParameters()
/// </summary>
public ApplyTo.Propfind? ApplyTo { get; set; }

/// <summary>
/// Gets or sets the content type of the request body.
/// The default value is application/xml; charset=utf-8.
/// </summary>
public MediaTypeHeaderValue ContentType { get; set; }

/// <summary>
/// Gets or sets the collection of http request headers.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/WebDav.Client/Request/ProppatchParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
using System.Xml.Linq;
using WebDav.Client.Core;

namespace WebDav
{
Expand All @@ -17,6 +19,7 @@ public ProppatchParameters()
PropertiesToSet = new Dictionary<XName, string>();
PropertiesToRemove = new List<XName>();
Namespaces = new List<NamespaceAttr>();
ContentType = MediaTypes.XmlMediaType;
Headers = new List<KeyValuePair<string, string>>();
CancellationToken = CancellationToken.None;
}
Expand All @@ -36,6 +39,12 @@ public ProppatchParameters()
/// </summary>
public IReadOnlyCollection<NamespaceAttr> Namespaces { get; set; }

/// <summary>
/// Gets or sets the content type of the request body.
/// The default value is application/xml; charset=utf-8.
/// </summary>
public MediaTypeHeaderValue ContentType { get; set; }

/// <summary>
/// Gets or sets the collection of http request headers.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions src/WebDav.Client/Request/PutFileParameters.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
using WebDav.Client.Core;

namespace WebDav
{
Expand All @@ -13,7 +15,7 @@ public class PutFileParameters
/// </summary>
public PutFileParameters()
{
ContentType = "application/octet-stream";
ContentType = MediaTypes.BinaryDataMediaType;
Headers = new List<KeyValuePair<string, string>>();
CancellationToken = CancellationToken.None;
}
Expand All @@ -22,7 +24,7 @@ public PutFileParameters()
/// Gets or sets the content type of the request body.
/// The default value is application/octet-stream.
/// </summary>
public string ContentType { get; set; }
public MediaTypeHeaderValue ContentType { get; set; }

/// <summary>
/// Gets or sets the resource lock token.
Expand Down
3 changes: 2 additions & 1 deletion src/WebDav.Client/Request/RequestParameters.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;

namespace WebDav
{
Expand All @@ -14,6 +15,6 @@ public RequestParameters()

public HttpContent Content { get; set; }

public string ContentType { get; set; }
public MediaTypeHeaderValue ContentType { get; set; }
}
}
13 changes: 8 additions & 5 deletions src/WebDav.Client/WebDavClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -107,7 +108,7 @@ public async Task<PropfindResponse> Propfind(Uri requestUri, PropfindParameters
? null
: new StringContent(PropfindRequestBuilder.BuildRequest(parameters.RequestType, parameters.CustomProperties, parameters.Namespaces));

var requestParams = new RequestParameters { Headers = headers, Content = requestBody };
var requestParams = new RequestParameters { Headers = headers, Content = requestBody, ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Propfind, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
Expand Down Expand Up @@ -143,7 +144,7 @@ public async Task<ProppatchResponse> Proppatch(Uri requestUri, ProppatchParamete
parameters.PropertiesToSet,
parameters.PropertiesToRemove,
parameters.Namespaces);
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody) };
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Proppatch, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _proppatchResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
Expand Down Expand Up @@ -401,7 +402,8 @@ public Task<WebDavResponse> PutFile(Uri requestUri, Stream stream)
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, Stream stream, string contentType)
{
return PutFile(CreateUri(requestUri), new StreamContent(stream), new PutFileParameters { ContentType = contentType });
var @params = new PutFileParameters { ContentType = new MediaTypeHeaderValue(contentType) };
return PutFile(CreateUri(requestUri), new StreamContent(stream), @params);
}

/// <summary>
Expand All @@ -413,7 +415,8 @@ public Task<WebDavResponse> PutFile(string requestUri, Stream stream, string con
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(Uri requestUri, Stream stream, string contentType)
{
return PutFile(requestUri, new StreamContent(stream), new PutFileParameters { ContentType = contentType });
var @params = new PutFileParameters { ContentType = new MediaTypeHeaderValue(contentType) };
return PutFile(requestUri, new StreamContent(stream), @params);
}

/// <summary>
Expand Down Expand Up @@ -667,7 +670,7 @@ public async Task<LockResponse> Lock(Uri requestUri, LockParameters parameters)

var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestBody = LockRequestBuilder.BuildRequestBody(parameters);
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody) };
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Lock, requestParams, parameters.CancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
return new LockResponse((int)response.StatusCode, response.ReasonPhrase);
Expand Down

0 comments on commit f930f8c

Please sign in to comment.