2021-10-18 11:35:30 +02:00
|
|
|
import { RemoteSource } from "../src/RemoteSource";
|
2021-10-11 16:44:20 +02:00
|
|
|
const errors=require("../src/errors.js");
|
|
|
|
|
|
|
|
describe("Tests des urls distantes", () =>
|
|
|
|
{
|
2021-10-13 17:20:59 +02:00
|
|
|
let source: RemoteSource;
|
2021-10-11 16:44:20 +02:00
|
|
|
|
|
|
|
beforeEach( () =>
|
|
|
|
{
|
2021-10-13 17:20:59 +02:00
|
|
|
source=new RemoteSource({ url:"http://localhost:8080/" });
|
2021-10-11 16:44:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Doit générer une erreur si l'url fournie est une chaîne vide.", () =>
|
|
|
|
{
|
|
|
|
expect(() => { return source.url=""; }).toThrowError(errors.remoteSourceNeedUrl);
|
|
|
|
expect(() => { return source.url=" "; }).toThrowError(errors.remoteSourceNeedUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Doit générer une erreur si l'url fournie n'utilise pas un des protocoles autorisés.", () =>
|
|
|
|
{
|
|
|
|
expect(() => { return source.url="htp://localhost:8080/"; }).toThrowError(errors.remoteSourceUrlFail);
|
|
|
|
expect(() => { return source.url="ftp://localhost:8080/"; }).toThrowError(errors.remoteSourceUrlFail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Doit accepter une url valide", () =>
|
|
|
|
{
|
|
|
|
expect(() => { return source.url="http://localhost:8080/"; }).not.toThrowError();
|
|
|
|
source.allowedUrlProtocol.push("ftp:");
|
|
|
|
expect(() => { return source.url="ftp://localhost:8080/"; }).not.toThrowError();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Seuls les headers conformes doivent être retenus", () =>
|
|
|
|
{
|
|
|
|
source.headers=[ { key:"token", value:"1234" }, { key:"vide", value:"" }, { key:"Sec-id", value:"666" }, { key:"Proxy-name", value:"myProxy" }, { key:"Content-Length", value:"255" }, { key:"userName", value:"Toto" }];
|
|
|
|
expect(source.headers).toEqual([ { key:"token", value:"1234" }, { key:"vide", value:"" }, { key:"userName", value:"Toto" }]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Doit retourner la configuration correcte pour fetch.", () =>
|
|
|
|
{
|
|
|
|
let headers=new Headers();
|
|
|
|
expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "omit" });
|
|
|
|
|
|
|
|
source.headers=[ { key:"token", value:"1234" }, { key:"userName", value:"Toto" }];
|
|
|
|
source.withCredentials=true;
|
|
|
|
headers=new Headers();
|
|
|
|
headers.append("token", "1234");
|
|
|
|
headers.append("userName", "Toto");
|
|
|
|
expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "include" });
|
|
|
|
});
|
|
|
|
});
|