コード例 #1
0
        private async Task AssertModelJson(object model, string contentType, bool forceHal, string expected)
        {
            using (var stream = new MemoryStream()) {
                var content = new StringContent("");
                content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                var formatter = new JsonHALMediaTypeFormatter();

                var link     = new Link("self", "href");
                var embedded = new[] {
                    new { bar = true }
                };

                var halModel = new HALResponse(model, new HALModelConfig {
                    ForceHAL = forceHal
                })
                               .AddLinks(link)
                               .AddEmbeddedCollection("bars", embedded);

                Assert.NotNull(formatter);

                await formatter.WriteToStreamAsync(typeof(HALResponse), halModel, stream, content, null);

                // Reset the position to ensure it can read
                stream.Position = 0;

                var    reader = new StreamReader(stream);
                string result = await reader.ReadToEndAsync();

                Assert.Equal(expected, result);
            }
        }
コード例 #2
0
        public void CanWrite_Returns_True_For_Model_With_Attributes()
        {
            var model     = new PersonModelWithAttributes();
            var formatter = new JsonHALMediaTypeFormatter();

            Assert.True(formatter.CanWriteType(model.GetType()));
        }
コード例 #3
0
        public void Adds_Json_Types() {
            var formatter = new JsonHALMediaTypeFormatter(
                jsonMediaTypes: new string[] { "application/test" }
            );

            Assert.NotNull(formatter);
            Assert.Contains("application/test", formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #4
0
        public void Adds_Default_Json_Type() {
            var formatter = new JsonHALMediaTypeFormatter(
                jsonMediaTypes: null
            );

            Assert.NotNull(formatter);
            Assert.Contains(JsonMediaTypeFormatter.DefaultMediaType.MediaType, formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #5
0
        public void Adds_Default_Hal_Type() {
            var formatter = new JsonHALMediaTypeFormatter(
                halJsonMediaTypes: null
            );

            Assert.NotNull(formatter);
            Assert.Contains("application/hal+json", formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #6
0
        public void Adds_Json_Types()
        {
            var formatter = new JsonHALMediaTypeFormatter(
                jsonMediaTypes: new string[] { "application/test" }
                );

            Assert.NotNull(formatter);
            Assert.Contains("application/test", formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #7
0
        public void Adds_Default_Json_Type()
        {
            var formatter = new JsonHALMediaTypeFormatter(
                jsonMediaTypes: null
                );

            Assert.NotNull(formatter);
            Assert.Contains(JsonMediaTypeFormatter.DefaultMediaType.MediaType, formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #8
0
        public void Adds_Default_Hal_Type()
        {
            var formatter = new JsonHALMediaTypeFormatter(
                halJsonMediaTypes: null
                );

            Assert.NotNull(formatter);
            Assert.Contains("application/hal+json", formatter.SupportedMediaTypes.Select(m => m.MediaType));
        }
コード例 #9
0
        private async Task AssertModelJson(object model, string contentType, bool forceHal, string expected) {
            using (var stream = new MemoryStream()) {
                var content = new StringContent("");
                content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                var formatter = new JsonHALMediaTypeFormatter();

                var link = new Link("self", "href");
                var embedded = new[] {
                    new { bar = true }
                };

                var halModel = new HALResponse(model, new HALModelConfig {
                    ForceHAL = forceHal
                })
                .AddLinks(link)
                .AddEmbeddedCollection("bars", embedded);

                Assert.NotNull(formatter);

                await formatter.WriteToStreamAsync(typeof(HALResponse), halModel, stream, content, null);

                // Reset the position to ensure it can read
                stream.Position = 0;

                var reader = new StreamReader(stream);
                string result = await reader.ReadToEndAsync();

                Assert.Equal(expected, result);
            }
        }
コード例 #10
0
        public void CanWrite_Returns_True_For_Model_With_Attributes()
        {
            var model = new PersonModelWithAttributes();
            var formatter = new JsonHALMediaTypeFormatter();

            Assert.True(formatter.CanWriteType(model.GetType()));
        }