question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Does the WireMock send Content-Length response header

See original GitHub issue

Hello,

I need to make a response mock with a specific value of Content-Length header:

        [TestMethod]
        public async Task CheckMockHeaders() {
            var responseBody = @"<?xml version=""1.0""?><empty/>";
            mockServer
                .Given(Request
                    .Create()
                    .WithPath("/test")
                    .UsingPost())
                .RespondWith(Response
                    .Create()
                    .WithStatusCode(200)
                    .WithHeader("content-type", "text/xml")
                    .WithHeader("Content-Length", responseBody.Length.ToString())
                    .WithBody(responseBody));
            
            using (var httpClient = new HttpClient()) {
                var uri = mockServer.Urls.Single() + "test";
                using (var response = await httpClient.PostAsync(uri, new StringContent(""))) {
                    response.EnsureSuccessStatusCode();
                    Assert.IsTrue(response.Content.Headers.Any(x => x.Key == "Content-Length"));
                }
            }
        }

Unfortunately the Assert fails and the Content-Length is not part of response.Content.Headers.

I suspect problem might be in the fact that the server by default assigns header Transfer-Encoding: chunked.

According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

Data is sent in a series of chunks. The Content-Length header is omitted in this case and at the beginning of each chunk you need to add the length of the current chunk in hexadecimal format…

I was not able to assign my own Transfer-Encoding: identity. I will create a separate question about it.

WireMock.Net version 1.0.3.16.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
StefHcommented, May 17, 2018

You are correct. I think that I switched Request en Response headers in my implementation.

I had some troubles some time ago when setting the Content-Length header on the IOwinResponse or HttpResponse object.

I will check that code again, and keep you informed.

0reactions
StefHcommented, May 25, 2018

Solved, see NuGet 1.0.3.18

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting Content-Length HTTP header in response using ...
I was able to get it to respect the header when I added the route by posting a request to the server. @Test...
Read more >
Mock API Response Templating
Response headers and bodies, as well as proxy URLs, can optionally be rendered using Handlebars templates. This enables attributes of the request to...
Read more >
11. Spring Cloud Contract WireMock
This contract specifies that any valid POST with an "id" field receives the response defined in this test. You can chain together calls...
Read more >
What is the Content-Length header?
A Content-Length header is a number that indicates the size of the data in the body of the request or response in bytes....
Read more >
WireMock.Net changelog - Awesome .NET - LibHunt
Net is a flexible product for stubbing and mocking web HTTP responses using ... #136 - Question: Does the WireMock send Content-Length response...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found