diff --git a/src/freeDatas2HTMLRemoteSources.ts b/src/freeDatas2HTMLRemoteSource.ts similarity index 73% rename from src/freeDatas2HTMLRemoteSources.ts rename to src/freeDatas2HTMLRemoteSource.ts index d2c0160..b3f3e81 100644 --- a/src/freeDatas2HTMLRemoteSources.ts +++ b/src/freeDatas2HTMLRemoteSource.ts @@ -1,17 +1,20 @@ const errors = require("./errors.js"); -import { RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces"; +import { RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces"; -export class RemoteSources implements RemoteSource +export class RemoteSource implements RemoteSources { public allowedUrlProtocol: string[]=["https:","http:"]; - private _url: string; + private _url: string=""; private _headers: { key:string, value:string }[]=[]; private _withCredentials: boolean=false; constructor(RemoteSettings: RemoteSourceSettings) { - this._url=RemoteSettings.url; + // Le fait de ne pas utiliser le préfixe _ implique de passer par les setters + // Mais l'url n'est testée que si elle n'est pas vide. + if(RemoteSettings.url !== "") + this.url=RemoteSettings.url; if(RemoteSettings.headers !== undefined) this.headers=RemoteSettings.headers; if(RemoteSettings.withCredentials !== undefined) @@ -26,12 +29,13 @@ export class RemoteSources implements RemoteSource { try { - const checkUrl=new URL(url);// peut déjà générer une erreur si url bidon + const checkUrl=new URL(url);// peut générer une erreur si url bidon if(this.allowedUrlProtocol.indexOf(checkUrl.protocol) === -1) throw new Error(); } catch(e) { + console.error(e); throw new Error(errors.remoteSourceUrlFail); } this._url=url.trim(); @@ -45,7 +49,8 @@ export class RemoteSources implements RemoteSource set headers(headers: { key:string, value:string }[]) { - const forbiddenHeadersNames: string[]=["Accept-Charset","Accept-Encoding","Access-Control-Request-Headers","Access-Control-Request-Method","Connection","Content-Length","Cookie","Cookie2","Date","DNT","Expect","Host","Keep-Alive","Origin","Referer","TE","Trailer","Transfer-Encoding","Upgrade","Via"]; // cf. https://developer.mozilla.org/fr/docs/Glossary/Forbidden_header_name + // cf. https://developer.mozilla.org/fr/docs/Glossary/Forbidden_header_name + const forbiddenHeadersNames: string[]=["Accept-Charset","Accept-Encoding","Access-Control-Request-Headers","Access-Control-Request-Method","Connection","Content-Length","Cookie","Cookie2","Date","DNT","Expect","Host","Keep-Alive","Origin","Referer","TE","Trailer","Transfer-Encoding","Upgrade","Via"]; for(let header of headers) { header.key=header.key.trim(); @@ -79,7 +84,7 @@ export class RemoteSources implements RemoteSource for(let header of this._headers) headers.append(header.key, header.value); } - const credentials : RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit"; - return { method: "GET", headers: headers, credentials: credentials }; + const credentials: RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit"; + return { method:"GET", headers:headers, credentials:credentials }; } } \ No newline at end of file diff --git a/tests/remoteSourceSpec.ts b/tests/remoteSourceSpec.ts index f263e32..06c6c29 100644 --- a/tests/remoteSourceSpec.ts +++ b/tests/remoteSourceSpec.ts @@ -1,13 +1,13 @@ -import { RemoteSources } from "../src/freeDatas2HTMLRemoteSources"; +import { RemoteSource } from "../src/freeDatas2HTMLRemoteSource"; const errors=require("../src/errors.js"); describe("Tests des urls distantes", () => { - let source: RemoteSources; + let source: RemoteSource; beforeEach( () => { - source=new RemoteSources({ url:"http://localhost:8080/" }); + source=new RemoteSource({ url:"http://localhost:8080/" }); }); it("Doit générer une erreur si l'url fournie est une chaîne vide.", () => @@ -47,5 +47,4 @@ describe("Tests des urls distantes", () => headers.append("userName", "Toto"); expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "include" }); }); - }); \ No newline at end of file