public async Task Invoke_SseRequestWithEventStreamAcceptHeader_Accepts() { ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(); HttpContext context = PrepareHttpContext(SSE_CONTENT_TYPE); await serverSentEventsMiddleware.Invoke(context, null); Assert.Equal(SSE_CONTENT_TYPE, context.Response.ContentType); }
public async Task Invoke_SseRequestWithNotEventStreamAcceptHeader_Accepts() { ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(); HttpContext context = PrepareHttpContext("text/plain"); await serverSentEventsMiddleware.Invoke(context, null); Assert.Null(context.Response.ContentType); }
public async Task Invoke_SseRequest_NoAuthorization_DoesNotCallAuthorizeAsync() { ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(); HttpContext context = PrepareHttpContext(); Mock <IPolicyEvaluator> policyEvaluatorMock = PreparePolicyEvaluatorMock(context); await serverSentEventsMiddleware.Invoke(context, policyEvaluatorMock.Object); policyEvaluatorMock.Verify(m => m.AuthorizeAsync(It.IsAny <AuthorizationPolicy>(), DEFAULT_AUTHENTICATE_RESULT, context, null), Times.Never); }
public async Task Invoke_SseRequest_Authorization_CallsAuthenticateAsync() { ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(ServerSentEventsAuthorization.Default); HttpContext context = PrepareHttpContext(); Mock <IPolicyEvaluator> policyEvaluatorMock = PreparePolicyEvaluatorMock(context); await serverSentEventsMiddleware.Invoke(context, policyEvaluatorMock.Object); policyEvaluatorMock.Verify(m => m.AuthenticateAsync(It.IsAny <AuthorizationPolicy>(), context), Times.Once); }
public async Task Invoke_SseRequest_CallsOnPrepareAccept() { Mock <Action <HttpResponse> > onPrepareAcceptMock = new Mock <Action <HttpResponse> >(); ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(new ServerSentEventsOptions { OnPrepareAccept = onPrepareAcceptMock.Object }); await serverSentEventsMiddleware.Invoke(PrepareHttpContext(null), null); onPrepareAcceptMock.Verify(m => m(It.IsAny <HttpResponse>()), Times.Once); }
public async Task Invoke_SseRequest_Authorization_PolicyAuthorizationResultForbid_CallsForbidAsync() { Mock <IAuthenticationService> authenticationServiceMock = new Mock <IAuthenticationService>(); ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(ServerSentEventsAuthorization.Default); HttpContext context = PrepareHttpContext(authenticationServiceMock); Mock <IPolicyEvaluator> policyEvaluatorMock = PreparePolicyEvaluatorMock(context, policyAuthorizationResult: PolicyAuthorizationResult.Forbid()); await serverSentEventsMiddleware.Invoke(context, policyEvaluatorMock.Object); authenticationServiceMock.Verify(m => m.ForbidAsync(context, null, null), Times.Once); }
public async Task Invoke_SseRequest_Authorization_PolicyAuthorizationResultSuccess_DoesNotCallForbidAsync() { Mock <IAuthenticationService> authenticationServiceMock = new Mock <IAuthenticationService>(); ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(ServerSentEventsAuthorization.Default); HttpContext context = PrepareHttpContext(authenticationServiceMock); Mock <IPolicyEvaluator> policyEvaluatorMock = PreparePolicyEvaluatorMock(context, policyAuthorizationResult: PolicyAuthorizationResult.Success()); await serverSentEventsMiddleware.Invoke(context, policyEvaluatorMock.Object); authenticationServiceMock.Verify(m => m.ForbidAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <AuthenticationProperties>()), Times.Never); }
public async Task Invoke_SseRequest_AuthorizationWithSchemes_PolicyAuthorizationResultChallenge_CallsChallengeAsyncForEveryScheme() { Mock <IAuthenticationService> authenticationServiceMock = new Mock <IAuthenticationService>(); ServerSentEventsMiddleware <ServerSentEventsService> serverSentEventsMiddleware = PrepareServerSentEventsMiddleware(new ServerSentEventsAuthorization { AuthenticationSchemes = "schema1,schema2" }); HttpContext context = PrepareHttpContext(authenticationServiceMock); Mock <IPolicyEvaluator> policyEvaluatorMock = PreparePolicyEvaluatorMock(context, policyAuthorizationResult: PolicyAuthorizationResult.Challenge()); await serverSentEventsMiddleware.Invoke(context, policyEvaluatorMock.Object); authenticationServiceMock.Verify(m => m.ChallengeAsync(context, "schema1", null), Times.Once); authenticationServiceMock.Verify(m => m.ChallengeAsync(context, "schema2", null), Times.Once); }