fix(OAS3): relative urls (#5341)

* Added tooling for appending OAS3 relative URLs to selected Server

Info
* Terms of service URL
* Contact URL
* License URL
* External Docs URL

Tag
* Tag External Docs URL

Operation
* Operation External Docs
** Operation Tag


Co-authored-by: Tim Lai <timothy.lai@gmail.com>
This commit is contained in:
geraldglynn
2020-08-04 21:21:19 +01:00
committed by GitHub
parent 225a915cf8
commit d9f5691f65
11 changed files with 174 additions and 31 deletions

View File

@@ -17,7 +17,10 @@ describe("<InfoContainer/>", function () {
url () {},
basePath () {},
host () {},
externalDocs () {}
externalDocs () {},
},
oas3Selectors: {
selectedServer () {},
},
getComponent: c => components[c]
}

View File

@@ -29,6 +29,7 @@ describe("<Operations/>", function(){
},
specSelectors: {
isOAS3() { return false },
url() { return "https://petstore.swagger.io/v2/swagger.json" },
taggedOperations() {
return fromJS({
"default": {
@@ -83,6 +84,7 @@ describe("<Operations/>", function(){
},
specSelectors: {
isOAS3() { return true },
url() { return "https://petstore.swagger.io/v2/swagger.json" },
taggedOperations() {
return fromJS({
"default": {

View File

@@ -32,6 +32,13 @@ import {
generateCodeVerifier,
createCodeChallenge,
} from "core/utils"
import {
isAbsoluteUrl,
buildBaseUrl,
buildUrl,
} from "core/utils/url"
import win from "core/window"
describe("utils", function() {
@@ -1334,6 +1341,92 @@ describe("utils", function() {
})
})
describe("isAbsoluteUrl", function() {
it("check if url is absolute", function() {
expect(!!isAbsoluteUrl("http://example.com")).toEqual(true)
expect(!!isAbsoluteUrl("https://secure-example.com")).toEqual(true)
expect(!!isAbsoluteUrl("HTTP://uppercase-example.com")).toEqual(true)
expect(!!isAbsoluteUrl("HTTP://uppercase-secure-example.com")).toEqual(true)
expect(!!isAbsoluteUrl("http://trailing-slash.com/")).toEqual(true)
expect(!!isAbsoluteUrl("ftp://file-transfer-protocol.com")).toEqual(true)
expect(!!isAbsoluteUrl("//no-protocol.com")).toEqual(true)
})
it("check if url is not absolute", function() {
expect(!!isAbsoluteUrl("/url-relative-to-host/base-path/path")).toEqual(false)
expect(!!isAbsoluteUrl("url-relative-to-base/base-path/path")).toEqual(false)
})
})
describe("buildBaseUrl", function() {
const specUrl = "https://petstore.swagger.io/v2/swagger.json"
const noServerSelected = ""
const absoluteServerUrl = "https://server-example.com/base-path/path"
const serverUrlRelativeToBase = "server-example/base-path/path"
const serverUrlRelativeToHost = "/server-example/base-path/path"
it("build base url with no server selected", function() {
expect(buildBaseUrl(noServerSelected, specUrl)).toBe("https://petstore.swagger.io/v2/swagger.json")
})
it("build base url from absolute server url", function() {
expect(buildBaseUrl(absoluteServerUrl, specUrl)).toBe("https://server-example.com/base-path/path")
})
it("build base url from relative server url", function() {
expect(buildBaseUrl(serverUrlRelativeToBase, specUrl)).toBe("https://petstore.swagger.io/v2/server-example/base-path/path")
expect(buildBaseUrl(serverUrlRelativeToHost, specUrl)).toBe("https://petstore.swagger.io/server-example/base-path/path")
})
})
describe("buildUrl", function() {
const specUrl = "https://petstore.swagger.io/v2/swagger.json"
const noUrl = ""
const absoluteUrl = "https://example.com/base-path/path"
const urlRelativeToBase = "relative-url/base-path/path"
const urlRelativeToHost = "/relative-url/base-path/path"
const noServerSelected = ""
const absoluteServerUrl = "https://server-example.com/base-path/path"
const serverUrlRelativeToBase = "server-example/base-path/path"
const serverUrlRelativeToHost = "/server-example/base-path/path"
it("build no url", function() {
expect(buildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined)
expect(buildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe(undefined)
expect(buildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe(undefined)
})
it("build absolute url", function() {
expect(buildUrl(absoluteUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://example.com/base-path/path")
expect(buildUrl(absoluteUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://example.com/base-path/path")
expect(buildUrl(absoluteUrl, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://example.com/base-path/path")
})
it("build relative url with no server selected", function() {
expect(buildUrl(urlRelativeToBase, specUrl, { selectedServer: noServerSelected })).toBe("https://petstore.swagger.io/v2/relative-url/base-path/path")
expect(buildUrl(urlRelativeToHost, specUrl, { selectedServer: noServerSelected })).toBe("https://petstore.swagger.io/relative-url/base-path/path")
})
it("build relative url with absolute server url", function() {
expect(buildUrl(urlRelativeToBase, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://server-example.com/base-path/relative-url/base-path/path")
expect(buildUrl(urlRelativeToHost, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://server-example.com/relative-url/base-path/path")
})
it("build relative url with server url relative to base", function() {
expect(buildUrl(urlRelativeToBase, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://petstore.swagger.io/v2/server-example/base-path/relative-url/base-path/path")
expect(buildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://petstore.swagger.io/relative-url/base-path/path")
})
it("build relative url with server url relative to host", function() {
expect(buildUrl(urlRelativeToBase, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/server-example/base-path/relative-url/base-path/path")
expect(buildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/relative-url/base-path/path")
})
})
describe("requiresValidationURL", function() {
it("Should tell us if we require a ValidationURL", function() {
const res = requiresValidationURL("https://example.com")

View File

@@ -18,7 +18,8 @@ describe("<Info/> Sanitization", function(){
description: "Description *with* <script>Markdown</script>"
}),
host: "example.test",
basePath: "/api"
basePath: "/api",
selectedServer: "https://example.test",
}
it("renders sanitized .title content", function(){