public void BuildBodyTest()
        {
            var message = new ErrorResponseBuilder()
                          .SetErrorCode(ErrorCode.InvalidClient)
                          .SetErrorDescription("Bad Client")
                          .SetErrorUri("http://error.com")
                          .SetLocation("http://mydomain.com")
                          .SetState("mystate")
                          .BuildBodyMessage();

            var expected = string.Format("{0}={1}&{2}={3}&{4}={5}&{6}={7}",
                                         OAuthTokens.Error, ErrorCode.InvalidClient,
                                         OAuthTokens.ErrorDescription, HttpUtility.UrlEncode("Bad Client"),
                                         OAuthTokens.ErrorUri, HttpUtility.UrlEncode("http://error.com"),
                                         OAuthTokens.State, "mystate");

            Assert.AreEqual(400, message.StatusCode);
            Assert.AreEqual(expected, message.Body);
            Assert.AreEqual(ContentType.FormEncoded, message.ContentType);

            var ex = new OAuthException(ErrorCode.InvalidClient, "Bad Client", "http://error.com", "mystate");

            message = new ErrorResponseBuilder(ex).SetLocation("http://mydomain.com").BuildBodyMessage();

            expected = string.Format("{0}={1}&{2}={3}&{4}={5}&{6}={7}",
                                     OAuthTokens.Error, ErrorCode.InvalidClient,
                                     OAuthTokens.ErrorDescription, HttpUtility.UrlEncode("Bad Client"),
                                     OAuthTokens.ErrorUri, HttpUtility.UrlEncode("http://error.com"),
                                     OAuthTokens.State, "mystate");
            Assert.AreEqual(400, message.StatusCode);
            Assert.AreEqual(expected, message.Body);
            Assert.AreEqual(ContentType.FormEncoded, message.ContentType);
        }
        public void BuildJsonTest()
        {
            var message = new ErrorResponseBuilder()
                          .SetErrorCode(ErrorCode.InvalidClient)
                          .SetErrorDescription("Bad Client")
                          .SetErrorUri("http://error.com")
                          .SetLocation("http://mydomain.com")
                          .SetState("mystate")
                          .BuildJsonMessage();

            Assert.AreEqual(400, message.StatusCode);
            Assert.AreEqual("http://mydomain.com", message.LocationUri);
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.State + "\":\"mystate\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.Error + "\":\"" + ErrorCode.InvalidClient + "\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.ErrorUri + "\":\"http://error.com\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.ErrorDescription + "\":\"Bad Client\""));
            Assert.AreEqual(ContentType.Json, message.ContentType);

            var ex = new OAuthException(ErrorCode.InvalidClient, "Bad Client", "http://error.com", "mystate");

            message = new ErrorResponseBuilder(ex).SetLocation("http://mydomain.com").BuildJsonMessage();

            Assert.AreEqual(400, message.StatusCode);
            Assert.AreEqual("http://mydomain.com", message.LocationUri);
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.State + "\":\"mystate\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.Error + "\":\"" + ErrorCode.InvalidClient + "\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.ErrorUri + "\":\"http://error.com\""));
            Assert.IsTrue(message.Body.Contains("\"" + OAuthTokens.ErrorDescription + "\":\"Bad Client\""));
            Assert.AreEqual(ContentType.Json, message.ContentType);
        }
Exemple #3
0
        //
        // GET: /OAuth/

        public ActionResult Token()
        {
            try
            {
                var oauthRequest = new TokenRequest(new OAuth2Provider.Request.HttpRequest(Request), MvcApplication.ServiceLocator);

                var token = oauthRequest.Authorize();

                if (token.RedirectsUri.HasValue())
                {
                    var redirectUri = OAuthResponse
                                      .TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken)
                                      .SetLocation(token.RedirectsUri)
                                      .BuildQueryMessage().LocationUri;

                    return(Redirect(redirectUri));
                }

                var response = OAuthResponse
                               .TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken)
                               .BuildJsonMessage();

                return(this.OAuth(response));
            }
            catch (OAuthException ex)
            {
                var response = new ErrorResponseBuilder(ex).BuildJsonMessage();
                return(this.OAuth(response));
            }
        }
 public void ExceptionNull(
     string reason,
     ISpecimenContext context,
     ErrorResponseBuilder sut)
 {
     context.MockToReturn(reason);
     ((ErrorResponse)sut.Create(typeof(ErrorResponse), context))
     .Exception.Should().BeNull();
 }
 public void QueriesContextForReason(
     string reason,
     ISpecimenContext context,
     ErrorResponseBuilder sut)
 {
     context.MockToReturn(reason);
     ((ErrorResponse)sut.Create(typeof(ErrorResponse), context))
     .Reason.Should().Be(reason);
 }
 public void ReturnsSuccessful(
     string reason,
     ISpecimenContext context,
     ErrorResponseBuilder sut)
 {
     context.MockToReturn(reason);
     ((ErrorResponse)sut.Create(typeof(ErrorResponse), context))
     .Succeeded.Should().BeTrue();
 }
 public void ReturnsErrorResponse(
     string reason,
     ISpecimenContext context,
     ErrorResponseBuilder sut)
 {
     context.MockToReturn(reason);
     sut.Create(typeof(ErrorResponse), context)
     .Should().BeOfType <ErrorResponse>();
 }
 public void UnrelatedTypeReturnsNoSpecimen(
     string reason,
     ISpecimenContext context,
     ErrorResponseBuilder sut)
 {
     context.MockToReturn(reason);
     sut.Create(typeof(string), context)
     .Should().BeOfType <NoSpecimen>();
 }
        public async Task GetsAsyncConversationError()
        {
            const string errorMessage = "Foo bar";

            var       conv            = new CommandConversation(CommandType.Run);
            var       responseMessage = ErrorResponseBuilder.Build(errorMessage);
            ICoreLink coreLink        = GenerateCoreLink(responseMessage);

            var ex = await Assert.ThrowsAsync <RemoteCoreException>(() => coreLink.Request(conv, WaitMs));

            Assert.Equal(errorMessage, ex.Message);
        }
Exemple #10
0
        /// <summary>
        /// Creates the error response.
        /// </summary>
        /// <param name="setParameters">The set parameters action.</param>
        /// <param name="statusCode">The status code.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">setParameters could not be null</exception>
        protected Response CreateErrorResponse(Action <ErrorResponseBuilder> setParameters, Nancy.HttpStatusCode statusCode = Nancy.HttpStatusCode.BadRequest)
        {
            if (setParameters == null)
            {
                throw new ArgumentException("setParameters could not be null");
            }

            var builder = new ErrorResponseBuilder();

            setParameters(builder);

            return(Response.AsJson(builder.BuildResponse())
                   .WithStatusCode(statusCode));
        }
Exemple #11
0
        private JsonResult BuildErrorResponseFromException(Exception ex)
        {
            var    userMessage = "We had some problems processing your request";
            string developerMessage;

            if (ex is InvalidLoginException)
            {
                developerMessage = ex.Message;
            }
            else
            {
                developerMessage = ex.StackTrace;
            }

            var responseBuilder = new ErrorResponseBuilder();

            return(responseBuilder.BuildErrorResponse(
                       userMessage, developerMessage, 500));
        }
        public void WritesReadsStateResponseError()
        {
            var message = ErrorResponseBuilder.Build("foo");

            Assert.Equal("foo", message.GetResponse(new ErrorResponse()).Message);
        }