Reputation: 1071
I have created a web performance test in VS 2015, and am trying to set request headers. When setting headers such as "Referer" or "User-Agent" everything seems to work fine. I just set the header by right-clicking the request and adding a new header, and when I run the test and inspect the request I can see that the new header has been added. However if I try to add a new header named "Content-Length" and then run the test, this header is missing from the request. I'm getting an http error which I assume is because VS doesn't use the "Content-Length" header:
HTTP Error 411. The request must be chunked or have a content length.
Upvotes: 3
Views: 1638
Reputation: 615
I ran into this problem as well. The consistent trend with these issues online seems to be when the content-length is 0. Otherwise VS seems to dynamically come up with the content-length header on it's own.
Explanation of Problem
Esentially, I don't think you can add it manually. VS wants to add this itself, so the real problem is why isn't VS adding this?
The problem comes from the request being simply a "Request" and not a "Web Service Request", the significant difference here being that the latter includes the "String Body" component. I'm assuming that VS does this because the content-length it recorded was 0. I recorded my workflow in Fiddler and got the same result. I would also assume that the reason adding the Content-Length header doesn't work is because this gets calculated by Visual Studio if the request has a body in the request.
The Workaround
Make the request a "Web Service Request" so that it includes the String Body component. In the test, right click and choose "Insert Web Service Request".
I got clued into this from the below post (which also includes an example for a coded UI test): https://social.msdn.microsoft.com/Forums/en-US/da492346-760e-4971-a666-8897ae7b35e3/length-required-error?forum=vstswebtest
Plugin Option
I couldn't find a way to just convert my existing request into a Web Service Request, so I created a WebTestRequestPlugin that can be added to a request to dynamically add a body to the request.
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.ComponentModel;
namespace YourProjectName
{
[DisplayName("Add Request Body")]
[Description("If a request is intended to have a content-length of 0, the Web Test won't include an empty String Body and the Content-Length header won't " +
"be added during test execution. This may result in a 411 - Length required error. This plugin can be added to that request to add a body to the request" +
"during test execution.")]
public class AddBodyToRequestPlugin : WebTestRequestPlugin
{
public AddBodyToRequestPlugin()
{
}
public override void PreRequest(object sender, PreRequestEventArgs e)
{
// Add string body if it doesn't exist yet.
if ( e.Request.Body == null )
{
e.Request.Body = new StringHttpBody();
}
}
}
}
Upvotes: 2