Skip to content

Commit d6ae02d

Browse files
authored
Merge pull request #255 from tinas/fix-case-insensitive-content-type
fix: normalize content-type header check for case insensitivity
2 parents be62f34 + 4247660 commit d6ae02d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export default async function requestAndResultsFormatter(options: OpenGraphScrap
7272
body = decode(Buffer.from(bodyArrayBuffer), charset);
7373
}
7474

75-
if (response?.headers?.get('content-type') && !response.headers.get('content-type')?.includes('text/')) {
75+
const contentType = response?.headers?.get('content-type')?.toLowerCase();
76+
if (contentType && !contentType.includes('text/')) {
7677
throw new Error('Page must return a header content-type with text/');
7778
}
7879
if (response?.status && (response.status.toString().startsWith('4') || response.status.toString().startsWith('5'))) {

tests/unit/openGraphScraper.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,22 @@ describe('return ogs', function () {
709709
});
710710
});
711711

712+
it('should handle case-insensitive Content-Type headers', function () {
713+
mockAgent.get('http://www.test.com')
714+
.intercept({ path: '/' })
715+
.reply(200, basicHTML, { headers: { 'Content-Type': 'Text/html; charset=utf-8' } });
716+
717+
return ogs({ url: 'http://www.test.com' })
718+
.then(function (data) {
719+
expect(data.result.success).to.be.eql(true);
720+
expect(data.error).to.be.eql(false);
721+
expect(data.result.ogTitle).to.be.eql('test page');
722+
expect(data.result.requestUrl).to.be.eql('http://www.test.com');
723+
expect(data.html).to.be.eql(basicHTML);
724+
expect(data.response).to.be.a('response');
725+
});
726+
});
727+
712728
it('when trying to hit a non html pages based on content-type', function () {
713729
mockAgent.get('http://www.test.com')
714730
.intercept({ path: '/' })

0 commit comments

Comments
 (0)