mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
Allow fetching plugins from URL (#1065)
This commit is contained in:
@@ -19,19 +19,8 @@ describe("robustFetch", () => {
|
||||
const url = "https://pyscript.net/non-existent-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_NOT_FOUND_ERROR,
|
||||
`Fetching from URL ${url} failed with error 404 (Not Found).`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
})
|
||||
|
||||
it('receiving a 404 when fetching should throw FetchError with the right errorCode', async () => {
|
||||
global.fetch = jest.fn(() => (Promise.resolve(new Response("Not Found", {status: 404}))));
|
||||
|
||||
const url = "https://pyscript.net/non-existent-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_NOT_FOUND_ERROR,
|
||||
`Fetching from URL ${url} failed with error 404 (Not Found).`
|
||||
`Fetching from URL ${url} failed with error 404 (Not Found). ` +
|
||||
`Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
@@ -43,7 +32,8 @@ describe("robustFetch", () => {
|
||||
const url = "https://pyscript.net/protected-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_UNAUTHORIZED_ERROR,
|
||||
`Fetching from URL ${url} failed with error 401 (Unauthorized).`
|
||||
`Fetching from URL ${url} failed with error 401 (Unauthorized). ` +
|
||||
`Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
@@ -55,7 +45,8 @@ describe("robustFetch", () => {
|
||||
const url = "https://pyscript.net/secret-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_FORBIDDEN_ERROR,
|
||||
`Fetching from URL ${url} failed with error 403 (Forbidden).`
|
||||
`Fetching from URL ${url} failed with error 403 (Forbidden). ` +
|
||||
`Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
@@ -67,7 +58,8 @@ describe("robustFetch", () => {
|
||||
const url = "https://pyscript.net/protected-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_SERVER_ERROR,
|
||||
`Fetching from URL ${url} failed with error 500 (Internal Server Error).`
|
||||
`Fetching from URL ${url} failed with error 500 (Internal Server Error). ` +
|
||||
`Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
@@ -79,9 +71,41 @@ describe("robustFetch", () => {
|
||||
const url = "https://pyscript.net/protected-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_UNAVAILABLE_ERROR,
|
||||
`Fetching from URL ${url} failed with error 503 (Service Unavailable).`
|
||||
`Fetching from URL ${url} failed with error 503 (Service Unavailable). ` +
|
||||
`Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
})
|
||||
|
||||
it('handle TypeError when using a bad url', async () => {
|
||||
global.fetch = jest.fn(() => (Promise.reject(new TypeError("Failed to fetch"))));
|
||||
|
||||
const url = "https://pyscript.net/protected-page"
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_ERROR,
|
||||
`Fetching from URL ${url} failed with error 'Failed to fetch'. Are your filename and path correct?`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
})
|
||||
|
||||
it('handle failed to fetch when using local file', async () => {
|
||||
global.fetch = jest.fn(() => (Promise.reject(new TypeError("Failed to fetch"))));
|
||||
|
||||
const url = "./my-awesome-pyscript.py"
|
||||
|
||||
const expectedError = new FetchError(
|
||||
ErrorCode.FETCH_ERROR,
|
||||
`PyScript: Access to local files
|
||||
(using "Paths:" in <py-config>)
|
||||
is not available when directly opening a HTML file;
|
||||
you must use a webserver to serve the additional files.
|
||||
See <a style="text-decoration: underline;" href="https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062">this reference</a>
|
||||
on starting a simple webserver with Python.
|
||||
`
|
||||
)
|
||||
|
||||
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user