fix(buildUrl): relative url is invalid URL (OAS3) or non-url (OAS2) (#6864)

* OAS3: relative url when no servers defined AND specUrl is invalid Url

* OAS2: specUrl is non-url string
This commit is contained in:
Tim Lai
2021-01-22 16:55:37 -08:00
committed by GitHub
parent cdfb64f711
commit a5eb3dc0c3
2 changed files with 34 additions and 7 deletions

View File

@@ -39,6 +39,7 @@ import {
} from "core/utils/url"
import win from "core/window"
import { afterAll, beforeAll, expect, jest } from "@jest/globals"
describe("utils", () => {
@@ -1389,6 +1390,17 @@ describe("utils", () => {
})
describe("buildUrl", () => {
const { location } = window
beforeAll(() => {
delete window.location
window.location = {
href: "http://localhost/",
}
})
afterAll(() => {
window.location = location
})
const specUrl = "https://petstore.swagger.io/v2/swagger.json"
const noUrl = ""
@@ -1401,6 +1413,9 @@ describe("utils", () => {
const serverUrlRelativeToBase = "server-example/base-path/path"
const serverUrlRelativeToHost = "/server-example/base-path/path"
const specUrlAsInvalidUrl = "./examples/test.yaml"
const specUrlOas2NonUrlString = "an allowed OAS2 TermsOfService description string"
it("build no url", () => {
expect(buildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined)
expect(buildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe(undefined)
@@ -1432,6 +1447,14 @@ describe("utils", () => {
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")
})
it("build relative url when no servers defined AND specUrl is invalid Url", () => {
expect(buildUrl(urlRelativeToHost, specUrlAsInvalidUrl, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path")
})
it("build relative url when no servers defined AND specUrl is OAS2 non-url string", () => {
expect(buildUrl(urlRelativeToHost, specUrlOas2NonUrlString, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path")
})
})
describe("requiresValidationURL", () => {