@@ -70,38 +70,52 @@ describe('storage.FunctionBuilder', () => {
7070 expect ( ( ) => storage . bucket ( 'bad/bucket/format' ) ) . to . throw ( Error ) ;
7171 } ) ;
7272
73- it ( 'should filter out spurious deploy-time events' , ( ) => {
74- let functionRan = false ;
75- let cloudFunction = storage . object ( ) . onChange ( ( ) => {
76- functionRan = true ;
77- return null ;
73+ it ( 'should correct nested media links using a single literal slash' , ( ) => {
74+ let cloudFunction = storage . object ( ) . onChange ( ( event ) => {
75+ return event . data . mediaLink ;
7876 } ) ;
77+ let badMediaLinkEvent = {
78+ data : {
79+ mediaLink : 'https://www.googleapis.com/storage/v1/b/mybucket.appspot.com'
80+ + '/o/nestedfolder/myobject.file?generation=12345&alt=media' ,
81+ } ,
82+ } ;
83+ return cloudFunction ( badMediaLinkEvent ) . then ( result => {
84+ expect ( result ) . equals (
85+ 'https://www.googleapis.com/storage/v1/b/mybucket.appspot.com'
86+ + '/o/nestedfolder%2Fmyobject.file?generation=12345&alt=media' ) ;
87+ } ) ;
88+ } ) ;
7989
80- let spuriousEvent = {
81- timestamp : '2017-03-06T20:02:05.192Z' ,
82- eventType : 'providers/cloud.storage/eventTypes/object.change' ,
83- resource : 'projects/_/buckets/rjh-20170306.appspot.com/objects/#0' ,
90+ it ( 'should correct nested media links using multiple literal slashes' , ( ) => {
91+ let cloudFunction = storage . object ( ) . onChange ( ( event ) => {
92+ return event . data . mediaLink ;
93+ } ) ;
94+ let badMediaLinkEvent = {
95+ data : {
96+ mediaLink : 'https://www.googleapis.com/storage/v1/b/mybucket.appspot.com'
97+ + '/o/nestedfolder/anotherfolder/myobject.file?generation=12345&alt=media' ,
98+ } ,
99+ } ;
100+ return cloudFunction ( badMediaLinkEvent ) . then ( result => {
101+ expect ( result ) . equals (
102+ 'https://www.googleapis.com/storage/v1/b/mybucket.appspot.com'
103+ + '/o/nestedfolder%2Fanotherfolder%2Fmyobject.file?generation=12345&alt=media' ) ;
104+ } ) ;
105+ } ) ;
106+
107+ it ( 'should not mess with media links using non-literal slashes' , ( ) => {
108+ let cloudFunction = storage . object ( ) . onChange ( ( event ) => {
109+ return event . data . mediaLink ;
110+ } ) ;
111+ let goodMediaLinkEvent = {
84112 data : {
85- kind : 'storage#object' ,
86- resourceState : 'exists' ,
87- id : 'rjh-20170306.appspot.com//0' ,
88- selfLink : 'https://www.googleapis.com/storage/v1/b/rjh-20170306.appspot.com/o/' ,
89- bucket : 'rjh-20170306.appspot.com' ,
90- generation : '0' ,
91- metageneration : '0' ,
92- contentType : '' ,
93- timeCreated : '1970-01-01T00:00:00.000Z' ,
94- updated : '1970-01-01T00:00:00.000Z' ,
95- size : '0' ,
96- md5Hash : '' ,
97- mediaLink : 'https://www.googleapis.com/storage/v1/b/rjh-20170306.appspot.com/o/?generation=0&alt=media' ,
98- crc32c : 'AAAAAA==' ,
113+ mediaLink : 'https://www.googleapis.com/storage/v1/b/mybucket.appspot.com'
114+ + '/o/nestedfolder%2Fanotherfolder%2Fmyobject.file?generation=12345&alt=media' ,
99115 } ,
100- params : { } ,
101116 } ;
102- return cloudFunction ( spuriousEvent ) . then ( ( result ) => {
103- expect ( result ) . equals ( null ) ;
104- expect ( functionRan ) . equals ( false ) ;
117+ return cloudFunction ( goodMediaLinkEvent ) . then ( result => {
118+ expect ( result ) . equals ( goodMediaLinkEvent . data . mediaLink ) ;
105119 } ) ;
106120 } ) ;
107121 } ) ;
0 commit comments