-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(handler): added handler for must contain domain and context and …
…it tests
- Loading branch information
1 parent
aca3f6e
commit 15b8c6e
Showing
8 changed files
with
349 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com/domain/context' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
application/json; charset=utf-8: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
60 changes: 60 additions & 0 deletions
60
src/__tests__/data/with-domain-context-and-version/swagger.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com/domain/context/v1' | ||
- url: 'https://api.geodatasource.com/v1/domain/context' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
application/json; charset=utf-8: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
application/json; charset=utf-8: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
59 changes: 59 additions & 0 deletions
59
src/__tests__/data/without-domain-and-with-context/swagger.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com/context' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
application/json; charset=utf-8: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
62 changes: 62 additions & 0 deletions
62
src/__tests__/rules/handlers/must-contain-domain-and-context.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import path from 'path'; | ||
import { parse } from '../../../index'; | ||
import { mustContainDomainAndContext } from '../../../rules/handlers'; | ||
import { RuleFault } from '../../../rules/rule-fault'; | ||
|
||
describe('mustContainDomainAndContext function', () => { | ||
const apiFileWithoutServer = path.join(__dirname, '..', '..', 'data', 'without-server-url', 'swagger.yml'); | ||
const apiFileWithDomainAndcontext = path.join(__dirname, '..', '..', 'data', 'with-domain-and-context', 'swagger.yml'); | ||
const apiFileWithDomaincontextAndVersion = path.join(__dirname, '..', '..', 'data', 'with-domain-context-and-version', 'swagger.yml'); | ||
const apiFileWithoutDomainAndContext = path.join(__dirname, '..', '..', 'data', 'without-domain-and-context', 'swagger.yml'); | ||
const apiFileWithoutDomainAndWithContext = path.join(__dirname, '..', '..', 'data', 'without-domain-and-with-context', 'swagger.yml'); | ||
|
||
it('should return when no server is present', async () => { | ||
const api = await parse(apiFileWithoutServer); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
mustContainDomainAndContext(api, faults); | ||
|
||
expect(faults.length).toBe(0); | ||
}); | ||
|
||
it('should have no faults when domain and context are present', async () => { | ||
const api = await parse(apiFileWithDomainAndcontext); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
mustContainDomainAndContext(api, faults); | ||
|
||
expect(faults.length).toBe(0); | ||
}); | ||
|
||
it('should have no faults when domain, context and version are present', async () => { | ||
const api = await parse(apiFileWithDomaincontextAndVersion); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
mustContainDomainAndContext(api, faults); | ||
|
||
expect(faults.length).toBe(0); | ||
}); | ||
|
||
it('should have one fault when domain and context are not present', async () => { | ||
const api = await parse(apiFileWithoutDomainAndContext); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
mustContainDomainAndContext(api, faults); | ||
|
||
expect(faults.length).toBe(1); | ||
}); | ||
|
||
it('should have one fault when domain is not present and context is', async () => { | ||
const api = await parse(apiFileWithoutDomainAndWithContext); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
mustContainDomainAndContext(api, faults); | ||
|
||
expect(faults.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { OpenAPI } from "openapi-types"; | ||
import Url from "url-parse"; | ||
import { RuleFault, Severity, RuleFaultContent } from "../rule-fault"; | ||
import { produceRuleFaultValueForUrl, pushFault } from './util'; | ||
|
||
const faults = { | ||
mustContainDomainAndContext: 'Missing domain and/or context on url property' | ||
}; | ||
|
||
const produceMustContainDomainAndContext = (urlString: string): RuleFault => { | ||
return { | ||
value: produceRuleFaultValueForUrl(urlString), | ||
errors: [ | ||
{ | ||
severity: Severity.warning, | ||
message: faults.mustContainDomainAndContext | ||
} as RuleFaultContent | ||
] | ||
}; | ||
}; | ||
|
||
const isMissingDomainAndContext = (urlString: string): boolean => { | ||
const url = new Url(urlString); | ||
|
||
/** | ||
* Matches /domain/context but excludes the version on the test which matches `/v\w+/` | ||
*/ | ||
const matcher = /\/(?!(v\d+)\b)\w+\/(?!(v\d+)\b)\w+/i; | ||
return !matcher.test(url.pathname); | ||
}; | ||
|
||
export const mustContainDomainAndContext = (api: OpenAPI.Document, ruleFaults: RuleFault[]) => { | ||
const apiParsed: any = api; | ||
|
||
/** | ||
* Without any url, there is no way to evaluate port | ||
*/ | ||
if (!apiParsed.servers) { | ||
return; | ||
} | ||
|
||
apiParsed.servers.forEach((server: any) => { | ||
if (isMissingDomainAndContext(server.url)) { | ||
pushFault(produceMustContainDomainAndContext(server.url), ruleFaults); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters