Exemple #1
0
        public async Task BeforeRequestAsyncTest()
        {
            var apiAction = new DefaultApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"));
            var context   = new TestRequestContext(apiAction, new
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            });

            context.HttpContext.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.HttpContext.RequestMessage.Method     = HttpMethod.Post;

            var attr = new JsonContentAttribute()
            {
                CharSet = "utf-16"
            };
            await attr.OnRequestAsync(new ApiParameterContext(context, 0));

            var body = await context.HttpContext.RequestMessage.Content.ReadAsUtf8ByteArrayAsync();

            var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;

            using var buffer = new RecyclableBufferWriter <byte>();
            JsonBufferSerializer.Serialize(buffer, context.Arguments[0], options);
            var target = buffer.WrittenSpan.ToArray();

            Assert.True(body.SequenceEqual(target));
        }
Exemple #2
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new TestActionContext(
                httpApi: null,
                httpApiConfig: new HttpApiConfig(),
                apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync")));

            context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.RequestMessage.Method     = HttpMethod.Post;


            var parameter = context.ApiActionDescriptor.Parameters[0].Clone(new
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            });

            var attr = new JsonContentAttribute();

            await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter);

            var body = await context.RequestMessage.Content.ReadAsStringAsync();

            var options = context.HttpApiConfig.FormatOptions.CloneChange(attr.DateTimeFormat);
            var target  = context.HttpApiConfig.JsonFormatter.Serialize(parameter.Value, options);

            Assert.True(body == target);
        }
Exemple #3
0
        public async Task BeforeRequestAsyncTest()
        {
            var apiAction = new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"));
            var context   = new TestRequestContext(apiAction, new
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            });

            context.HttpContext.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.HttpContext.RequestMessage.Method     = HttpMethod.Post;

            var attr = new JsonContentAttribute();
            await attr.OnRequestAsync(new ApiParameterContext(context, 0));

            var body = await context.HttpContext.RequestMessage.Content.ReadAsByteArrayAsync();

            var options = context.HttpContext.Options.JsonSerializeOptions;
            var target  = context.HttpContext.Services.GetService <IJsonFormatter>().Serialize(context.Arguments[0], options);

            Assert.True(body.SequenceEqual(target));
        }