From 871cea19b76c0d3f9e98e1d8cc445896ac412d30 Mon Sep 17 00:00:00 2001
From: Sebastian Poxhofer <secustor@users.noreply.github.com>
Date: Thu, 1 Dec 2022 18:48:45 +0100
Subject: [PATCH] feat(http): set by default Accept header for json requests
 (#19202)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/util/http/github.spec.ts |  2 +-
 lib/util/http/index.spec.ts  | 27 +++++++++++++++++++++++----
 lib/util/http/index.ts       |  5 +++++
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/lib/util/http/github.spec.ts b/lib/util/http/github.spec.ts
index 8a56df0207..c3ef8ad22f 100644
--- a/lib/util/http/github.spec.ts
+++ b/lib/util/http/github.spec.ts
@@ -155,7 +155,7 @@ describe('util/http/github', () => {
         .scope(githubApiHost, {
           reqheaders: {
             authorization: 'token abc',
-            accept: 'application/vnd.github.v3+json',
+            accept: 'application/json, application/vnd.github.v3+json',
           },
         })
         .get(url)
diff --git a/lib/util/http/index.spec.ts b/lib/util/http/index.spec.ts
index 00f964975f..97226e30c1 100644
--- a/lib/util/http/index.spec.ts
+++ b/lib/util/http/index.spec.ts
@@ -70,7 +70,14 @@ describe('util/http/index', () => {
   });
 
   it('getJson', async () => {
-    httpMock.scope(baseUrl).get('/').reply(200, '{ "test": true }');
+    httpMock
+      .scope(baseUrl, {
+        reqheaders: {
+          accept: 'application/json',
+        },
+      })
+      .get('/')
+      .reply(200, '{ "test": true }');
     expect(await http.getJson('http://renovate.com')).toEqual({
       authorization: false,
       body: {
@@ -324,7 +331,11 @@ describe('util/http/index', () => {
     describe('getJson', () => {
       it('infers body type', async () => {
         httpMock
-          .scope(baseUrl)
+          .scope(baseUrl, {
+            reqheaders: {
+              accept: 'application/json',
+            },
+          })
           .get('/')
           .reply(200, JSON.stringify({ test: true }));
 
@@ -342,7 +353,11 @@ describe('util/http/index', () => {
       it('reports warnings', async () => {
         memCache.init();
         httpMock
-          .scope(baseUrl)
+          .scope(baseUrl, {
+            reqheaders: {
+              accept: 'application/json',
+            },
+          })
           .get('/')
           .reply(200, JSON.stringify({ test: 'foobar' }));
 
@@ -361,7 +376,11 @@ describe('util/http/index', () => {
 
       it('throws', async () => {
         httpMock
-          .scope(baseUrl)
+          .scope(baseUrl, {
+            reqheaders: {
+              accept: 'application/json',
+            },
+          })
           .get('/')
           .reply(200, JSON.stringify({ test: 'foobar' }));
 
diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts
index 3fe4f883e5..43dc523705 100644
--- a/lib/util/http/index.ts
+++ b/lib/util/http/index.ts
@@ -245,6 +245,11 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
       method,
       responseType: 'json',
     };
+    // signal that we expect a json response
+    opts.headers = {
+      accept: 'application/json',
+      ...opts.headers,
+    };
     if (body) {
       opts.json = body;
     }
-- 
GitLab