コード例 #1
0
 public ProblemJsonResponse(ProblemDocument problemDocument)
 {
     ProblemDocument = problemDocument;
     Contents        = problemDocument.Save;
     ContentType     = "application/problem+json";
     StatusCode      = (HttpStatusCode)problemDocument.StatusCode.GetValueOrDefault();
 }
コード例 #2
0
        public void ProblemJsonResponse_Should_expose_problem_document_as_property()
        {
            // given
            var document = new ProblemDocument();
            var response = new ProblemJsonResponse(document);

            // then
            response.ProblemDocument.ShouldBeSameAs(document);
        }
コード例 #3
0
        public void ProblemDocumentConst1()
        {
            var problemDocument = new ProblemDocument("Type", "Title", HttpStatusCode.Conflict, "war ein Konflikt");

            Assert.AreEqual("Title", problemDocument.Title);
            Assert.AreEqual("Type", problemDocument.Type);
            Assert.AreEqual(HttpStatusCode.Conflict, problemDocument.Status);
            Assert.AreEqual("war ein Konflikt", problemDocument.Detail);
        }
コード例 #4
0
        public void Process_Should_return_correct_Response()
        {
            // given
            var doc = new ProblemDocument();

            // when
            var response = _processor.Process(new MediaRange("application/json"), doc, new NancyContext());

            // thne
            response.ShouldBeOfType <ProblemJsonResponse>();
            ((ProblemJsonResponse)response).ProblemDocument.ShouldBeSameAs(doc);
        }
コード例 #5
0
        public void CreateAProblem()
        {
            var problem = new ProblemDocument
            {
                ProblemType     = new Uri("http://example.org"),
                Title           = "Houston we have a problem",
                StatusCode      = HttpStatusCode.BadGateway,
                ProblemInstance = new Uri("http://foo")
            };

            Assert.NotNull(problem);
        }
コード例 #6
0
        public void CreateAProblemWithARelativeInstanceUrl()
        {
            var problem = new ProblemDocument
            {
                ProblemType     = new Uri("http://example.org"),
                Title           = "Houston we have a problem",
                StatusCode      = HttpStatusCode.BadGateway,
                ProblemInstance = new Uri("foo")
            };

            Assert.NotNull(problem);
            Assert.Equal(problem.ProblemInstance.OriginalString, "foo");
        }
コード例 #7
0
        public void ProblemDocumentConst2()
        {
            var problemDocument = new ProblemDocument("Type", "Title", new List <DomainError>
            {
                new TestError("irgend ein error")
            });

            Assert.AreEqual("Title", problemDocument.Title);
            Assert.AreEqual("Type", problemDocument.Type);
            Assert.AreEqual(HttpStatusCode.BadRequest, problemDocument.Status);
            Assert.AreEqual("irgend ein error", problemDocument.ProblemDetails.First().Detail);
            Assert.AreEqual("TestError", problemDocument.ProblemDetails.First().Type);
        }
コード例 #8
0
        public void When_set_up_to_handle_status_code_Handle_should_set_ProblemDocument_status_code()
        {
            // given
            var nancyContext    = new NancyContext();
            var problemDocument = new ProblemDocument();

            _handler.SetHandler(HttpStatusCode.BadGateway, problemDocument);

            // when
            _handler.Handle(HttpStatusCode.BadGateway, nancyContext);

            // then
            ((ProblemJsonResponse)nancyContext.Response).ProblemDocument.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadGateway);
        }
コード例 #9
0
        public void When_not_set_up_to_handle_status_code_Handle_should_set_ProblemJsonResponse_with_returned_ProblemDocument()
        {
            // given
            var nancyContext    = new NancyContext();
            var problemDocument = new ProblemDocument();

            _handler.SetHandler(HttpStatusCode.BadGateway, problemDocument);

            // when
            _handler.Handle(HttpStatusCode.BadGateway, nancyContext);

            // then
            nancyContext.Response.ShouldBeOfType <ProblemJsonResponse>();
            ((ProblemJsonResponse)nancyContext.Response).ProblemDocument.ShouldBeSameAs(problemDocument);
        }
コード例 #10
0
        public void ReturnAProblem()
        {
            var problem = new ProblemDocument
            {
                ProblemType     = new Uri("http://example.org"),
                Title           = "Houston we have a problem",
                StatusCode      = (HttpStatusCode?)428,
                ProblemInstance = new Uri("http://foo")
            };

            problem.Extensions.Add("bar", new JValue("100"));

            var response = new HttpResponseMessage(HttpStatusCode.BadGateway)
            {
                Content = new ProblemContent(problem)
            };

            var problemString = response.Content.ReadAsStringAsync().Result;

            Assert.NotEmpty(problemString);
        }
コード例 #11
0
        public void RoundTripAProblem()
        {
            var problem = new ProblemDocument
            {
                ProblemType     = new Uri("http://example.org"),
                Title           = "Houston we have a problem",
                StatusCode      = HttpStatusCode.BadGateway,
                ProblemInstance = new Uri("http://foo")
            };

            problem.Extensions.Add("bar", new JValue("100"));

            var ms = new MemoryStream();

            problem.Save(ms);

            ms.Position = 0;

            var problem2 = ProblemDocument.Parse(ms);

            Assert.Equal(problem, problem2);
        }
コード例 #12
0
        public async Task <ActionResult> Post([FromBody] UserDto user)
        {
            var hasher = new PasswordHasher <MongoExternalUser>();

            try
            {
                var mongoExternalUser = await _userStore.AutoProvisionUser("IdSrv", user.Username, new List <Claim>
                {
                    new Claim(JwtClaimTypes.Name, user.Username),
                    new Claim(JwtClaimTypes.Email, user.Email)
                });

                var hash = hasher.HashPassword(mongoExternalUser, user.Password);
                await _userStore.SetPasswordHashForUser(mongoExternalUser, hash);

                return(await Task.FromResult(new OkResult()));
            }
            catch (Exception e)
            {
                if (e is UserExistsException)
                {
                    var problem = new ProblemDocument
                    {
                        ProblemType = new Uri("http://tempuri.org/errors/user-already-exists"),
                        Title       = $"User {user.Username} already exists",
                        StatusCode  = HttpStatusCode.BadRequest
                    };

                    var contentResult = new ContentResult();
                    contentResult.Content     = await new ProblemContent(problem).ReadAsStringAsync();
                    contentResult.ContentType = "application/problem+json";
                    contentResult.StatusCode  = (int?)HttpStatusCode.BadRequest;
                    return(await Task.FromResult(contentResult));
                }

                return(await Task.FromResult(new StatusCodeResult(500)));
            }
        }
コード例 #13
0
 public BadRequestResult(ProblemDocument problem)
     : base(null)
 {
     _problem = problem;
 }
コード例 #14
0
 public void SetHandler(HttpStatusCode httpStatusCode, ProblemDocument problemDocument = null)
 {
     problemDocument = problemDocument ?? new ProblemDocument();
     When((code, context) => code == httpStatusCode, context => problemDocument);
 }