public async Task MessagePackFormatter()
        {
            var person = new User
            {
                UserId      = 1,
                FullName    = "John Denver",
                Age         = 35,
                Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
            };

            var messagePackBinary = MessagePackSerializer.Serialize(person);

            // OutputFormatter
            var outputFormatterContext = GetOutputFormatterContext(person, typeof(User), MsgPackContentType);
            var outputFormatter        = new MessagePackOutputFormatter();

            outputFormatter.CanWriteResult(outputFormatterContext).IsTrue();

            await outputFormatter.WriteAsync(outputFormatterContext);

            var body = outputFormatterContext.HttpContext.Response.Body;

            Assert.NotNull(body);
            body.Position = 0;

            using (var ms = new MemoryStream())
            {
                await body.CopyToAsync(ms);

                Assert.Equal(messagePackBinary, ms.ToArray());
            }

            // InputFormatter
            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
            httpContext.Request.Body        = new NonSeekableReadStream(messagePackBinary);
            httpContext.Request.ContentType = MsgPackContentType;

            var inputFormatterContext = CreateInputFormatterContext(typeof(User), httpContext);

            var inputFormatter = new MessagePackInputFormatter();

            inputFormatter.CanRead(inputFormatterContext).IsTrue();

            var result = await inputFormatter.ReadAsync(inputFormatterContext);

            Assert.False(result.HasError);

            var userModel = Assert.IsType <User>(result.Model);

            userModel.IsStructuralEqual(person);
        }
        public void MessagePackFormatterCanNotRead()
        {
            var person = new User();

            // OutputFormatter
            var outputFormatterContext = GetOutputFormatterContext(person, typeof(User), "application/json");
            var outputFormatter        = new MessagePackOutputFormatter();

            outputFormatter.CanWriteResult(outputFormatterContext).IsFalse();

            // InputFormatter
            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
            httpContext.Request.Body        = new NonSeekableReadStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(person)));
            httpContext.Request.ContentType = "application/json";

            var inputFormatterContext = CreateInputFormatterContext(typeof(User), httpContext);

            var inputFormatter = new MessagePackInputFormatter();

            inputFormatter.CanRead(inputFormatterContext).IsFalse();
        }
示例#3
0
        public void MessagePackOutputFormatterSupportsXMsgPack()
        {
            var outputFormatter = new MessagePackOutputFormatter();

            outputFormatter.SupportedMediaTypes.Is(MsgPackContentType);
        }