Integration testing with in-memory IdentityServer

前端 未结 7 1561
一生所求
一生所求 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:44

    Test API startup:

    public class Startup
    {
        public static HttpMessageHandler BackChannelHandler { get; set; }
    
        public void Configuration(IAppBuilder app)
        {
            //accept access tokens from identityserver and require a scope of 'Test'
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "https://localhost",
                BackchannelHttpHandler = BackChannelHandler,
                ...
            });
    
            ...
        }
    }
    

    Assigning the AuthServer.Handler to TestApi BackChannelHandler in my unit test project:

        protected TestServer AuthServer { get; set; }
        protected TestServer MockApiServer { get; set; }
        protected TestServer TestApiServer { get; set; }
    
        [OneTimeSetUp]
        public void Setup()
        {
            ...
            AuthServer = TestServer.Create();
            TestApi.Startup.BackChannelHandler = AuthServer.CreateHandler();
            TestApiServer = TestServer.Create();
        }
    

提交回复
热议问题