@@ -127,6 +127,72 @@ describe('$$cookieWriter', function() {
127127 expect ( document . cookie ) . toEqual ( 'cookie=bender' ) ;
128128 } ) ;
129129 } ) ;
130-
131130} ) ;
132131
132+ describe ( 'cookie options' , function ( ) {
133+ var fakeDocument , $$cookieWriter ;
134+
135+ function getLastCookieAssignment ( key ) {
136+ return fakeDocument [ 0 ] . cookie
137+ . split ( ';' )
138+ . reduce ( function ( prev , value ) {
139+ var pair = value . split ( '=' , 2 ) ;
140+ if ( pair [ 0 ] === key ) {
141+ if ( prev === undefined ) {
142+ return pair [ 1 ] === undefined ? true : pair [ 1 ] ;
143+ } else {
144+ throw 'duplicate key in cookie string' ;
145+ }
146+ } else {
147+ return prev ;
148+ }
149+ } , undefined ) ;
150+ }
151+
152+ beforeEach ( function ( ) {
153+ fakeDocument = [ { cookie : '' } ] ;
154+ module ( 'ngCookies' , { $document : fakeDocument } ) ;
155+ inject ( function ( $browser ) {
156+ $browser . $$baseHref = '/a/b' ;
157+ } ) ;
158+ inject ( function ( _$$cookieWriter_ ) {
159+ $$cookieWriter = _$$cookieWriter_ ;
160+ } ) ;
161+ } ) ;
162+
163+ it ( 'should use baseHref as default path' , function ( ) {
164+ $$cookieWriter ( 'name' , 'value' ) ;
165+ expect ( getLastCookieAssignment ( 'path' ) ) . toBe ( '/a/b' ) ;
166+ } ) ;
167+
168+ it ( 'should accept path option' , function ( ) {
169+ $$cookieWriter ( 'name' , 'value' , { path : '/c/d' } ) ;
170+ expect ( getLastCookieAssignment ( 'path' ) ) . toBe ( '/c/d' ) ;
171+ } ) ;
172+
173+ it ( 'should accept domain option' , function ( ) {
174+ $$cookieWriter ( 'name' , 'value' , { domain : '.example.com' } ) ;
175+ expect ( getLastCookieAssignment ( 'domain' ) ) . toBe ( '.example.com' ) ;
176+ } ) ;
177+
178+ it ( 'should accept secure option' , function ( ) {
179+ $$cookieWriter ( 'name' , 'value' , { secure : true } ) ;
180+ expect ( getLastCookieAssignment ( 'secure' ) ) . toBe ( true ) ;
181+ } ) ;
182+
183+ it ( 'should accept expires option on set' , function ( ) {
184+ $$cookieWriter ( 'name' , 'value' , { expires : 'Fri, 19 Dec 2014 00:00:00 GMT' } ) ;
185+ expect ( getLastCookieAssignment ( 'expires' ) ) . toMatch ( / ^ F r i , 1 9 D e c 2 0 1 4 0 0 : 0 0 : 0 0 ( U T C | G M T ) $ / ) ;
186+ } ) ;
187+
188+ it ( 'should always use epoch time as expire time on remove' , function ( ) {
189+ $$cookieWriter ( 'name' , undefined , { expires : 'Fri, 19 Dec 2014 00:00:00 GMT' } ) ;
190+ expect ( getLastCookieAssignment ( 'expires' ) ) . toMatch ( / ^ T h u , 0 ? 1 J a n 1 9 7 0 0 0 : 0 0 : 0 0 ( U T C | G M T ) $ / ) ;
191+ } ) ;
192+
193+ it ( 'should accept date object as expires option' , function ( ) {
194+ $$cookieWriter ( 'name' , 'value' , { expires : new Date ( Date . UTC ( 1981 , 11 , 27 ) ) } ) ;
195+ expect ( getLastCookieAssignment ( 'expires' ) ) . toMatch ( / ^ S u n , 2 7 D e c 1 9 8 1 0 0 : 0 0 : 0 0 ( U T C | G M T ) $ / ) ;
196+ } ) ;
197+
198+ } ) ;
0 commit comments