UI protocol: move Insomnia requests collection to its own directory
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIHttpServer.ts
index 280f1d38040d71ab7151d1c33fa69b93f0977189..ce52ed4b5dd432a9c9035bbdc0eac01c7a422e4f 100644 (file)
@@ -49,10 +49,11 @@ export default class UIHttpServer extends AbstractUIServer {
 
   public sendResponse(response: ProtocolResponse): void {
     const [uuid, payload] = response;
-    const statusCode = this.responseStatusToStatusCode(payload.status);
     if (this.responseHandlers.has(uuid) === true) {
       const { res } = this.responseHandlers.get(uuid);
-      res.writeHead(statusCode, { 'Content-Type': 'application/json' });
+      res.writeHead(this.responseStatusToStatusCode(payload.status), {
+        'Content-Type': 'application/json',
+      });
       res.write(JSON.stringify(payload));
       res.end();
       this.responseHandlers.delete(uuid);
@@ -71,7 +72,7 @@ export default class UIHttpServer extends AbstractUIServer {
   }
 
   private requestListener(req: IncomingMessage, res: ServerResponse): void {
-    if (this.isBasicAuthEnabled() === true && this.isValidBasicAuth(req) === false) {
+    if (this.authenticate(req) === false) {
       res.setHeader('Content-Type', 'text/plain');
       res.setHeader('WWW-Authenticate', 'Basic realm=users');
       res.writeHead(StatusCodes.UNAUTHORIZED);
@@ -128,6 +129,16 @@ export default class UIHttpServer extends AbstractUIServer {
     }
   }
 
+  private authenticate(req: IncomingMessage): boolean {
+    if (this.isBasicAuthEnabled() === true) {
+      if (this.isValidBasicAuth(req) === true) {
+        return true;
+      }
+      return false;
+    }
+    return true;
+  }
+
   private responseStatusToStatusCode(status: ResponseStatus): StatusCodes {
     switch (status) {
       case ResponseStatus.SUCCESS: