Skip to content

Commit

Permalink
Merge pull request #255 from tinas/fix-case-insensitive-content-type
Browse files Browse the repository at this point in the history
fix: normalize content-type header check for case insensitivity
  • Loading branch information
jshemas authored Jan 14, 2025
2 parents be62f34 + 4247660 commit d6ae02d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) {
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/openGraphScraper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/' })
Expand Down

0 comments on commit d6ae02d

Please sign in to comment.