如何在VS WebTest中发布Json对象?

问题描述 投票:2回答:3

我正在Visual Studio 2015 Enterprise中编写一些webtests来对我的API进行负载测试。

我的一些API调用期望Json对象作为请求的主体。但webtest界面似乎没有任何方法可以直接设置Post请求的主体;您可以添加键和值,但不能将请求设置为隐式序列化的对象,甚至只能是纯字符串。

那么你如何在网络测试中发布Json?

c# json visual-studio load-testing webtest
3个回答
6
投票

根据您的需要,有两种选择,可能更多。这两种机制都具有相同的属性集来设置URL和许多其他字段。

在Web测试编辑器中,您可以添加Web服务请求(Insert web service request上下文菜单命令),然后将其设置为StringBody字段。字符串主体的内容可以包含上下文参数。

普通请求的上下文菜单有一个Add file upload parameter


0
投票

在搜索了一段时间后,我找到了一个解决方案,允许您通过Web测试的post请求发送JSON对象。

1)创建一个Web测试性能插件:https://docs.microsoft.com/en-us/visualstudio/test/how-to-create-a-web-performance-test-plug-in?view=vs-2017

2)将以下代码添加到类中(不要忘记构建):

using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebTests.RequestPlugins
{
    [DisplayName("Add JSON content to Body")]
    [Description("HEY! Tip! Uglify your JSON before pasting.")]
    public class AddJsonContentToBody : WebTestRequestPlugin
    {
        [Description("Assigns the HTTP Body payload to the content provided and sets the content type to application/json")]
        public string JsonContent { get; set; }

    public override void PreRequest(object sender, PreRequestEventArgs e)
    {
        var stringBody = new StringHttpBody();
        stringBody.BodyString = JsonContent;
        stringBody.ContentType = "application/json";

        e.Request.Body = stringBody;
    }
}

3)在您的请求URL r-单击并选择“添加请求插件”,然后您应该看到您的新插件。

4)将你的json弄脏以便能够粘贴。

5)运行测试

来源代码:https://ericflemingblog.wordpress.com/2016/01/21/helpful-custom-request-plugins-for-web-tests/


0
投票

在web测试 - >字符串主体 - >内容类型属性中,

  • 输入值“application / json”
  • 将json添加到字符串主体

Content Type Picture for Json Data

© www.soinside.com 2019 - 2024. All rights reserved.