@@ -17,6 +17,10 @@ function getCsp(response: Response) {
1717 return csp ?. split ( ';' ) . map ( ( s ) => s . trim ( ) ) ;
1818}
1919
20+ function getLinkHeaders ( response : Response ) {
21+ return response . headers . get ( 'Link' ) ?. split ( ', ' ) ;
22+ }
23+
2024const OLD_ENV = { ...process . env } ;
2125afterAll ( ( ) => {
2226 process . env = OLD_ENV ;
@@ -36,6 +40,10 @@ describe('middleware function', () => {
3640 const response = await middleware ( request ) ;
3741
3842 expect ( response . status ) . toBe ( 404 ) ;
43+ expect ( getLinkHeaders ( response ) ) . toEqual ( [
44+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-55Roman.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
45+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-65Bold.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
46+ ] ) ;
3947 } ) ;
4048
4149 it ( 'if request path is protected, and no access/id token is obtained, redirect to auth page' , async ( ) => {
@@ -61,7 +69,7 @@ describe('middleware function', () => {
6169 expect ( response . cookies . get ( 'csrf_token' ) ?. value ) . toEqual ( '' ) ;
6270 } ) ;
6371
64- it ( 'if request path is protected, tokens exist AND token has client-id, respond with CSP' , async ( ) => {
72+ it ( 'if request path is protected, tokens exist AND token has client-id, respond with CSP and links to preload fonts ' , async ( ) => {
6573 getTokenMock . mockResolvedValueOnce ( {
6674 accessToken : 'access-token' ,
6775 clientId : 'client1' ,
@@ -92,6 +100,11 @@ describe('middleware function', () => {
92100 'upgrade-insecure-requests' ,
93101 '' ,
94102 ] ) ;
103+
104+ expect ( getLinkHeaders ( response ) ) . toEqual ( [
105+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-55Roman.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
106+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-65Bold.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
107+ ] ) ;
95108 } ) ;
96109
97110 it ( 'if request path is protected, tokens exist BUT token missing client-id, redirect to request-to-be-added page' , async ( ) => {
@@ -113,7 +126,7 @@ describe('middleware function', () => {
113126 ) ;
114127 } ) ;
115128
116- it ( 'if request path is not protected, respond with CSP' , async ( ) => {
129+ it ( 'if request path is not protected, respond with CSP and links to preload fonts ' , async ( ) => {
117130 const url = new URL ( 'https://url.com/create-and-submit-templates' ) ;
118131 const request = new NextRequest ( url ) ;
119132 const response = await middleware ( request ) ;
@@ -136,6 +149,11 @@ describe('middleware function', () => {
136149 'upgrade-insecure-requests' ,
137150 '' ,
138151 ] ) ;
152+
153+ expect ( getLinkHeaders ( response ) ) . toEqual ( [
154+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-55Roman.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
155+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-65Bold.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
156+ ] ) ;
139157 } ) ;
140158
141159 it ( 'public path (/auth/request-to-be-added-to-a-service) responds with CSP' , async ( ) => {
@@ -163,6 +181,11 @@ describe('middleware function', () => {
163181 'upgrade-insecure-requests' ,
164182 '' ,
165183 ] ) ;
184+
185+ expect ( getLinkHeaders ( response ) ) . toEqual ( [
186+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-55Roman.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
187+ '<https://assets.nhs.uk/fonts/FrutigerLTW01-65Bold.woff2>; rel=preload; as=font; crossorigin=anonymous' ,
188+ ] ) ;
166189 } ) ;
167190
168191 it ( 'when running in development mode, CSP script-src allows unsafe-eval and does not upgrade insecure requests' , async ( ) => {
0 commit comments