Example #1
0
        public async Task ValidateServiceTicketWithBadResponse_ShouldThrowsHttpRequestException()
        {
            // Arrange
            var ticket     = Guid.NewGuid().ToString();
            var requestUrl = $"{_options.CasServerUrlBase}/serviceValidate?ticket={ticket}&service={Uri.EscapeDataString(_service)}";
            var mockHttp   = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, requestUrl)
            .Respond(HttpStatusCode.BadRequest);
            var validator = new Cas20ServiceTicketValidator(_options, new HttpClient(mockHttp));

            // Act & Assert
            await Assert.ThrowsAsync <HttpRequestException>(() => validator.ValidateAsync(ticket, _service, CancellationToken.None)).ConfigureAwait(false);

            mockHttp.VerifyNoOutstandingRequest();
            mockHttp.VerifyNoOutstandingExpectation();
        }
        public async Task ValidateServiceTicket_FailAsync()
        {
            // Arrange
            var ticket     = Guid.NewGuid().ToString();
            var requestUrl = $"{options.CasServerUrlBase}/serviceValidate?ticket={ticket}&service={Uri.EscapeDataString(service)}";
            var mockHttp   = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, requestUrl)
            .Respond("application/xml", ResourceHelper.GetResourceStream("Resources/Cas20ValidationFail.xml"));
            var validator = new Cas20ServiceTicketValidator(options, new HttpClient(mockHttp));

            // Act & Assert
            await Assert.ThrowsAsync <AuthenticationException>(() => validator.ValidateAsync(ticket, service, CancellationToken.None));

            mockHttp.VerifyNoOutstandingRequest();
            mockHttp.VerifyNoOutstandingExpectation();
        }
Example #3
0
        public async Task ValidateServiceTicket_FailAsync()
        {
            // Arrange
            var ticket     = Guid.NewGuid().ToString();
            var requestUrl = $"{_fixture.Options.CasServerUrlBase}/serviceValidate?ticket={ticket}&service={Uri.EscapeDataString(_fixture.Service)}";
            var mockHttp   = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, requestUrl)
            .Respond(_fixture.FileProvider.ReadAsHttpContent("Cas20ValidationFail.xml", mediaType: "application/xml"));
            var validator = new Cas20ServiceTicketValidator(_fixture.Options, new HttpClient(mockHttp));

            // Act & Assert
            await Assert.ThrowsAsync <AuthenticationException>(() => validator.ValidateAsync(ticket, _fixture.Service, CancellationToken.None));

            mockHttp.VerifyNoOutstandingRequest();
            mockHttp.VerifyNoOutstandingExpectation();
        }
Example #4
0
        public async Task ValidateServiceTicketWithSuccessXmlResponse_ShouldReturnPrincipal()
        {
            // Arrange
            var ticket     = Guid.NewGuid().ToString();
            var requestUrl = $"{_options.CasServerUrlBase}/serviceValidate?ticket={ticket}&service={Uri.EscapeDataString(_service)}";
            var mockHttp   = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, requestUrl)
            .Respond(_files.ReadAsHttpContent("Cas20ValidationSuccess.xml", mediaType: "application/xml"));
            var validator = new Cas20ServiceTicketValidator(_options, new HttpClient(mockHttp));

            // Act
            var principal = await validator.ValidateAsync(ticket, _service, CancellationToken.None).ConfigureAwait(false);

            //Assert
            Assert.NotNull(principal);
            Assert.NotNull(principal.Assertion);
            Assert.Equal(principal.GetPrincipalName(), principal.Assertion.PrincipalName);
            Assert.Empty(principal.Assertion.Attributes);
            mockHttp.VerifyNoOutstandingRequest();
            mockHttp.VerifyNoOutstandingExpectation();
        }
        public async Task ValidateServiceTicket_SuccessAsync()
        {
            // Arrange
            var ticket     = Guid.NewGuid().ToString();
            var requestUrl = $"{options.CasServerUrlBase}/serviceValidate?ticket={ticket}&service={Uri.EscapeDataString(service)}";
            var mockHttp   = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, requestUrl)
            .Respond("application/xml", ResourceHelper.GetResourceStream("Resources/Cas20ValidationSuccess.xml"));
            var validator = new Cas20ServiceTicketValidator(options, new HttpClient(mockHttp));

            // Act
            var principal = await validator.ValidateAsync(ticket, service, CancellationToken.None);

            //Assert
            Assert.NotNull(principal);
            Assert.NotNull(principal.Assertion);
            Assert.Equal(principal.GetPrincipalName(), principal.Assertion.PrincipalName);
            Assert.Empty(principal.Assertion.Attributes);
            mockHttp.VerifyNoOutstandingRequest();
            mockHttp.VerifyNoOutstandingExpectation();
        }