Skip to content

Commit

Permalink
Sending a POST to a file uri should not create a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
smatsson committed Nov 11, 2023
1 parent 8ac0ebc commit 331d707
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 25 additions & 1 deletion Source/tusdotnet.test/Tests/EndpointRoutingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
using NSubstitute;
using Shouldly;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using tusdotnet.Interfaces;
using tusdotnet.Models;
using tusdotnet.test.Extensions;
using Xunit;

namespace tusdotnet.test.Tests
Expand Down Expand Up @@ -60,6 +62,28 @@ public async Task The_Endpoint_Configuration_Factory_Is_Used()
endpointFactoryUsed.ShouldBeTrue();
}

[Fact]
public async Task Ignores_Request_If_Url_Does_Not_Match()
{
using var server = CreateTestServer(endpoints => endpoints.MapTus("/files", _ => Task.FromResult(CreateConfig())));

await SendAndAssert(server, "/files", HttpStatusCode.Created);

await SendAndAssert(server, "/otherfiles", HttpStatusCode.NotFound);

await SendAndAssert(server, "/files/testfile", HttpStatusCode.NotFound);

static async Task SendAndAssert(TestServer server, string path, HttpStatusCode expectedStatusCode)
{
var response = await server.CreateRequest(path)
.AddTusResumableHeader()
.AddHeader("Upload-Length", "100")
.SendAsync("POST");

response.StatusCode.ShouldBe(expectedStatusCode);
}
}

private static TestServer CreateTestServer(Action<IEndpointRouteBuilder> endpoints, Action<IServiceCollection> configureServices = null)
{
var builder = new WebHostBuilder()
Expand All @@ -83,7 +107,7 @@ private static DefaultTusConfiguration CreateConfig(Action onAuthorizeCalled = n
{
return new DefaultTusConfiguration
{
Store = Substitute.For<ITusStore>(),
Store = Substitute.For<ITusStore, ITusCreationStore>(),
Events = new()
{
OnAuthorizeAsync = _ =>
Expand Down
4 changes: 3 additions & 1 deletion Source/tusdotnet/Adapters/EndpointUrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace tusdotnet.Adapters
{
// This class assumes that it's running for an endpoint and such we already know that
// the path pattern matches as the code would not run otherwise.
internal class EndpointUrlHelper : IUrlHelper
{
public static EndpointUrlHelper Instance { get; } = new();
Expand All @@ -22,7 +24,7 @@ public bool UrlMatchesFileIdUrl(ContextAdapter context)

public bool UrlMatchesUrlPath(ContextAdapter context)
{
return true;
return !UrlMatchesFileIdUrl(context);
}
}
}
Expand Down

0 comments on commit 331d707

Please sign in to comment.