From 93b4281c3db6a13308b098b7ab151c0995e92356 Mon Sep 17 00:00:00 2001 From: Kurman Karabukaev Date: Sun, 1 Dec 2013 05:03:49 -0800 Subject: [PATCH 1/4] Basic Cookies service/wrapper over BrowserCookies --- lib/core_dom/cookies.dart | 19 +++++++- lib/core_dom/module.dart | 1 + test/core_dom/cookies_spec.dart | 82 ++++++++++++++++++++++----------- 3 files changed, 75 insertions(+), 27 deletions(-) diff --git a/lib/core_dom/cookies.dart b/lib/core_dom/cookies.dart index 7346d9050..020b4888e 100644 --- a/lib/core_dom/cookies.dart +++ b/lib/core_dom/cookies.dart @@ -5,7 +5,6 @@ part of angular.core.dom; * It is not meant to be used directly by applications. Instead * use the Cookies service. * -* NOTE the Cookies service is not yet implemented. */ class BrowserCookies { dom.Document _document; @@ -101,3 +100,21 @@ class BrowserCookies { get all => _updateLastCookies(); } + +class Cookies { + + BrowserCookies _browserCookies; + + Cookies(BrowserCookies this._browserCookies); + + operator[](name) => this._browserCookies[name]; + + operator[]=(name, value) { + this._browserCookies[name] = value; + } + + remove(name) { + this._browserCookies[name] = null; + } +} + diff --git a/lib/core_dom/module.dart b/lib/core_dom/module.dart index 58e607462..691059ca5 100644 --- a/lib/core_dom/module.dart +++ b/lib/core_dom/module.dart @@ -45,6 +45,7 @@ class NgCoreDomModule extends Module { type(BlockCache); type(GetterSetter); type(BrowserCookies); + type(Cookies); type(LocationWrapper); } } diff --git a/test/core_dom/cookies_spec.dart b/test/core_dom/cookies_spec.dart index daff829f8..0e086a9b7 100644 --- a/test/core_dom/cookies_spec.dart +++ b/test/core_dom/cookies_spec.dart @@ -3,26 +3,32 @@ library cookies_spec; import '../_specs.dart'; import 'package:angular/core_dom/module.dart'; -main() => describe('browser cookies', () { - var cookies; - - deleteAllCookies() { - var cookies = document.cookie.split(";"); - var path = window.location.pathname; - - for (var i = 0; i < cookies.length; i++) { - var cookie = cookies[i]; - var eqPos = cookie.indexOf("="); - var name = eqPos > -1 ? cookie.substring(0, eqPos) : ''; - var parts = path.split('/'); - while (!parts.isEmpty) { - var joinedParts = parts.join('/'); - document.cookie = name + "=;path=" + (joinedParts.isEmpty ? '/': joinedParts) + - ";expires=Thu, 01 Jan 1970 00:00:00 GMT"; - parts.removeLast(); - } +main() => describe('cookies', () { + deleteAllCookies() { + var cookies = document.cookie.split(";"); + var path = window.location.pathname; + + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i]; + var eqPos = cookie.indexOf("="); + var name = eqPos > -1 ? cookie.substring(0, eqPos) : ''; + var parts = path.split('/'); + while (!parts.isEmpty) { + var joinedParts = parts.join('/'); + document.cookie = name + "=;path=" + (joinedParts.isEmpty ? '/': joinedParts) + + ";expires=Thu, 01 Jan 1970 00:00:00 GMT"; + parts.removeLast(); } } + } + + afterEach(() { + deleteAllCookies(); + expect(document.cookie).toEqual(''); + }); + + describe('browser cookies', () { + var cookies; beforeEach(inject((BrowserCookies iCookies) { deleteAllCookies(); @@ -31,13 +37,6 @@ main() => describe('browser cookies', () { cookies = iCookies; })); - - afterEach(() { - deleteAllCookies(); - expect(document.cookie).toEqual(''); - }); - - describe('remove via cookies(cookieName, null)', () { it('should remove a cookie when it is present', () { @@ -114,7 +113,7 @@ main() => describe('browser cookies', () { if (document.cookie != cookieStr) { throw "browser didn't drop long cookie when it was expected. make the " + - "cookie in this test longer"; + "cookie in this test longer"; } expect(cookies['x']).toEqual('shortVal'); @@ -196,4 +195,35 @@ main() => describe('browser cookies', () { document.cookie = "existingCookie=existingValue;path=/"; expect(cookies.all).toEqual({'existingCookie':'existingValue'}); }); + }); + + describe('cookies service', () { + var cookiesService; + beforeEach(inject((Cookies iCookies) { + cookiesService = iCookies; + document.cookie = 'oatmealCookie=fresh;path=/'; + })); + + it('should read cookie', () { + expect(cookiesService["oatmealCookie"]).toEqual("fresh"); + }); + + describe("set cookie", () { + it('should set new key value pair', () { + cookiesService["oven"] = "hot"; + expect(document.cookie).toContain("oven=hot"); + }); + + it('should override existing value', () { + cookiesService["oatmealCookie"] = "stale"; + expect(document.cookie).toContain("oatmealCookie=stale"); + }); + }); + + it('should remove cookie', () { + cookiesService.remove("oatmealCookie"); + expect(document.cookie).not.toContain("oatmealCookie"); + }); + }); }); + From 10da7341e6a1f34aaee069edd986df1a368d1c23 Mon Sep 17 00:00:00 2001 From: Kurman Karabukaev Date: Sun, 1 Dec 2013 11:12:18 -0800 Subject: [PATCH 2/4] Docs for cookies service --- lib/core_dom/cookies.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/core_dom/cookies.dart b/lib/core_dom/cookies.dart index 020b4888e..b235cc91c 100644 --- a/lib/core_dom/cookies.dart +++ b/lib/core_dom/cookies.dart @@ -101,18 +101,30 @@ class BrowserCookies { get all => _updateLastCookies(); } +/** + * Cookies service + */ class Cookies { BrowserCookies _browserCookies; Cookies(BrowserCookies this._browserCookies); + /** + * Returns the value of given cookie key + */ operator[](name) => this._browserCookies[name]; + /** + * Sets a value for given cookie key + */ operator[]=(name, value) { this._browserCookies[name] = value; } + /** + * Remove given cookie + */ remove(name) { this._browserCookies[name] = null; } From a65127c8097b4d5fe70883704ab0d7e7cdb64fb2 Mon Sep 17 00:00:00 2001 From: Kurman Karabukaev Date: Fri, 7 Feb 2014 16:35:47 -0800 Subject: [PATCH 3/4] fixing merge issues --- test/core_dom/cookies_spec.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/core_dom/cookies_spec.dart b/test/core_dom/cookies_spec.dart index 595d13bcf..e2d2ab36c 100644 --- a/test/core_dom/cookies_spec.dart +++ b/test/core_dom/cookies_spec.dart @@ -210,6 +210,7 @@ main() => describe('cookies', () { expect(cookies.all).toEqual({'existingCookie':'existingValue'}); }); }); + describe('cookies service', () { var cookiesService; beforeEach(inject((Cookies iCookies) { @@ -232,6 +233,7 @@ main() => describe('cookies', () { expect(document.cookie).toContain("oatmealCookie=stale"); }); }); + it('should remove cookie', () { cookiesService.remove("oatmealCookie"); expect(document.cookie).not.toContain("oatmealCookie"); From 596ca90395e8311a350b7827f0af558ee1afac7d Mon Sep 17 00:00:00 2001 From: Kurman Karabukaev Date: Mon, 7 Apr 2014 17:11:32 -0700 Subject: [PATCH 4/4] Moving hardcoded recorder URL to config class and can be overridden by installing extended PlaybackHttpBackendConfig class in applications. --- lib/playback/playback_http.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/playback/playback_http.dart b/lib/playback/playback_http.dart index 6701fd2ff..e23a5cbb7 100644 --- a/lib/playback/playback_http.dart +++ b/lib/playback/playback_http.dart @@ -12,6 +12,9 @@ import 'package:angular/playback/playback_data.dart' as playback_data; @NgInjectableService() class PlaybackHttpBackendConfig { + + String get recorder_url => '/record'; + requestKey(String url, {String method, bool withCredentials, String responseType, String mimeType, Map requestHeaders, sendData, @@ -63,7 +66,7 @@ class RecordingHttpBackend implements HttpBackend { onProgress: onProgress); assert(key is String); - _prodBackend.request('/record', //TODO make this URL configurable. + _prodBackend.request(_config.recorder_url, method: 'POST', sendData: JSON.encode({ "key": key, "data": JSON.encode({ "status": r.status,