import Test from '../Test';

class Cookies extends Test {
  protected request: RequestInterface;

  protected logger: LoggerInterface;

  constructor(request: RequestInterface, logger: LoggerInterface) {
    super();
    this.request = request;
    this.logger = logger;
  }

  public async run(url: string): Promise<ResultInterface> {
    this.logger.info('Starting Cookies test...');
    const result = await this.request.get(url);
    let subChecks = null;

    if (result.response.headers.hasOwnProperty('set-cookie')) {
      const cookies = result.response.headers['set-cookie'];
      subChecks = this.checkCookies(cookies);
    }

    return this.getResult('Cookies', this.getStatus(subChecks), subChecks);
  }

  private checkCookies(cookies: string[]): ResultInterface[] {
    const regx = new RegExp('.*(secure; HttpOnly)$', 'i');

    return cookies.map((cookie) => {
      if (!regx.test(cookie)) {
        return this.getResult(cookie.substr(0, cookie.indexOf('=')), 'UNSUCCESSFUL');
      }
      return this.getResult(cookie.substr(0, cookie.indexOf('=')), 'SUCCESSFUL');
    });
  }
}

export default Cookies;

import Test from '../Test';

class Cookies extends Test {
  protected request: RequestInterface;

  protected logger: LoggerInterface;

  constructor(request: RequestInterface, logger: LoggerInterface) {
    super();
    this.request = request;
    this.logger = logger;
  }

  public async run(url: string): Promise<ResultInterface> {
    this.logger.info('Starting Cookies test...');
    const result = await this.request.get(url);
    let subChecks = [];

    if (result.response.headers.hasOwnProperty('set-cookie')) {
      const cookies = result.response.headers['set-cookie'];
      subChecks = this.checkCookies(cookies);
    }

    return this.getResult('Cookies', this.getStatus(subChecks), subChecks);
  }

  private checkCookies(cookies: string[]): ResultInterface[] {
    const regx = new RegExp('.*(secure; HttpOnly)$', 'i');

    return cookies.map((cookie) => {
      if (!regx.test(cookie)) {
        return this.getResult(cookie.substr(0, cookie.indexOf('=')), 'UNSUCCESSFUL');
      }
      return this.getResult(cookie.substr(0, cookie.indexOf('=')), 'SUCCESSFUL');
    });
  }
}

export default Cookies;

Fix issue when page doesn't have any cookies
Fix issue when page doesn't have any cookies
