Repository: hapijs/hapi (JavaScript)

Issue:
Content-type header is null when empty response is returned
<!--
  ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️
  You must complete this entire issue template to receive support. You MUST NOT remove, change, or replace the template with your own format. A missing or incomplete report will cause your issue to be closed without comment. Please respect the time and experience that went into this template. It is here for a reason. Thank you!
  ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️
-->

#### Support plan

<!--
We are here to help!

Community based support is, by its nature, limited to available community members able to help. Most community support issues are resolved within 2 weeks. Before submitting an issue, please review the various support options available at https://hapi.dev/support/. That page includes useful information about different ways to ask questions.
-->

* *is this issue currently blocking your project?* (no):
* *is this issue affecting a production system?* (yes):

#### Context

* *node version*: 12.16.3
* *module version with issue*: 19.2.0
* *last module version without issue*:  19.1.1
* *environment* (e.g. node, browser, native): node 
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi application
* *any other relevant information*:  
After updating to v19.2.0 from v19.1.1 it was noticed that the headers of responses with no content now contain the content-type header with the value of null. 
Before v19.2.0, the content-type header was omitted for such responses.

#### What are you trying to achieve or the steps to reproduce?

<!--
Describe your issue in detail, including full steps to reproduce the issue, any configuration, schemas, code samples, or inputs needed. Make sure to wrap all code examples in backticks so that they display correctly. Before submitting an issue, make sure to click on the Preview tab above to verify everything is formatted correctly.
-->

The issue can be reproduced by returning an empty response from a route handler.
I suspect this may be due to changes here: https://github.com/hapijs/hapi/commit/ed0688c596d65ada38bea000ab6726c823a0abd9

```js
  server.route({
      method: "GET",
      path: "/test",
      handler: (request, h) => {
         // response headers contain content-type as null on v19.2.0
         // response headers omit content-type  header on v19.1.1 and lower
        return h.response().code(204);
      },
  });
```

#### What was the result you got?
response headers containing content-type of null

#### What result did you expect?
response headers not containing content-type header



Discussion:
Confirmed that ed0688c596d65ada38bea000ab6726c823a0abd9 is the offending commit. Fix incoming soon.



Patch:
diff --git a/lib/headers.js b/lib/headers.js
index 19e68b4a4..f3cb8f549 100755
--- a/lib/headers.js
+++ b/lib/headers.js
@@ -122,7 +122,7 @@ exports.type = function (request) {
 
     const response = request.response;
     const type = response.contentType;
-    if (type !== response.headers['content-type']) {
+    if (type !== null && type !== response.headers['content-type']) {
         response.type(type);
     }
 };


Test patch:
diff --git a/test/headers.js b/test/headers.js
index e0a23c71a..70edba394 100755
--- a/test/headers.js
+++ b/test/headers.js
@@ -523,5 +523,15 @@ describe('Headers', () => {
             const res = await server.inject('/?callback=me');
             expect(res.payload).to.equal('test');
         });
+
+        it('does not set content-type by default on 204 response', async () => {
+
+            const server = Hapi.server();
+            server.route({ method: 'GET', path: '/', handler: (request, h) => h.response().code(204) });
+
+            const res = await server.inject('/');
+            expect(res.statusCode).to.equal(204);
+            expect(res.headers['content-type']).to.equal(undefined);
+        });
     });
 });