Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize content-type header check for case insensitivity #255

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading