Skip to content

Commit

Permalink
add test for FromRoute attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz96 committed Nov 17, 2023
1 parent 32f77a8 commit 82dd9ed
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 61 deletions.
73 changes: 42 additions & 31 deletions test/UriGeneration.IntegrationTests/AttributeRoutingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task Works_WithFromQueryParameter()
}

[Fact]
public async Task Works_WithFromRouteParameter()
public async Task Works_WithRouteParameter()
{
var client = _factory.CreateClient();

Expand All @@ -90,6 +90,17 @@ public async Task Works_WithFromRouteParameter()
Assert.Equal("http://localhost/AttributeRouting/Test7/1", uri);
}

[Fact]
public async Task Works_WithFromRouteParameter()
{
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test8/1");

Assert.Equal("http://localhost/AttributeRouting/Test8/1", uri);
}

[Fact]
public async Task Works_WithActionNameAttribute()
{
Expand All @@ -112,11 +123,11 @@ public async Task Excludes_FromBodyParameter()
};

var response = await client.PostAsJsonAsync(
"/AttributeRouting/Test9",
"/AttributeRouting/Test10",
model);
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test9", uri);
Assert.Equal("http://localhost/AttributeRouting/Test10", uri);
}

[Fact]
Expand All @@ -130,11 +141,11 @@ public async Task Excludes_FromFormParameter()
};

var response = await client.PostAsync(
"/AttributeRouting/Test10",
"/AttributeRouting/Test11",
new FormUrlEncodedContent(model));
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test10", uri);
Assert.Equal("http://localhost/AttributeRouting/Test11", uri);
}

[Fact]
Expand All @@ -144,9 +155,9 @@ public async Task Excludes_FromHeaderParameter()
client.DefaultRequestHeaders.Add("apiKey", "Test");

string uri = await client.GetStringAsync(
"/AttributeRouting/Test11");
"/AttributeRouting/Test12");

Assert.Equal("http://localhost/AttributeRouting/Test11", uri);
Assert.Equal("http://localhost/AttributeRouting/Test12", uri);
}

[Fact]
Expand All @@ -155,9 +166,9 @@ public async Task Excludes_FromServicesParameter()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test12");
"/AttributeRouting/Test13");

Assert.Equal("http://localhost/AttributeRouting/Test12", uri);
Assert.Equal("http://localhost/AttributeRouting/Test13", uri);
}

[Fact]
Expand All @@ -174,11 +185,11 @@ public async Task Excludes_IFormFileParameter()
};

var response = await client.PostAsync(
"/AttributeRouting/Test13",
"/AttributeRouting/Test14",
content);
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test13", uri);
Assert.Equal("http://localhost/AttributeRouting/Test14", uri);
}

[Fact]
Expand All @@ -200,11 +211,11 @@ public async Task Excludes_IFormFileCollectionParameter()
};

var response = await client.PostAsync(
"/AttributeRouting/Test14",
"/AttributeRouting/Test15",
content);
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test14", uri);
Assert.Equal("http://localhost/AttributeRouting/Test15", uri);
}

[Fact]
Expand All @@ -226,11 +237,11 @@ public async Task Excludes_EnumerableOfIFormFileParameter()
};

var response = await client.PostAsync(
"/AttributeRouting/Test15",
"/AttributeRouting/Test16",
content);
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test15", uri);
Assert.Equal("http://localhost/AttributeRouting/Test16", uri);
}

[Fact]
Expand All @@ -239,9 +250,9 @@ public async Task Excludes_CancellationTokenParameter()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test16");
"/AttributeRouting/Test17");

Assert.Equal("http://localhost/AttributeRouting/Test16", uri);
Assert.Equal("http://localhost/AttributeRouting/Test17", uri);
}

[Fact]
Expand All @@ -261,19 +272,19 @@ public async Task Excludes_IFormCollectionParameter()
};

var response = await client.PostAsync(
"/AttributeRouting/Test17",
"/AttributeRouting/Test18",
content);
string uri = await response.Content.ReadAsStringAsync();

Assert.Equal("http://localhost/AttributeRouting/Test17", uri);
Assert.Equal("http://localhost/AttributeRouting/Test18", uri);
}

[InlineData(
"/EndpointName1/Test18?endpointName=En1",
"http://localhost/EndpointName1/Test18?endpointName=En1")]
"/EndpointName1/Test19?endpointName=En1",
"http://localhost/EndpointName1/Test19?endpointName=En1")]
[InlineData(
"/EndpointName1/Test18?endpointName=En2",
"http://localhost/EndpointName2/Test18?endpointName=En2")]
"/EndpointName1/Test19?endpointName=En2",
"http://localhost/EndpointName2/Test19?endpointName=En2")]
[Theory]
public async Task Works_WithEndpointName(
string requestUri,
Expand All @@ -292,9 +303,9 @@ public async Task Works_WithAsyncSuffix()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test19");
"/AttributeRouting/Test20");

