Преглед на файлове

Feature (upload): Added withCredentials configuration.

Victor Voisin преди 5 години
родител
ревизия
43bc5ad159

+ 3 - 0
packages/ckeditor5-upload/docs/features/simple-upload-adapter.md

@@ -62,6 +62,9 @@ ClassicEditor
 			// The URL that the images are uploaded to.
 			uploadUrl: 'http://example.com',
 
+			// Enable XMLHttpRequest.withCredentials property
+			withCredentials: true,
+
 			// Headers sent along with the XMLHttpRequest to the upload server.
 			headers: {
 				'X-CSRF-TOKEN': 'CSFR-Token',

+ 6 - 0
packages/ckeditor5-upload/src/adapters/simpleuploadadapter.js

@@ -205,6 +205,12 @@ class Adapter {
 			this.xhr.setRequestHeader( headerName, headers[ headerName ] );
 		}
 
+		// Set withCredentials if specified
+		const withCredentials = this.options.withCredentials || false;
+		if ( withCredentials ) {
+			this.xhr.withCredentials = true;
+		}
+
 		// Prepare the form data.
 		const data = new FormData();
 

+ 39 - 0
packages/ckeditor5-upload/tests/adapters/simpleuploadadapter.js

@@ -329,6 +329,45 @@ describe( 'SimpleUploadAdapter', () => {
 					expect( loader.uploaded ).to.equal( 4 );
 				} );
 			} );
+
+			it( 'should use withCredentials in the request (when specified)', () => {
+				const editorElement = document.createElement( 'div' );
+				document.body.appendChild( editorElement );
+
+				return ClassicTestEditor
+					.create( editorElement, {
+						plugins: [ SimpleUploadAdapter ],
+						simpleUpload: {
+							uploadUrl: 'http://example.com',
+							withCredentials: true
+						}
+					} )
+					.then( editor => {
+						const adapter = editor.plugins.get( FileRepository ).createUploadAdapter( loader );
+						const validResponse = {
+							url: 'http://example.com/images/image.jpeg'
+						};
+
+						const uploadPromise = adapter.upload();
+
+						return loader.file
+							.then( () => {
+								const request = sinonXHR.requests[ 0 ];
+								request.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( validResponse ) );
+
+								expect( request ).to.have.property( 'withCredentials', true );
+
+								return uploadPromise;
+							} )
+							.then( uploadResponse => {
+								expect( uploadResponse ).to.be.a( 'object' );
+								expect( uploadResponse ).to.have.property( 'default', 'http://example.com/images/image.jpeg' );
+
+								editorElement.remove();
+							} )
+							.then( () => editor.destroy() );
+					} );
+			} );
 		} );
 	} );
 } );