All files / src/sso/client ClientCookie.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 5/5
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  1x       1x   1x 6x     15x 88x 87x     1x 1x 1x 1x   15x       15x 3x   15x 12x   3x      
import { RequestInit, Response } from 'node-fetch';
import dbg from 'debug';
 
import { CookieList } from '../interfaces';
 
const debug = dbg('node-expose-sspi:client');
 
export class ClientCookie {
  private cookieList: CookieList = {};
 
  saveCookies(response: Response): void {
    response.headers.forEach((value, name) => {
      if (name !== 'Set-Cookie'.toLowerCase()) {
        return;
      }
      // parse something like <key>=<val>[; Expires=xxxxx;]
      const [key, val] = value.split(/[=;]/g);
      debug('val: ', val);
      debug('key: ', key);
      this.cookieList[key] = val;
    });
    debug('cookieList: ', this.cookieList);
  }
 
  restituteCookies(requestInit: RequestInit): void {
    const cookieStr = Object.keys(this.cookieList)
      .map((key) => key + '=' + this.cookieList[key])
      .join('; ');
    if (cookieStr.length === 0) {
      return;
    }
    requestInit.headers = { ...requestInit.headers, cookie: cookieStr };
  }
}