Esempio n. 1
0
 /// <summary>
 /// Create a instance of a ProblemContent object from a ProblemDocument
 /// </summary>
 /// <param name="problemDocument"></param>
 public ProblemContent(ProblemDocument problemDocument)
 {
     // Problem documents tend to be small so we should serialize them immediately so that we can return the length of the stream.
     // This should prevent the host from inadverently chunking the response because it doesn't know the size of the response. 
     _problemStream = new MemoryStream();
     problemDocument.Save(_problemStream);
     _problemStream.Position = 0;
     Headers.ContentType = new MediaTypeHeaderValue("application/problem+json");
 }
 /// <summary>
 /// Create a instance of a ProblemContent object from a ProblemDocument
 /// </summary>
 /// <param name="problemDocument"></param>
 public ProblemContent(ProblemDocument problemDocument)
 {
     // Problem documents tend to be small so we should serialize them immediately so that we can return the length of the stream.
     // This should prevent the host from inadverently chunking the response because it doesn't know the size of the response.
     _problemStream = new MemoryStream();
     problemDocument.Save(_problemStream);
     _problemStream.Position = 0;
     Headers.ContentType     = new MediaTypeHeaderValue("application/problem+json");
 }
        /// <summary>
        /// Reading ProblemDocument intstance from HttpContent
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static Task <ProblemDocument> ReadAsProblemAsync(this HttpContent content)
        {
            if (content.Headers.ContentType.MediaType.ToLowerInvariant() != "application/problem+json")
            {
                throw new ArgumentException("Cannot process HttpContent with media type " + content.Headers.ContentType.MediaType);
            }

            return(content.ReadAsStreamAsync().ContinueWith(t => ProblemDocument.Parse(t.Result)));
        }
Esempio n. 4
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);

        }
        /// <summary>
        ///  Create problem document instance from JObject
        /// </summary>
        /// <param name="jObject"></param>
        /// <returns></returns>
        public static ProblemDocument Parse(JObject jObject)
        {
            var doc = new ProblemDocument();


            foreach (var jProp in jObject.Properties())
            {
                switch (jProp.Name)
                {
                case "type":
                    doc.ProblemType = new Uri((string)jProp.Value, UriKind.RelativeOrAbsolute);
                    break;

                case "title":
                    doc.Title = (string)jProp.Value;
                    break;

                case "status":
                    doc.StatusCode = (HttpStatusCode)(int)jProp.Value;
                    break;

                case "detail":
                    doc.Detail = (string)jProp.Value;
                    break;

                case "instance":
                    doc.ProblemInstance = new Uri((string)jProp.Value, UriKind.RelativeOrAbsolute);
                    break;

                default:
                    doc.Extensions.Add(jProp.Name, jProp.Value);
                    break;
                }
            }

            if (doc.ProblemType == null)
            {
                throw new ArgumentException("Missing problemType property");
            }
            if (string.IsNullOrEmpty(doc.Title))
            {
                throw new ArgumentException("Missing title property");
            }

            return(doc);
        }
Esempio n. 6
0
        public void ParseProblemDocument()
        {
            var stream = this.GetType().Assembly.GetManifestResourceStream("Hapikit.net.Tests.problem.json");
            Func <string, Uri> typeProducer = (o) => new Uri(o);
            var vocab = new VocabTerm <ProblemDocument>();

            vocab.MapProperty <string>("type", (s, o) => { s.ProblemType = typeProducer(o); });
            vocab.MapProperty <string>("title", (s, o) => { s.Title = o; });
            vocab.MapProperty <string>("detail", (s, o) => { s.Detail = o; });
            vocab.MapProperty <string>("instance", (s, o) => { s.ProblemInstance = new Uri(o, UriKind.RelativeOrAbsolute); });


            var problem = new Tavis.ProblemDocument();

            JsonStreamingParser.ParseStream(stream, problem, vocab);

            Assert.Equal("https://example.com/probs/out-of-credit", problem.ProblemType.OriginalString);
            Assert.Equal("You do not have enough credit.", problem.Title);
        }
Esempio n. 7
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);

        }
Esempio n. 8
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);
            
        }
Esempio n. 9
0
        /// <summary>
        ///  Create problem document instance from JObject
        /// </summary>
        /// <param name="jObject"></param>
        /// <returns></returns>
        public static ProblemDocument Parse(JObject jObject)
        {
            var doc = new ProblemDocument();


            foreach (var jProp in jObject.Properties()){

                switch (jProp.Name)
                {
                    case "type":
                        doc.ProblemType = new Uri((string)jProp.Value, UriKind.RelativeOrAbsolute);
                        break;
                    case "title":
                        doc.Title = (string)jProp.Value;
                        break;
                    case "status":
                        doc.StatusCode = (HttpStatusCode)(int)jProp.Value;
                        break;
                    case "detail":
                        doc.Detail = (string)jProp.Value;
                        break;
                    case "instance":
                        doc.ProblemInstance = new Uri((string) jProp.Value);
                        break;
                    default:
                        doc.Extensions.Add(jProp.Name,jProp.Value);                
                        break;
                }
    
            }

            if (doc.ProblemType == null) throw new ArgumentException("Missing problemType property");
            if (string.IsNullOrEmpty(doc.Title)) throw new ArgumentException("Missing title property");

            return doc;
        }
Esempio n. 10
0
        public void ParseProblemDocument()
        {
            var stream = this.GetType().Assembly.GetManifestResourceStream("Hapikit.net.Tests.problem.json");
            Func<string, Uri> typeProducer = (o) => new Uri(o); 
            var vocab = new VocabTerm<ProblemDocument>();
            
            vocab.MapProperty<string>("type", (s, o) => { s.ProblemType = typeProducer(o); });
            vocab.MapProperty<string>("title", (s, o) => { s.Title = o; });
            vocab.MapProperty<string>("detail", (s, o) => { s.Detail = o; });
            vocab.MapProperty<string>("instance", (s, o) => { s.ProblemInstance = new Uri(o,UriKind.RelativeOrAbsolute); });


            var problem = new Tavis.ProblemDocument();
            JsonStreamingParser.ParseStream(stream, problem, vocab);

            Assert.Equal("https://example.com/probs/out-of-credit", problem.ProblemType.OriginalString);
            Assert.Equal("You do not have enough credit.", problem.Title);
        }