Integration testing with in-memory IdentityServer

前端 未结 7 1578
一生所求
一生所求 2020-12-07 17:23

I have an API that uses IdentityServer4 for token validation. I want to unit test this API with an in-memory TestServer. I\'d like to host the IdentityServer in the in-memor

7条回答
  •  醉酒成梦
    2020-12-07 17:48

    I think you probably need to make a test double fake for your authorization middleware depending on how much functionality you want. So basically you want a middleware that does everything that the Authorization middleware does minus the back channel call to the discovery doc.

    IdentityServer4.AccessTokenValidation is a wrapper around two middlewares. The JwtBearerAuthentication middleware, and the OAuth2IntrospectionAuthentication middleware. Both of these grab the discovery document over http to use for token validation. Which is a problem if you want to do an in-memory self-contained test.

    If you want to go through the trouble you will probably need to make a fake version of app.UseIdentityServerAuthentication that doesnt do the external call that fetches the discovery document. It only populates the HttpContext principal so that your [Authorize] policies can be tested.

    Check out how the meat of IdentityServer4.AccessTokenValidation looks here. And follow up with a look at how JwtBearer Middleware looks here

提交回复
热议问题