예제 #1
0
        public void AuthorizationTokenInjectionFails()
        {
            _restStubHostActivator.Host
            .Given(
                Request.Create()
                .WithPath("/sts/token")
                .WithHeader("Authorization", $"Bearer {SsoSettings.ApiKey}")
                .WithHeader("Accept", "application/json")
                .UsingGet()
                )
            .RespondWith(
                Response.Create()
                .WithStatusCode(HttpStatusCode.InternalServerError)
                .WithHeader("Content-Type", "application/json")
                .WithBody("{\"Message\":\"Authorization header not present\"}")
                );

            var url = RestStubHostActivator.Uri.AppendPathSegment("/api/resource");
            var requestBodyStream = new MultipartFormDataContentStream(new StringStream("dummy"));
            var request           = WcfWebRequestFactory.Create(url, requestBodyStream);

            request.SetProperty(AuthorizationProperties.TokenId, "token-cache-key");
            var httpRequestHeaders = request.GetHttpRequestMessage().Headers;

            httpRequestHeaders.Add(HttpRequestHeader.Accept, "application/xml");
            httpRequestHeaders.Add(HttpRequestHeader.ContentType, requestBodyStream.ContentType);

            Invoking(() => RestClient.WithCustomBehaviors(WebHttpAuthorizationBehavior).Invoke(request))
            .Should().Throw <HttpRequestException>()
            .WithMessage(
                $"Failed to get token from '{AuthorizationTokenServiceUri}'.\r\n"
                + "The remote server returned an error: (500) Internal Server Error.\r\n"
                + "Authorization header not present.");
        }
