|
1 | | - |
2 | 1 | import { Map, fromJS } from "immutable" |
3 | 2 | import { |
4 | 3 | mapToList, |
@@ -36,6 +35,7 @@ import { |
36 | 35 | isAbsoluteUrl, |
37 | 36 | buildBaseUrl, |
38 | 37 | buildUrl, |
| 38 | + safeBuildUrl, |
39 | 39 | } from "core/utils/url" |
40 | 40 |
|
41 | 41 | import win from "core/window" |
@@ -1445,6 +1445,7 @@ describe("utils", () => { |
1445 | 1445 | const absoluteServerUrl = "https://server-example.com/base-path/path" |
1446 | 1446 | const serverUrlRelativeToBase = "server-example/base-path/path" |
1447 | 1447 | const serverUrlRelativeToHost = "/server-example/base-path/path" |
| 1448 | + const serverUrlWithVariables = "https://api.example.com:{port}/{basePath}" |
1448 | 1449 |
|
1449 | 1450 | const specUrlAsInvalidUrl = "./examples/test.yaml" |
1450 | 1451 | const specUrlOas2NonUrlString = "an allowed OAS2 TermsOfService description string" |
@@ -1488,6 +1489,85 @@ describe("utils", () => { |
1488 | 1489 | it("build relative url when no servers defined AND specUrl is OAS2 non-url string", () => { |
1489 | 1490 | expect(buildUrl(urlRelativeToHost, specUrlOas2NonUrlString, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path") |
1490 | 1491 | }) |
| 1492 | + |
| 1493 | + it("throws error when server url contains non-transcluded server variables", () => { |
| 1494 | + const buildUrlThunk = () => buildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlWithVariables }) |
| 1495 | + |
| 1496 | + expect(buildUrlThunk).toThrow(/^Invalid base URL/) |
| 1497 | + }) |
| 1498 | + }) |
| 1499 | + |
| 1500 | + describe("safeBuildUrl", () => { |
| 1501 | + const { location } = window |
| 1502 | + beforeAll(() => { |
| 1503 | + delete window.location |
| 1504 | + window.location = { |
| 1505 | + href: "http://localhost/", |
| 1506 | + } |
| 1507 | + }) |
| 1508 | + afterAll(() => { |
| 1509 | + window.location = location |
| 1510 | + }) |
| 1511 | + |
| 1512 | + const specUrl = "https://petstore.swagger.io/v2/swagger.json" |
| 1513 | + |
| 1514 | + const noUrl = "" |
| 1515 | + const absoluteUrl = "https://example.com/base-path/path" |
| 1516 | + const urlRelativeToBase = "relative-url/base-path/path" |
| 1517 | + const urlRelativeToHost = "/relative-url/base-path/path" |
| 1518 | + |
| 1519 | + const noServerSelected = "" |
| 1520 | + const absoluteServerUrl = "https://server-example.com/base-path/path" |
| 1521 | + const serverUrlRelativeToBase = "server-example/base-path/path" |
| 1522 | + const serverUrlRelativeToHost = "/server-example/base-path/path" |
| 1523 | + const serverUrlWithVariables = "https://api.example.com:{port}/{basePath}" |
| 1524 | + |
| 1525 | + const specUrlAsInvalidUrl = "./examples/test.yaml" |
| 1526 | + const specUrlOas2NonUrlString = "an allowed OAS2 TermsOfService description string" |
| 1527 | + |
| 1528 | + it("build no url", () => { |
| 1529 | + expect(safeBuildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined) |
| 1530 | + expect(safeBuildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe(undefined) |
| 1531 | + expect(safeBuildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe(undefined) |
| 1532 | + }) |
| 1533 | + |
| 1534 | + it("build absolute url", () => { |
| 1535 | + expect(safeBuildUrl(absoluteUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://example.com/base-path/path") |
| 1536 | + expect(safeBuildUrl(absoluteUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://example.com/base-path/path") |
| 1537 | + expect(safeBuildUrl(absoluteUrl, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://example.com/base-path/path") |
| 1538 | + }) |
| 1539 | + |
| 1540 | + it("build relative url with no server selected", () => { |
| 1541 | + expect(safeBuildUrl(urlRelativeToBase, specUrl, { selectedServer: noServerSelected })).toBe("https://petstore.swagger.io/v2/relative-url/base-path/path") |
| 1542 | + expect(safeBuildUrl(urlRelativeToHost, specUrl, { selectedServer: noServerSelected })).toBe("https://petstore.swagger.io/relative-url/base-path/path") |
| 1543 | + }) |
| 1544 | + |
| 1545 | + it("build relative url with absolute server url", () => { |
| 1546 | + expect(safeBuildUrl(urlRelativeToBase, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://server-example.com/base-path/relative-url/base-path/path") |
| 1547 | + expect(safeBuildUrl(urlRelativeToHost, specUrl, { selectedServer: absoluteServerUrl })).toBe("https://server-example.com/relative-url/base-path/path") |
| 1548 | + }) |
| 1549 | + |
| 1550 | + it("build relative url with server url relative to base", () => { |
| 1551 | + expect(safeBuildUrl(urlRelativeToBase, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://petstore.swagger.io/v2/server-example/base-path/relative-url/base-path/path") |
| 1552 | + expect(safeBuildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe("https://petstore.swagger.io/relative-url/base-path/path") |
| 1553 | + }) |
| 1554 | + |
| 1555 | + it("build relative url with server url relative to host", () => { |
| 1556 | + expect(safeBuildUrl(urlRelativeToBase, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/server-example/base-path/relative-url/base-path/path") |
| 1557 | + expect(safeBuildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/relative-url/base-path/path") |
| 1558 | + }) |
| 1559 | + |
| 1560 | + it("build relative url when no servers defined AND specUrl is invalid Url", () => { |
| 1561 | + expect(safeBuildUrl(urlRelativeToHost, specUrlAsInvalidUrl, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path") |
| 1562 | + }) |
| 1563 | + |
| 1564 | + it("build relative url when no servers defined AND specUrl is OAS2 non-url string", () => { |
| 1565 | + expect(safeBuildUrl(urlRelativeToHost, specUrlOas2NonUrlString, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path") |
| 1566 | + }) |
| 1567 | + |
| 1568 | + it("build no url when server url contains non-transcluded server variables", () => { |
| 1569 | + expect(safeBuildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlWithVariables })).toBe(undefined) |
| 1570 | + }) |
1491 | 1571 | }) |
1492 | 1572 |
|
1493 | 1573 | describe("requiresValidationURL", () => { |
|
0 commit comments