Skip to content

Commit

Permalink
Merge pull request #158 from simplabs/exists
Browse files Browse the repository at this point in the history
Add method to check whether a cookie exists
  • Loading branch information
pangratz authored Feb 12, 2018
2 parents 73994fe + 9a6fc4b commit 9f8f0fb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ export default Ember.Controller.extend({

The `cookies` service has methods for reading and writing cookies:

* `read(name, options = {})`: reads the cookie with the given name, returns its value as a
`String`; options can be used to set `raw` (boolean, disables URL-decoding the value).
* `read(name, options = {})`: reads the cookie with the given name, returns its
value as a `String`; options can be used to set `raw` (boolean, disables
URL-decoding the value).
* `write(name, value, options = {})`: writes a cookie with the given name and
value; options can be used to set `domain`, `expires` (Date), `maxAge` (time in seconds), `path`, `secure`,
and `raw` (boolean, disables URL-encoding the value).
value; options can be used to set `domain`, `expires` (Date), `maxAge` (time
in seconds), `path`, `secure`, and `raw` (boolean, disables URL-encoding the
value).
* `clear(name, options = {})`: clears the cookie so that future reads do not
return a value; options can be used to specify `domain`, `path` or `secure`.
* `exists(name)`: checks whether a cookie exists at all (even with a falsy
value) and returns `true` if that is the case or `false` otherwise.

## License

Expand Down
15 changes: 13 additions & 2 deletions addon/services/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default Service.extend({
return filtered.reduce((acc, cookie) => {
if (!isEmpty(cookie)) {
let [key, value] = cookie;
acc[key.trim()] = value.trim();
acc[key.trim()] = (value || '').trim();
}
return acc;
}, {});
Expand Down Expand Up @@ -92,6 +92,17 @@ export default Service.extend({
this.write(name, null, options);
},

exists(name) {
let all;
if (this.get('_isFastBoot')) {
all = this.get('_fastBootCookies');
} else {
all = this.get('_documentCookies');
}

return all.hasOwnProperty(name);
},

_writeDocumentCookie(name, value, options = {}) {
let serializedCookie = this._serializeCookie(name, value, options);
this.set('_document.cookie', serializedCookie);
Expand Down Expand Up @@ -178,7 +189,7 @@ export default Service.extend({

_filterDocumentCookies(unfilteredCookies) {
return unfilteredCookies.map((c) => c.split('='))
.filter((c) => isPresent(c[0]) && isPresent(c[1]));
.filter((c) => c.length === 2 && isPresent(c[0]));
},

_serializeCookie(name, value, options = {}) {
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/services/cookies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ describe('CookiesService', function() {
expect(afterRoundtrip).to.eq(value);
});

it('handles invalid values for cookies', function() {
it('handles invalid cookies', function() {
document.cookie = '=blank';

expect(this.subject().read('')).to.deep.equal({});
});

Expand Down Expand Up @@ -390,6 +391,25 @@ describe('CookiesService', function() {
});
});

describe("checking for a cookie's existence", function() {
it('returns true when the cookie exists', function() {
let value = randomString();
document.cookie = `${COOKIE_NAME}=${value};`;

expect(this.subject().exists(COOKIE_NAME)).to.be.true;
});

it('returns true when the cookie exists with a falsy value', function() {
document.cookie = `${COOKIE_NAME}=;`;

expect(this.subject().exists(COOKIE_NAME)).to.be.true;
});

it('returns false when the cookie does not exist', function() {
expect(this.subject().exists(COOKIE_NAME)).to.be.false;
});
});

itReadsAfterWrite.apply(this);
});

Expand Down Expand Up @@ -708,6 +728,25 @@ describe('CookiesService', function() {
});
});

describe("checking for a cookie's existence", function() {
it('returns true when the cookie exists', function() {
let value = randomString();
this.subject().write(COOKIE_NAME, value);

expect(this.subject().exists(COOKIE_NAME)).to.be.true;
});

it('returns true when the cookie exists with a falsy value', function() {
this.subject().write(COOKIE_NAME);

expect(this.subject().exists(COOKIE_NAME)).to.be.true;
});

it('returns false when the cookie does not exist', function() {
expect(this.subject().exists(COOKIE_NAME)).to.be.false;
});
});

itReadsAfterWrite.apply(this);
});
});

0 comments on commit 9f8f0fb

Please sign in to comment.