"use strict";

var _cookiejar = require("cookiejar");

var _scrapingConnector = require("./scraping-connector");

describe("Cookie utilities", function () {
  var byName = function byName(a, b) {
    return a.name > b.name ? 1 : -1;
  };

  var toObj = function toObj(cookie) {
    return Object.assign({}, cookie);
  };

  test('cloneCookieJar should clone a cookie jar', function () {
    var j1 = new _cookiejar.CookieJar();
    j1.setCookie('PHPSESSID=12345; path=/; domain=www.merlinspotions.com; HttpOnly');
    j1.setCookie('sandy-client-id=67890');
    j1.setCookie('_ga=121212');

    var j2 = _scrapingConnector.ScrapingConnector.cloneCookieJar(j1);

    var c1 = j1.getCookies(_cookiejar.CookieAccessInfo.All).map(toObj).sort(byName);
    var c2 = j2.getCookies(_cookiejar.CookieAccessInfo.All).map(toObj).sort(byName);
    expect(c1.length).toEqual(3);
    expect(c1).toEqual(c2);
  });
  test('diffCookieJar should return only new/changed cookies', function () {
    var j1 = new _cookiejar.CookieJar();
    j1.setCookie('PHPSESSID=12345; path=/; domain=www.merlinspotions.com; HttpOnly');
    j1.setCookie('cookie-will-change=foo');

    var j2 = _scrapingConnector.ScrapingConnector.cloneCookieJar(j1);

    j2.setCookie('cookie-will-change=foo-changed');
    j2.setCookie('brand-new-cookie=new; domain=www.merlinspotions.com; path=/; secure');

    var diff = _scrapingConnector.ScrapingConnector.diffCookieJars(j1, j2).map(toObj).sort(byName);

    expect(diff).toEqual([{
      domain: 'www.merlinspotions.com',
      expiration_date: Infinity,
      explicit_domain: true,
      explicit_path: true,
      name: 'brand-new-cookie',
      noscript: false,
      path: '/',
      secure: true,
      value: 'new'
    }, {
      domain: undefined,
      expiration_date: Infinity,
      explicit_domain: false,
      explicit_path: false,
      name: 'cookie-will-change',
      noscript: false,
      path: '/',
      secure: false,
      value: 'foo-changed'
    }]);
  });
  test('rewriteCookieDomain should conditionally rewrite proxied cookies', function () {
    var jar = new _cookiejar.CookieJar();
    jar.setCookie('a=a; domain=www.merlinspotions.com; HttpOnly; secure');
    jar.setCookie('b=b; domain=something-else.com');
    jar.setCookie('c=c');
    var cookies = jar.getCookies(_cookiejar.CookieAccessInfo.All);
    var rewritten = cookies.map(function (cookie) {
      return _scrapingConnector.ScrapingConnector.rewriteCookieDomain({
        'www.merlinspotions.com': 'www.merlinsproxy.com'
      }, cookie);
    }).map(toObj).sort(byName);
    var expected = [{
      domain: 'www.merlinsproxy.com',
      expiration_date: Infinity,
      explicit_domain: true,
      explicit_path: false,
      name: 'a',
      noscript: true,
      path: '/',
      secure: true,
      value: 'a'
    }, {
      domain: 'something-else.com',
      expiration_date: Infinity,
      explicit_domain: true,
      explicit_path: false,
      name: 'b',
      noscript: false,
      path: '/',
      secure: false,
      value: 'b'
    }, {
      domain: undefined,
      expiration_date: Infinity,
      explicit_domain: false,
      explicit_path: false,
      name: 'c',
      noscript: false,
      path: '/',
      secure: false,
      value: 'c'
    }];
    expect(rewritten).toEqual(expected);
  });
});