From 424766058bf5b7c536bd5f21deb66c5bcba588ca Mon Sep 17 00:00:00 2001 From: tinas Date: Wed, 18 Dec 2024 22:22:44 +0300 Subject: [PATCH] fix: normalize content-type header check for case insensitivity --- lib/request.ts | 3 ++- tests/unit/openGraphScraper.spec.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/request.ts b/lib/request.ts index a7a0d46..76031d4 100644 --- a/lib/request.ts +++ b/lib/request.ts @@ -72,7 +72,8 @@ export default async function requestAndResultsFormatter(options: OpenGraphScrap body = decode(Buffer.from(bodyArrayBuffer), charset); } - if (response?.headers?.get('content-type') && !response.headers.get('content-type')?.includes('text/')) { + const contentType = response?.headers?.get('content-type')?.toLowerCase(); + if (contentType && !contentType.includes('text/')) { throw new Error('Page must return a header content-type with text/'); } if (response?.status && (response.status.toString().startsWith('4') || response.status.toString().startsWith('5'))) { diff --git a/tests/unit/openGraphScraper.spec.ts b/tests/unit/openGraphScraper.spec.ts index 98101ed..84aae5e 100644 --- a/tests/unit/openGraphScraper.spec.ts +++ b/tests/unit/openGraphScraper.spec.ts @@ -709,6 +709,22 @@ describe('return ogs', function () { }); }); + it('should handle case-insensitive Content-Type headers', function () { + mockAgent.get('http://www.test.com') + .intercept({ path: '/' }) + .reply(200, basicHTML, { headers: { 'Content-Type': 'Text/html; charset=utf-8' } }); + + return ogs({ url: 'http://www.test.com' }) + .then(function (data) { + expect(data.result.success).to.be.eql(true); + expect(data.error).to.be.eql(false); + expect(data.result.ogTitle).to.be.eql('test page'); + expect(data.result.requestUrl).to.be.eql('http://www.test.com'); + expect(data.html).to.be.eql(basicHTML); + expect(data.response).to.be.a('response'); + }); + }); + it('when trying to hit a non html pages based on content-type', function () { mockAgent.get('http://www.test.com') .intercept({ path: '/' })