예제 #2
0
        public void InvokeAuthorizationTokenServiceClientThrowsWhenAuthorizationTokenServiceClientReturnsNoAuthorizationToken()
        {
            var url     = RestStubHostActivator.Uri.AppendPathSegment("/api/resource");
            var request = WcfWebRequestFactory.Create(url, new StringStream("dummy"));

            var authorizationTokenIdentifierMock = new Mock <IAuthorizationTokenIdentifier>();

            authorizationTokenIdentifierMock.Setup(m => m.Key).Returns("token-cache-key");
            AuthorizationTokenServiceMiddleware.Mock
            .Setup(m => m.CreateAuthorizationTokenIdentifier(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Returns(authorizationTokenIdentifierMock.Object);

            var authorizationTokenServiceClient = new Mock <IAuthorizationTokenServiceClient>();

            authorizationTokenServiceClient
            .Setup(m => m.GetAuthorizationToken(It.IsAny <IAuthorizationTokenIdentifier>()))
            .Returns((IAuthorizationToken)null);
            AuthorizationTokenServiceMiddleware.Mock
            .Setup(m => m.CreateAuthorizationTokenServiceClient(It.IsAny <Uri>(), It.IsAny <Uri>()))
            .Returns(authorizationTokenServiceClient.Object);

            var webHttpAuthorizationBehavior = new WebHttpAuthorizationBehavior(typeof(AuthorizationTokenServiceMiddleware), AuthorizationTokenServiceUri);

            Invoking(() => RestClient.WithCustomBehaviors(webHttpAuthorizationBehavior).Invoke(request))
            .Should().Throw <InvalidOperationException>()
            .WithMessage(
                $"AuthorizationTokenServiceClient '{authorizationTokenServiceClient.Object.GetType().Name}''s {nameof(IAuthorizationTokenServiceClient.GetAuthorizationToken)} returned no {nameof(IAuthorizationToken)}.");
        }
        public void FaultResponseHasPropertiesPropagatedFromRequest()
        {
            var url = RestStubHostActivator.Uri.AppendPathSegment("/api/resource");

            _restStubHostActivator.Host
            .Given(
                Request.Create()
                .WithPath(url.Path)
                .UsingGet()
                )
            .RespondWith(
                Response.Create()
                .WithStatusCode(HttpStatusCode.InternalServerError)
                );

            var request = WcfWebRequestFactory.Create(url);

            request.SetProperty(BtsProperties.InterchangeID, "interchange");
            var response = RestClient.WithCustomBehaviors(PropertyPropagationBehavior).Invoke(request);

            response.Should().NotBeNull();
            response.IsFault.Should().BeTrue();
            response !.Properties
            .Should().NotContainKey(MessagePropertiesExtensions.PROPERTIES_TO_PROMOTE_KEY)
            .And.ContainKey(MessagePropertiesExtensions.PROPERTIES_TO_WRITE_KEY);
            response.Properties.GetPropertiesToWrite()
            .Should().NotBeNull()
            .And.BeEquivalentTo(new KeyValuePair <XmlQualifiedName, object>(BtsProperties.InterchangeID.QName, "interchange"));
        }
        public void ResponseHasPropertiesPropagatedFromRequest()
        {
            var url = RestStubHostActivator.Uri.AppendPathSegment("/api/resource");

            _restStubHostActivator.Host
            .Given(
                Request.Create()
                .WithPath(url.Path)
                .UsingGet()
                )
            .RespondWith(
                Response.Create()
                .WithHeader("Content-Type", "application/xml")
                .WithBody("<root>dummy</root>")
                .WithStatusCode(HttpStatusCode.OK)
                );

            var request = WcfWebRequestFactory.Create(url);

            request.SetProperty(BtsProperties.MessageType, "message#type");
            request.SetProperty(BtsProperties.InterchangeID, "interchange");
            var response = RestClient.WithCustomBehaviors(PropertyPropagationBehavior).Invoke(request);

            response.Should().NotBeNull();
            response.IsEmpty.Should().BeFalse();
            response !.Properties
            .Should().ContainKey(MessagePropertiesExtensions.PROPERTIES_TO_PROMOTE_KEY)
            .And.ContainKey(MessagePropertiesExtensions.PROPERTIES_TO_WRITE_KEY);
            response.Properties.GetPropertiesToPromote()
            .Should().NotBeNull()
            .And.BeEquivalentTo(new[] { new KeyValuePair <XmlQualifiedName, object>(BtsProperties.MessageType.QName, "message#type") });
            response.Properties.GetPropertiesToWrite()
            .Should().NotBeNull()
            .And.BeEquivalentTo(new[] { new KeyValuePair <XmlQualifiedName, object>(BtsProperties.InterchangeID.QName, "interchange") });
        }
예제 #5
0
        public void AuthorizationTokenInjectionSucceeds()
        {
            var authorizationToken = $"cryptographic-client-authorization-token-{Guid.NewGuid():D}";
            var url = RestStubHostActivator.Uri.AppendPathSegment("/api/resource");

            _restStubHostActivator.Host
            .Given(
                Request.Create()
                .WithPath("/sts/token")
                .WithHeader("Authorization", $"Bearer {SsoSettings.ApiKey}")
                .WithHeader("Accept", "application/json")
                .UsingGet()
                )
            .RespondWith(
                Response.Create()
                .WithStatusCode(HttpStatusCode.OK)
                .WithHeader("Content-Type", "application/json")
                .WithBody($"{{\"Token\":\"{authorizationToken}\",\"Expires\":\"{DateTime.UtcNow:O}\"}}")
                );
            _restStubHostActivator.Host
            .Given(
                Request.Create()
                .WithPath(url.Path)
                .WithHeader("Authorization", $"Bearer {authorizationToken}")
                .WithHeader("Accept", "application/xml")
                .WithHeader("Content-Type", new RegexMatcher(Regex.Escape("multipart/form-data; boundary=\"")))
                .UsingPost()
                )
            .RespondWith(
                Response.Create()
                .WithHeader("Content-Type", "application/xml")
                .WithBody("<root>dummy</root>")
                .WithStatusCode(HttpStatusCode.OK)
                );

            var requestBodyStream = new MultipartFormDataContentStream(new StringStream("dummy"));
            var request           = WcfWebRequestFactory.Create(url, requestBodyStream);

            request.SetProperty(AuthorizationProperties.TokenId, "token-cache-key");
            var httpRequestHeaders = request.GetHttpRequestMessage().Headers;

            httpRequestHeaders.Add(HttpRequestHeader.Accept, "application/xml");
            httpRequestHeaders.Add(HttpRequestHeader.ContentType, requestBodyStream.ContentType);

            var response = RestClient.WithCustomBehaviors(WebHttpAuthorizationBehavior).Invoke(request);

            response.Should().NotBeNull();
            var httpResponse = response.GetHttpResponseMessage();

            httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
        }