Skip to content

Commit

Permalink
feat(cli): use Content-Type header to detect ruleset format
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 24, 2022
1 parent f3fb425 commit ab184f4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
41 changes: 40 additions & 1 deletion packages/cli/src/services/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ describe('Linter service', () => {
.persist()
.get('/ruleset.json')
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'application/yaml',
'Content-Type': 'application/json',
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json`);
Expand All @@ -326,6 +326,45 @@ describe('Linter service', () => {
]),
);
});

it('fallbacks to Content-Type', async () => {
nock('http://foo.local')
.persist()
.get('/ruleset')
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'application/json',
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset`);
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
expect(output).toEqual(
expect.not.arrayContaining([
expect.objectContaining({
message: 'Info object should contain `contact` object',
}),
]),
);
});

it('ignores query parameters', async () => {
nock('http://foo.local')
.persist()
.get('/ruleset.json')
.query({ token: 'bar' })
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'text/plain', // GitHub raw like
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json?token=bar`);
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
expect(output).toEqual(
expect.not.arrayContaining([
expect.objectContaining({
message: 'Info object should contain `contact` object',
}),
]),
);
});
});
});

Expand Down
34 changes: 29 additions & 5 deletions packages/cli/src/services/linter/utils/getRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ async function getDefaultRulesetFile(): Promise<Optional<string>> {
return;
}

function isBasicRuleset(filepath: string): boolean {
return /\.(json|ya?ml)$/.test(path.extname(filepath));
}

function isErrorWithCode(error: Error | (Error & { code: unknown })): error is Error & { code: string } {
return 'code' in error && typeof error.code === 'string';
}
Expand All @@ -49,7 +45,7 @@ export async function getRuleset(rulesetFile: Optional<string>): Promise<Ruleset
let ruleset: string;

try {
if (isBasicRuleset(rulesetFile)) {
if (await isBasicRuleset(rulesetFile)) {
const migratedRuleset = await migrateRuleset(rulesetFile, {
format: 'esm',
fs,
Expand Down Expand Up @@ -104,3 +100,31 @@ function load(source: string, uri: string): RulesetDefinition {

return m.exports;
}

function stripSearchFromUrl(url: string): string {
try {
const { href, search } = new URL(url);
return href.slice(0, href.length - search.length);
} catch {
return url;
}
}

async function isBasicRuleset(uri: string): Promise<boolean> {
if (path.isURL(uri)) {
uri = stripSearchFromUrl(uri);
}

if (/\.(json|ya?ml)$/.test(path.extname(uri))) {
return true;
}

try {
const contentType = (await fetch(uri)).headers.get('Content-Type');
return (
contentType !== null && ['application/yaml', 'text/yaml', 'application/json', 'text/json'].includes(contentType)
);
} catch {
return false;
}
}

0 comments on commit ab184f4

Please sign in to comment.