Assert.Equal("http://localhost/AttributeRouting/Test19", uri);
Assert.Equal("http://localhost/AttributeRouting/Test20", uri);
}

[Fact]
Expand All @@ -303,9 +314,9 @@ public async Task Passes_LinkOptions()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test20");
"/AttributeRouting/Test21");

Assert.Equal("http://localhost/AttributeRouting/Test20/", uri);
Assert.Equal("http://localhost/AttributeRouting/Test21/", uri);
}

[Fact]
Expand All @@ -314,9 +325,9 @@ public async Task Works_WithoutMethodCacheAndCachedExpressionCompiler()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/AttributeRouting/Test21?id=1");
"/AttributeRouting/Test22?id=1");

Assert.Equal("http://localhost/AttributeRouting/Test21?id=1", uri);
Assert.Equal("http://localhost/AttributeRouting/Test22?id=1", uri);
}

[Fact]
Expand All @@ -325,9 +336,9 @@ public async Task Works_WithRouteTemplateProvider()
var client = _factory.CreateClient();

string uri = await client.GetStringAsync(
"/TemplateProvider/Test22");
"/TemplateProvider/Test23");

Assert.Equal("http://localhost/TemplateProvider/Test22", uri);
Assert.Equal("http://localhost/TemplateProvider/Test23", uri);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,124 +80,132 @@ public void VoidAction()
c => c.Test7(id));
}

[HttpGet("{id}")]
public string? Test8([FromRoute] int id)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test8(id));
}

[ActionName("ActionName")]
[HttpGet]
public string? Test8()
public string? Test9()
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test8());
c => c.Test9());
}

[HttpPost]
public string? Test9([FromBody] TestModel model)
public string? Test10([FromBody] TestModel model)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test9(model));
c => c.Test10(model));
}

[HttpPost]
public string? Test10([FromForm] TestModel model)
public string? Test11([FromForm] TestModel model)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test10(model));
c => c.Test11(model));
}

[HttpGet]
public string? Test11([FromHeader] string apiKey)
public string? Test12([FromHeader] string apiKey)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test11(apiKey));
c => c.Test12(apiKey));
}

[HttpGet]
public string? Test12([FromServices] IUriGenerator uriGenerator)
public string? Test13([FromServices] IUriGenerator uriGenerator)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test12(uriGenerator));
c => c.Test13(uriGenerator));
}

[HttpPost]
public string? Test13(IFormFile file)
public string? Test14(IFormFile file)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test13(file));
c => c.Test14(file));
}

[HttpPost]
public string? Test14(IFormFileCollection files)
public string? Test15(IFormFileCollection files)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test14(files));
c => c.Test15(files));
}

[HttpPost]
public string? Test15(IEnumerable<IFormFile> files)
public string? Test16(IEnumerable<IFormFile> files)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test15(files));
c => c.Test16(files));
}

[HttpGet]
public string? Test16(CancellationToken cancellationToken)
public string? Test17(CancellationToken cancellationToken)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test16(cancellationToken));
c => c.Test17(cancellationToken));
}

[HttpPost]
public string? Test17(IFormCollection form)
public string? Test18(IFormCollection form)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test17(form));
c => c.Test18(form));
}

[HttpGet("/EndpointName1/[action]", Name = "En1")]
[HttpGet("/EndpointName2/[action]", Name = "En2")]
public string? Test18(string endpointName)
public string? Test19(string endpointName)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test18(endpointName),
c => c.Test19(endpointName),
endpointName);
}

[HttpGet]
public Task<string?> Test19Async()
public Task<string?> Test20Async()
{
return Task.FromResult(
_uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test19Async()));
c => c.Test20Async()));
}

[HttpGet]
public string? Test20()
public string? Test21()
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test20(),
c => c.Test21(),
options: new UriOptions
{
AppendTrailingSlash = true
});
}

[HttpGet]
public string? Test21(int id)
public string? Test22(int id)
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test21(id),
c => c.Test22(id),
options: new UriOptions
{
BypassMethodCache = true,
Expand All @@ -206,11 +214,11 @@ public void VoidAction()
}

[TestTemplateProvider("/TemplateProvider/[action]")]
public string? Test22()
public string? Test23()
{
return _uriGenerator.GetUriByExpression<AttributeRoutingController>(
HttpContext,
c => c.Test22());
c => c.Test23());
}
}
}

0 comments on commit 82dd9ed

Please sign in to comment.