Skip to content
Aleksey Tsvetkov edited this page Jan 11, 2016 · 6 revisions

What is it

This plugin provides chrome cookies behavior emulation. By default, chrome.cookies.* methods are sinon stubs, but after plugin installing all chrome.cookies.* methods called on fake cookie storage, which keep current cookie state.

How it works

Before all we should register plugin in chrome object

var chrome = require('sinon-chrome');
var CookiePlugin = chrome.plugins.CookiePlugin;

chrome.registerPlugin(new CookiePlugin());

Let's setup initial state for our cookie storage

chrome.cookies.state = [
{
    "domain": ".domain.com",
    "expirationDate": 1511612273,
    "hostOnly": false,
    "httpOnly": false,
    "name": "COOKIE_NAME",
    "path": "/data",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "COOKIE_VALUE"
  },
{
    "domain": "other-domain.com",
    "hostOnly": false,
    "httpOnly": false,
    "name": "other-cookie",
    "path": "/",
    "secure": false,
    "session": true,
    "storeId": "0",
    "value": "123"
  }
];

Read cookies

chrome.cookies.getAll({domain: '.domain.com'}, function (list) {
   console.log(list.length); // 1
   console.log(list[0].name); // "COOKIE_NAME"
});

chrome.cookies.getAll({}, function (list) {
   console.log(list.length); // 2
   console.log(list[0].name); // "COOKIE_NAME"
   console.log(list[1].name); // "other-cookie"
});

chrome.cookies.getAll({domain: 'google.com'}, function (list) {
   console.log(list.length); // 0
});

Get cookie via chrome.cookies.get method

var params = {
   url: 'http:/google.com',
   name: 'cookie-name'
};
chrome.cookies.get(params, function (cookie) {
   console.log(cookie); // null
});
var params = {
   url: 'http://.domain.com',
   name: 'COOKIE_NAME'
};
chrome.cookies.get(params, function (cookie) {
   console.log(cookie); // chrome.cookies.Cookie object
});

Write cookies

Append new cookie

var params = {
    url: 'http://google.com',
    name: 'custom_cookie',
    value: '123'
};

chrome.cookies.set(params, function (cookie) {
    console.log(cookie.name); // 'custom_cookie'
});

after it, fake cookie storage has state

chrome.cookies.state = [
    {
        "domain": ".domain.com",
        "expirationDate": 1511612273,
        "hostOnly": false,
        "httpOnly": false,
        "name": "COOKIE_NAME",
        "path": "/data",
        "secure": false,
        "session": false,
        "storeId": "0",
        "value": "COOKIE_VALUE"
    },
    {
        "domain": "other-domain.com",
        "hostOnly": false,
        "httpOnly": false,
        "name": "other-cookie",
        "path": "/",
        "secure": false,
        "session": true,
        "storeId": "0",
        "value": "123"
    },
    {
        name: 'custom_cookie',
        value: '123',
        domain: 'google.com',
        hostOnly: true,
        httpOnly: false,
        secure: false,
        session: true,
        path: '/'
    }
];

chrome.cookie.onChanged will be triggered after cookie fake storage update.

var params = {
    url: 'http://mega-site.com',
    name: 'cookie-name',
    value: 'cookie-value'
};
chrome.cookies.onChanged.addListener(function (changeInfo) {
    console.log(changeInfo);
});
chrome.cookies.set(params, function (cookie) {
    console.log(cookie);
});

Output will be

{cause: 'explicit', removed: false, cookie: [object Object]}
{name: 'cookie-name', value: 'cookie-value', domain: 'mega-site.com', hostOnly: true, httpOnly: false, secure: false, session: true, path: '/'}

You can replace existing cookie

var params = {
    url: 'http://mega-site.com',
    name: 'cookie-name',
    value: 'other-cookie-value'
};
chrome.cookies.onChanged.addListener(function (changeInfo) {
    console.log(changeInfo);
});
chrome.cookies.set(params, function (cookie) {
    console.log(cookie);
});

Output will be

{cause: 'overwrite', removed: true, cookie: [object Object]}
{cause: 'explicit', removed: false, cookie: [object Object]}
{name: 'cookie-name', value: 'other-cookie-value', domain: 'mega-site.com', hostOnly: true, httpOnly: false, secure: false, session: true, path: '/'}

Remove cookie

var params = {
    url: 'http://mega-site.com',
    name: 'cookie-name'
};

chrome.cookies.onChanged.addListener(function (changeInfo) {
    console.log(changeInfo); 
});

chrome.cookies.remove(params, function (removeInfo) {
    console.log(removeInfo);
});

Output will be

{cookie: [object Object], removed: true, cause: 'explicit'}
{url: 'http://mega-site.com', name: 'cookie-name'}
Clone this wiki locally