Skip to content

Commit

Permalink
feat: adding test For TusCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetianx committed May 13, 2024
1 parent 722dfd4 commit da99090
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 57 deletions.
59 changes: 2 additions & 57 deletions src/BirdMessenger.Test/HttpClientExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,12 @@ public HttpClientExtensionTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task TestTusCreateAndDelAsync()
{
bool isInovkeOnPreSendRequestAsync = false;
using var httpClient = new HttpClient();

MetadataCollection dir = new MetadataCollection();
dir["filename"] = "fileNameTest";
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 1000,
Metadata = dir,
OnPreSendRequestAsync = x =>
{
_testOutputHelper.WriteLine("OnPreSendRequestAsync is invoked");
isInovkeOnPreSendRequestAsync = true;
return Task.CompletedTask;
}
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, resp.TusResumableVersion);
Assert.True(isInovkeOnPreSendRequestAsync);

TusDeleteRequestOption tusDeleteRequestOption = new TusDeleteRequestOption()
{
FileLocation = resp.FileLocation
};

var delResp = await httpClient.TusDeleteAsync(tusDeleteRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, delResp.TusResumableVersion);
}

[Fact]
public async Task TestTusCreateArgumentExceptionAsync()
{
bool isArgumentException = false;
try
{
using var httpClient = new HttpClient();
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 1000,
IsUploadDeferLength = true
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
}
catch (ArgumentException)
{
isArgumentException = true;
}

Assert.True(isArgumentException);
}


[Theory]
[InlineData(1000)]
[InlineData(50)]
[InlineData(-1)]
[InlineData(1)]
[InlineData(0)]
public async Task TestTusHeadAsync(long uploadLength)
{
Expand Down
128 changes: 128 additions & 0 deletions src/BirdMessenger.Test/TusCreateTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BirdMessenger.Collections;
using Xunit;
using Xunit.Abstractions;

namespace BirdMessenger.Test;

public class TusCreateTest
{
private readonly ITestOutputHelper _testOutputHelper;
public static Uri TusEndpoint = new Uri("http://localhost:5094/files");

public TusCreateTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task CreateZoreSizeFile()
{
bool isInovkeOnPreSendRequestAsync = false;

using var httpClient = new HttpClient();

MetadataCollection dir = new MetadataCollection();
dir["filename"] = "fileNameTest";
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 0,
Metadata = dir,
OnPreSendRequestAsync = x =>
{
_testOutputHelper.WriteLine("OnPreSendRequestAsync is invoked");
isInovkeOnPreSendRequestAsync = true;
return Task.CompletedTask;
}
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, resp.TusResumableVersion);
Assert.True(isInovkeOnPreSendRequestAsync);
}
[Fact]
public async Task CreateWithIsUploadDeferLength()
{
bool isInovkeOnPreSendRequestAsync = false;

using var httpClient = new HttpClient();

MetadataCollection dir = new MetadataCollection();
dir["filename"] = "fileNameTest";
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 0,
IsUploadDeferLength = true,
Metadata = dir,
OnPreSendRequestAsync = x =>
{
_testOutputHelper.WriteLine("OnPreSendRequestAsync is invoked");
isInovkeOnPreSendRequestAsync = true;
return Task.CompletedTask;
}
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, resp.TusResumableVersion);
Assert.True(isInovkeOnPreSendRequestAsync);
}
[Fact]
public async Task TestTusCreateAndDelAsync()
{
bool isInovkeOnPreSendRequestAsync = false;
using var httpClient = new HttpClient();

MetadataCollection dir = new MetadataCollection();
dir["filename"] = "fileNameTest";
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 1000,
Metadata = dir,
OnPreSendRequestAsync = x =>
{
_testOutputHelper.WriteLine("OnPreSendRequestAsync is invoked");
isInovkeOnPreSendRequestAsync = true;
return Task.CompletedTask;
}
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, resp.TusResumableVersion);
Assert.True(isInovkeOnPreSendRequestAsync);

TusDeleteRequestOption tusDeleteRequestOption = new TusDeleteRequestOption()
{
FileLocation = resp.FileLocation
};

var delResp = await httpClient.TusDeleteAsync(tusDeleteRequestOption, CancellationToken.None);
Assert.Equal(TusVersion.V1_0_0, delResp.TusResumableVersion);
}

[Fact]
public async Task TestTusCreateArgumentExceptionAsync()
{
bool isArgumentException = false;
try
{
using var httpClient = new HttpClient();
TusCreateRequestOption tusCreateRequestOption = new TusCreateRequestOption()
{
Endpoint = TusEndpoint,
UploadLength = 1000,
IsUploadDeferLength = true
};
var resp = await httpClient.TusCreateAsync(tusCreateRequestOption, CancellationToken.None);
}
catch (ArgumentException)
{
isArgumentException = true;
}

Assert.True(isArgumentException);
}

}

0 comments on commit da99090

Please sign in to comment.