@@ -529,4 +529,98 @@ describe('Request tests', () => {
529529 requestSpy . mockRestore ( ) ;
530530 } ) ;
531531 } ) ;
532+
533+ describe ( 'URL length optimization for includeReference parameters' , ( ) => {
534+ it ( 'should use compact format when URL with many include[] parameters exceeds threshold' , async ( ) => {
535+ const client = httpClient ( { defaultHostname : 'example.com' } ) ;
536+ const mock = new MockAdapter ( client as any ) ;
537+ const url = '/content_types/blog/entries/entry123' ;
538+ const mockResponse = { entry : { uid : 'entry123' , title : 'Test' } } ;
539+
540+ // Create many include[] parameters that would make URL long
541+ const manyIncludes = Array . from ( { length : 100 } , ( _ , i ) => `ref_field_${ i } ` ) ;
542+ const requestData = { params : { 'include[]' : manyIncludes } } ;
543+
544+ mock . onGet ( url ) . reply ( 200 , mockResponse ) ;
545+
546+ const result = await getData ( client , url , requestData ) ;
547+ expect ( result ) . toEqual ( mockResponse ) ;
548+
549+ // Verify the request was made (URL optimization allowed it to succeed)
550+ expect ( mock . history . get . length ) . toBe ( 1 ) ;
551+ const requestUrl = mock . history . get [ 0 ] . url || '' ;
552+ // With compact format, the URL should be shorter and contain comma-separated values
553+ // We verify success means the optimization worked
554+ expect ( requestUrl . length ) . toBeLessThan ( 3000 ) ;
555+ } ) ;
556+
557+ it ( 'should use compact format for Live Preview requests with lower threshold' , async ( ) => {
558+ const client = httpClient ( { defaultHostname : 'example.com' } ) ;
559+ const mock = new MockAdapter ( client as any ) ;
560+ const url = '/content_types/blog/entries/entry123' ;
561+ const mockResponse = { entry : { uid : 'entry123' , title : 'Test' } } ;
562+
563+ client . stackConfig = {
564+ live_preview : {
565+ enable : true ,
566+ preview_token : 'someToken' ,
567+ live_preview : 'someHash' ,
568+ host : 'rest-preview.contentstack.com' ,
569+ } ,
570+ } ;
571+
572+ // Create include[] parameters that would exceed 1500 chars for Live Preview
573+ // but might be okay for regular requests (2000 chars)
574+ const manyIncludes = Array . from ( { length : 80 } , ( _ , i ) => `ref_field_${ i } _with_long_name` ) ;
575+ const requestData = { params : { 'include[]' : manyIncludes } } ;
576+
577+ const livePreviewUrl = 'https://rest-preview.contentstack.com' + url ;
578+ mock . onGet ( livePreviewUrl ) . reply ( 200 , mockResponse ) ;
579+
580+ const result = await getData ( client , url , requestData ) ;
581+ expect ( result ) . toEqual ( mockResponse ) ;
582+
583+ // Verify the request was made to Live Preview host
584+ expect ( mock . history . get . length ) . toBe ( 1 ) ;
585+ expect ( mock . history . get [ 0 ] . url ) . toContain ( 'rest-preview.contentstack.com' ) ;
586+ } ) ;
587+
588+ it ( 'should throw error when URL is too long even with compact format' , async ( ) => {
589+ const client = httpClient ( { defaultHostname : 'example.com' } ) ;
590+ const url = '/content_types/blog/entries/entry123' ;
591+
592+ client . stackConfig = {
593+ live_preview : {
594+ enable : true ,
595+ preview_token : 'someToken' ,
596+ live_preview : 'someHash' ,
597+ host : 'rest-preview.contentstack.com' ,
598+ } ,
599+ } ;
600+
601+ // Create an extremely large number of includes that would exceed even compact format
602+ const manyIncludes = Array . from ( { length : 500 } , ( _ , i ) => `very_long_reference_field_name_${ i } _with_many_characters` ) ;
603+ const requestData = { params : { 'include[]' : manyIncludes } } ;
604+
605+ await expect ( getData ( client , url , requestData ) ) . rejects . toThrow ( / e x c e e d s t h e m a x i m u m a l l o w e d l e n g t h / ) ;
606+ } ) ;
607+
608+ it ( 'should use standard format when URL length is within threshold' , async ( ) => {
609+ const client = httpClient ( { defaultHostname : 'example.com' } ) ;
610+ const mock = new MockAdapter ( client as any ) ;
611+ const url = '/content_types/blog/entries/entry123' ;
612+ const mockResponse = { entry : { uid : 'entry123' , title : 'Test' } } ;
613+
614+ // Create a small number of includes that won't exceed threshold
615+ const requestData = { params : { 'include[]' : [ 'ref1' , 'ref2' , 'ref3' ] } } ;
616+
617+ mock . onGet ( url ) . reply ( 200 , mockResponse ) ;
618+
619+ const result = await getData ( client , url , requestData ) ;
620+ expect ( result ) . toEqual ( mockResponse ) ;
621+
622+ // Verify the request was made successfully
623+ expect ( mock . history . get . length ) . toBe ( 1 ) ;
624+ } ) ;
625+ } ) ;
532626} ) ;
0 commit comments