Changement de nom + relecture classe RemoteSource.

This commit is contained in:
Fabrice PENHOËT 2021-10-13 17:20:59 +02:00
parent b2d5f75065
commit 879ef17fe2
2 changed files with 16 additions and 12 deletions

View File

@ -1,17 +1,20 @@
const errors = require("./errors.js"); 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:"]; public allowedUrlProtocol: string[]=["https:","http:"];
private _url: string; private _url: string="";
private _headers: { key:string, value:string }[]=[]; private _headers: { key:string, value:string }[]=[];
private _withCredentials: boolean=false; private _withCredentials: boolean=false;
constructor(RemoteSettings: RemoteSourceSettings) 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) if(RemoteSettings.headers !== undefined)
this.headers=RemoteSettings.headers; this.headers=RemoteSettings.headers;
if(RemoteSettings.withCredentials !== undefined) if(RemoteSettings.withCredentials !== undefined)
@ -26,12 +29,13 @@ export class RemoteSources implements RemoteSource
{ {
try 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) if(this.allowedUrlProtocol.indexOf(checkUrl.protocol) === -1)
throw new Error(); throw new Error();
} }
catch(e) catch(e)
{ {
console.error(e);
throw new Error(errors.remoteSourceUrlFail); throw new Error(errors.remoteSourceUrlFail);
} }
this._url=url.trim(); this._url=url.trim();
@ -45,7 +49,8 @@ export class RemoteSources implements RemoteSource
set headers(headers: { key:string, value:string }[]) 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) for(let header of headers)
{ {
header.key=header.key.trim(); header.key=header.key.trim();
@ -79,7 +84,7 @@ export class RemoteSources implements RemoteSource
for(let header of this._headers) for(let header of this._headers)
headers.append(header.key, header.value); headers.append(header.key, header.value);
} }
const credentials : RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit"; const credentials: RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit";
return { method: "GET", headers: headers, credentials: credentials }; return { method:"GET", headers:headers, credentials:credentials };
} }
} }

View File

@ -1,13 +1,13 @@
import { RemoteSources } from "../src/freeDatas2HTMLRemoteSources"; import { RemoteSource } from "../src/freeDatas2HTMLRemoteSource";
const errors=require("../src/errors.js"); const errors=require("../src/errors.js");
describe("Tests des urls distantes", () => describe("Tests des urls distantes", () =>
{ {
let source: RemoteSources; let source: RemoteSource;
beforeEach( () => 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.", () => 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"); headers.append("userName", "Toto");
expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "include" }); expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "include" });
}); });
}); });