Skip to content

Commit

Permalink
Propagate shorturl filename
Browse files Browse the repository at this point in the history
  • Loading branch information
jakublabno committed Jan 9, 2025
1 parent d14c64e commit 39a260a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/shortUrl/CreateFileLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var features = new Features(client);

const string name = "abc";
var file = new FileStream("<path_to_file>", FileMode.Open);
var file = new FileInfo("<path_to_file>");

try
{
Expand Down
6 changes: 3 additions & 3 deletions smsapi/Api/Action/ShortUrl/CreateShortUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum ShortUrlExpirationUnit

private string? _description;
private (uint, string)? _expireAt;
private readonly Stream? _file;
private readonly FileInfo? _file;

private readonly string _name;
private readonly string? _url;
Expand All @@ -32,7 +32,7 @@ public CreateShortUrl(string name, string url)
_url = url;
}

public CreateShortUrl(string name, Stream file)
public CreateShortUrl(string name, FileInfo file)
{
_name = name;
_file = file;
Expand Down Expand Up @@ -93,7 +93,7 @@ protected override Dictionary<string, Stream> Files()
{
var files = new Dictionary<string, Stream>();

_file?.Let(file => files.Add("file", file));
_file?.Let(file => files.Add(file.Name, file.OpenRead()));

return files;
}
Expand Down
2 changes: 1 addition & 1 deletion smsapi/Api/ShortUrlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CreateShortUrl Create(string name, string uri)
return action;
}

public CreateShortUrl Create(string name, Stream file)
public CreateShortUrl Create(string name, FileInfo file)
{
var action = new CreateShortUrl(name, file);
action.Proxy(proxy);
Expand Down
2 changes: 1 addition & 1 deletion smsapi/NativeHttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static HttpContent ConvertRequestDataToHttpContent(
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
FileName = "\"file\""
FileName = $"\"{files.Keys.First()}\""
};

var content = new MultipartFormDataContent
Expand Down
10 changes: 7 additions & 3 deletions smsapiTests/Unit/Action/ShortUrl/CreateShortUrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ public void send_description()
public void send_name_and_file()
{
var name = "fancy name";
var file = new MemoryStream();
var fileName = "richMedia.txt";
var fileContent = "file content";
var filePath = Path.Combine(Path.GetTempPath(), fileName);
File.WriteAllText(filePath, fileContent);
var file = new FileInfo(filePath);

CreateShortUrl(name, file).Execute();

_proxyAssert.AssertParametersCount(2);
_proxyAssert.AssertParametersContain("name", name);
_proxyAssert.AssertParametersContain("type", "FILE");
_proxyAssert.AssertFileAttached(file);
_proxyAssert.AssertFileAttached(fileName, file.OpenRead());
}

[TestMethod]
Expand All @@ -96,7 +100,7 @@ private CreateShortUrl CreateShortUrl(string name, string uri)
return action;
}

private CreateShortUrl CreateShortUrl(string name, Stream file)
private CreateShortUrl CreateShortUrl(string name, FileInfo file)
{
var action = new CreateShortUrl(name, file);
action.Proxy(_spyProxy);
Expand Down
4 changes: 2 additions & 2 deletions smsapiTests/Unit/ProxyAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public ProxyAssert AssertParametersContain(string name, string value)
return this;
}

public void AssertFileAttached(Stream file)
public void AssertFileAttached(string name, Stream file)
{
Assert.IsTrue(
proxy.Files.Contains(value: file),
proxy.Files.Contains(value: KeyValuePair.Create(name, new StreamReader(file).ReadToEnd())),
"Not attached file found"
);
}
Expand Down
12 changes: 8 additions & 4 deletions smsapiTests/Unit/SpyProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SpyProxy : Proxy
public RequestMethod RequestMethod { get; private set; }

public Dictionary<string, dynamic?> Parameters { get; } = new();
public ICollection<Stream> Files { get; } = new List<Stream>();
public ICollection<KeyValuePair<string, string>> Files { get; } = new List<KeyValuePair<string, string>>();

public void Authentication(IClient client)
{
Expand All @@ -39,7 +39,8 @@ public HttpResponseEntity Execute(ActionContentType contentType, string uri, ISe
RequestedUri = uri;
SetParameters(data);
RequestMethod = method;
Files.Add(file);
Files.Add(KeyValuePair.Create("", new StreamReader(file).ReadToEnd()));
file.Position = 0;

return new HttpResponseEntity(new Task<Stream>(() => new MemoryStream()), HttpStatusCode.OK);
}
Expand All @@ -49,9 +50,12 @@ public HttpResponseEntity Execute(ActionContentType contentType, string uri, ISe
RequestedUri = uri;
SetParameters(data);
RequestMethod = method;
foreach (var file in files.Values)
foreach (var file in files)
{
Files.Add(file);
var content = new StreamReader(file.Value).ReadToEnd();
file.Value.Position = 0;

Files.Add(KeyValuePair.Create(file.Key, content));
}

return new HttpResponseEntity(Task.FromResult(Stream.Null), HttpStatusCode.OK);
Expand Down

0 comments on commit 39a260a

Please sign in to comment.