Comments (10)
from testing.
This should be moved to aspnet/Hosting.
@rynowak You were discussing being able to get a RequestDelegate
instead of an HttpClient
from the TestServer
. Did you file an issue for that anywhere?
from testing.
the "best" way to currently test is to check for existence of attributes through reflection and unit-test the components separately @kasparkallas
Can you explain further or link to a guide? Do you just mean checking to see if the [Authorization]
data attribute is attached to a method via reflection and perform some forked set of actions for authorized and not-authorized behavior?
I assume this would not take granular claims assessment into account, i.e. if your controller was checking to see if the current user HasClaims
..
from testing.
@ferrx Yes, I did mean checking for the presence of the attribute via reflection. Which is clearly not a good way to test it because of all the possible combinations of attributes, filters and explicit authorization logic in controller (like you said). Sorry, I don't have an article to share.
I have been informed that the ClaimsPrincipal
can be passed when using the open-source ASP.NET Core integration testing project Alba which does the same thing as TestHost
but exposes RequestDelegate
(if I understand correctly).
A code example setting up WindowsPrincipal
for User
that I got from Alba's Gitter room (thanks @joemcbride):
private Task<IScenarioResult> run(Action<Scenario> configuration)
{
using (var system = SystemUnderTest.ForStartup<Startup>())
{
system.Environment.EnvironmentName = "Development";
return system.Scenario(configuration);
}
}
[Fact]
public async Task authenticated_values()
{
var result = await run(_ =>
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
_.Context.User = principal;
_.Get.Url("/api/values");
_.Context.Request.Method.ShouldBe("GET");
});
result.Context.Response.StatusCode.ShouldBe(200);
var output = result.ResponseBody.ReadAsJson<string[]>();
output.ShouldNotBeNull();
output.ShouldBe(new []{ "value1", "value2"});
}
from testing.
@javiercn - is it possible to use Microsoft.AspNetCore.Mvc.Testing
to get a RequestDelegate
?
from testing.
The test server takes an IWebHostBuilder
which creates an IApplicationBuilder
and uses pretty much the entire hosting pipeline. There's no way to get the request delegate directly.
from testing.
I believe is trivial to expose the request delegate from TestServer.
That said, If i were to set the principal in the HttpContext at the beginning of the request, I would just do it with a startup filter in the same fashion that we do to setup the request culture.
Create a startup filter, plug it in DI and make it setup a middleware that sets the principal to whatever you are expecting.
If we were to expose the request delegate you would need to be responsible of creating the HttpContext yourself and I'm not sure of the subtleties that might have (it might be doable)
from testing.
from testing.
I believe is trivial to expose the request delegate from TestServer.
I'm not sure it is, but we should discuss offline.
from testing.
This issue was moved to dotnet/aspnetcore#2357
from testing.
Related Issues (20)
- Integration tests in separate project HOT 2
- Skip not working in ConditionalFact and ConditionalTheory
- Create "xunit" logger provider HOT 5
- Regression: ConditionalTheory not always recognized HOT 3
- Some ConditionalTheory tests are run N^2 times HOT 3
- .NET 4.5.1 test runs fail with "EXEC : warning : No test is available" HOT 5
- ConditionalFact test case fails due to change in xunit API HOT 1
- Allow ConditionalTheory to have an empty data collection HOT 5
- Older package from template than NuGet shows HOT 1
- Tests marked with `ConditionalFactAttribute` not discovered running on full framework HOT 2
- Inverse OSSkipCondition HOT 4
- Flaky test: ConditionalFactTest.ConditionalFactSkip on Win7 HOT 3
- TimeoutAfter won't time out if the task performs a synchronous wait HOT 3
- Run tests on published test project HOT 2
- Create infrastructure for testing with docker HOT 2
- Move HttpClientSlim to Microsoft.AspNetCore.Testing
- Rewrite HttpClientSlimTests in a way that doesn't depend on Hosting HOT 7
- Tests using TestServer are not working when I UseMVC() in the test startup HOT 2
- THIS ISSUE TRACKER IS CLOSED - use the Home repo issue tracker
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing.