Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file creation requests #1657

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/FileLinks/FileLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class FileLink : StripeEntity<FileLink>, IHasId, IHasMetadata, IHasObject
/// </summary>
[JsonProperty("expires_at")]
[JsonConverter(typeof(DateTimeConverter))]
public DateTime ExpiresAt { get; set; }
public DateTime? ExpiresAt { get; set; }

/// <summary>
/// Has the value <c>true</c> if the object exists in live mode or the value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,23 @@ private static StreamContent CreateStreamContent(Stream value, string name)
return content;
}

private static string QuoteString(string value)
{
return "\"" + value + "\"";
}

private void ProcessParameters(IEnumerable<KeyValuePair<string, object>> nameValueCollection)
{
foreach (var kvp in nameValueCollection)
{
switch (kvp.Value)
{
case string s:
this.Add(CreateStringContent(s), kvp.Key);
this.Add(CreateStringContent(s), QuoteString(kvp.Key));
break;

case Stream s:
this.Add(CreateStreamContent(s, kvp.Key));
this.Add(CreateStreamContent(s, QuoteString(kvp.Key)));
break;

default:
Expand Down
21 changes: 17 additions & 4 deletions src/Stripe.net/Services/Files/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public FileService(IStripeClient client)

public virtual File Create(FileCreateOptions options, RequestOptions requestOptions = null)
{
requestOptions = this.SetupRequestOptions(requestOptions);
requestOptions.BaseUrl = requestOptions.BaseUrl ?? this.Client.FilesBase;
requestOptions = this.SetupRequestOptionsForFilesRequest(requestOptions);
return this.CreateEntity(options, requestOptions);
}

public virtual Task<File> CreateAsync(FileCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
requestOptions = this.SetupRequestOptions(requestOptions);
requestOptions.BaseUrl = requestOptions.BaseUrl ?? this.Client.FilesBase;
requestOptions = this.SetupRequestOptionsForFilesRequest(requestOptions);
return this.CreateEntityAsync(options, requestOptions, cancellationToken);
}

Expand Down Expand Up @@ -59,5 +57,20 @@ public virtual IEnumerable<File> ListAutoPaging(FileListOptions options = null,
{
return this.ListEntitiesAutoPaging(options, requestOptions);
}

private RequestOptions SetupRequestOptionsForFilesRequest(RequestOptions requestOptions)
{
if (requestOptions == null)
{
requestOptions = new RequestOptions();
}

if (requestOptions.BaseUrl == null)
{
requestOptions.BaseUrl = this.Client.FilesBase;
}

return requestOptions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task CreateHttpContent_Stream()
Assert.Equal(1, content.Headers.ContentType.Parameters.Count);

var stream = await content.ReadAsStreamAsync();
Assert.Equal(760, stream.Length);
Assert.Equal(764, stream.Length);
var result = new StreamReader(stream).ReadToEnd();

// The boundary will be a random GUID, so we just check for substrings.
Expand All @@ -86,10 +86,10 @@ public async Task CreateHttpContent_Stream()
"Content-Type: text/plain; charset=utf-8\r\nContent-Disposition: form-data; name=\"list[2]\"\r\n\r\n3\r\n",
result);
Assert.Contains(
"Content-Disposition: form-data; name=stream; filename=blob; filename*=utf-8''blob\r\nContent-Type: application/octet-stream\r\n\r\nHello World!\r\n",
"Content-Disposition: form-data; name=\"stream\"; filename=blob; filename*=utf-8''blob\r\nContent-Type: application/octet-stream\r\n\r\nHello World!\r\n",
result);
Assert.Contains(
"Content-Type: text/plain; charset=utf-8\r\nContent-Disposition: form-data; name=string\r\n\r\nString!\r\n",
"Content-Type: text/plain; charset=utf-8\r\nContent-Disposition: form-data; name=\"string\"\r\n\r\nString!\r\n",
result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public async Task Ctor_OneStringEntry_Success()
var content = new MultipartFormDataContent(source, "test-boundary");

var stream = await content.ReadAsStreamAsync();
Assert.Equal(131, stream.Length);
Assert.Equal(133, stream.Length);
var result = new StreamReader(stream).ReadToEnd();
Assert.Equal(
"--test-boundary\r\nContent-Type: text/plain; charset=utf-8\r\n"
+ "Content-Disposition: form-data; name=key\r\n\r\nvalueク\r\n--test-boundary--\r\n",
+ "Content-Disposition: form-data; name=\"key\"\r\n\r\nvalueク\r\n--test-boundary--\r\n",
result);
}

Expand All @@ -61,11 +61,11 @@ public async Task Ctor_OneStreamEntry_Success()
var content = new MultipartFormDataContent(source, "test-boundary");

var stream = await content.ReadAsStreamAsync();
Assert.Equal(172, stream.Length);
Assert.Equal(174, stream.Length);
var result = new StreamReader(stream).ReadToEnd();
Assert.Equal(
"--test-boundary\r\n"
+ "Content-Disposition: form-data; name=key; filename=blob; filename*=utf-8''blob\r\n"
+ "Content-Disposition: form-data; name=\"key\"; filename=blob; filename*=utf-8''blob\r\n"
+ "Content-Type: application/octet-stream\r\n\r\nHello World!\r\n"
+ "--test-boundary--\r\n",
result);
Expand All @@ -82,14 +82,14 @@ public async Task Ctor_TwoEntries_Success()
var content = new MultipartFormDataContent(source, "test-boundary");

var stream = await content.ReadAsStreamAsync();
Assert.Equal(295, stream.Length);
Assert.Equal(299, stream.Length);
var result = new StreamReader(stream).ReadToEnd();
Assert.Equal(
"--test-boundary\r\n"
+ "Content-Disposition: form-data; name=stream; filename=blob; filename*=utf-8''blob\r\n"
+ "Content-Disposition: form-data; name=\"stream\"; filename=blob; filename*=utf-8''blob\r\n"
+ "Content-Type: application/octet-stream\r\n\r\nHello クWorld!\r\n"
+ "--test-boundary\r\nContent-Type: text/plain; charset=utf-8\r\n"
+ "Content-Disposition: form-data; name=string\r\n\r\nString!ク\r\n--test-boundary--\r\n",
+ "Content-Disposition: form-data; name=\"string\"\r\n\r\nString!ク\r\n--test-boundary--\r\n",
result);
}

Expand Down