@@ -22,6 +22,7 @@ describe("createDomainLabels", () => {
2222 previewDeploymentId : "" ,
2323 internalPath : "/" ,
2424 stripPath : false ,
25+ middlewares : null ,
2526 } ;
2627
2728 it ( "should create basic labels for web entrypoint" , async ( ) => {
@@ -172,12 +173,12 @@ describe("createDomainLabels", () => {
172173 "websecure" ,
173174 ) ;
174175
175- // Web entrypoint should have both middlewares with redirect first
176+ // Web entrypoint with HTTPS should only have redirect
176177 expect ( webLabels ) . toContain (
177- "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file,addprefix-test-app-1 " ,
178+ "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file" ,
178179 ) ;
179180
180- // Websecure should only have the addprefix middleware
181+ // Websecure should have the addprefix middleware
181182 expect ( websecureLabels ) . toContain (
182183 "traefik.http.routers.test-app-1-websecure.middlewares=addprefix-test-app-1" ,
183184 ) ;
@@ -209,9 +210,9 @@ describe("createDomainLabels", () => {
209210 "traefik.http.middlewares.addprefix-test-app-1.addprefix.prefix=/hello" ,
210211 ) ;
211212
212- // Should have middlewares in correct order: redirect, stripprefix, addprefix
213+ // Web router with HTTPS should only have redirect
213214 expect ( webLabels ) . toContain (
214- "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file,stripprefix-test-app-1,addprefix-test-app-1 " ,
215+ "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file" ,
215216 ) ;
216217 } ) ;
217218
@@ -242,6 +243,131 @@ describe("createDomainLabels", () => {
242243 ) ;
243244 } ) ;
244245
246+ it ( "should add single custom middleware to router" , async ( ) => {
247+ const customMiddlewareDomain = {
248+ ...baseDomain ,
249+ middlewares : [ "auth@file" ] ,
250+ } ;
251+ const labels = await createDomainLabels (
252+ appName ,
253+ customMiddlewareDomain ,
254+ "web" ,
255+ ) ;
256+
257+ expect ( labels ) . toContain (
258+ "traefik.http.routers.test-app-1-web.middlewares=auth@file" ,
259+ ) ;
260+ } ) ;
261+
262+ it ( "should add multiple custom middlewares to router" , async ( ) => {
263+ const customMiddlewareDomain = {
264+ ...baseDomain ,
265+ middlewares : [ "auth@file" , "rate-limit@file" ] ,
266+ } ;
267+ const labels = await createDomainLabels (
268+ appName ,
269+ customMiddlewareDomain ,
270+ "web" ,
271+ ) ;
272+
273+ expect ( labels ) . toContain (
274+ "traefik.http.routers.test-app-1-web.middlewares=auth@file,rate-limit@file" ,
275+ ) ;
276+ } ) ;
277+
278+ it ( "should only have redirect on web router when HTTPS is enabled with custom middlewares" , async ( ) => {
279+ const combinedDomain = {
280+ ...baseDomain ,
281+ https : true ,
282+ middlewares : [ "auth@file" ] ,
283+ } ;
284+ const labels = await createDomainLabels ( appName , combinedDomain , "web" ) ;
285+
286+ // Web router with HTTPS should only redirect, custom middlewares go on websecure
287+ expect ( labels ) . toContain (
288+ "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file" ,
289+ ) ;
290+ expect ( labels ) . not . toContain ( "auth@file" ) ;
291+ } ) ;
292+
293+ it ( "should combine custom middlewares with stripPath middleware (no HTTPS)" , async ( ) => {
294+ const combinedDomain = {
295+ ...baseDomain ,
296+ path : "/api" ,
297+ stripPath : true ,
298+ middlewares : [ "auth@file" ] ,
299+ } ;
300+ const labels = await createDomainLabels ( appName , combinedDomain , "web" ) ;
301+
302+ // stripprefix should come before custom middleware
303+ expect ( labels ) . toContain (
304+ "traefik.http.routers.test-app-1-web.middlewares=stripprefix-test-app-1,auth@file" ,
305+ ) ;
306+ } ) ;
307+
308+ it ( "should only have redirect on web router even with all built-in middlewares and custom middlewares" , async ( ) => {
309+ const fullDomain = {
310+ ...baseDomain ,
311+ https : true ,
312+ path : "/api" ,
313+ stripPath : true ,
314+ internalPath : "/hello" ,
315+ middlewares : [ "auth@file" , "rate-limit@file" ] ,
316+ } ;
317+ const webLabels = await createDomainLabels ( appName , fullDomain , "web" ) ;
318+
319+ // Web router with HTTPS should only redirect
320+ expect ( webLabels ) . toContain (
321+ "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file" ,
322+ ) ;
323+ // Middleware definitions should still be present (Traefik needs them registered)
324+ expect ( webLabels ) . toContain (
325+ "traefik.http.middlewares.stripprefix-test-app-1.stripprefix.prefixes=/api" ,
326+ ) ;
327+ expect ( webLabels ) . toContain (
328+ "traefik.http.middlewares.addprefix-test-app-1.addprefix.prefix=/hello" ,
329+ ) ;
330+ // But they should NOT be attached to the router
331+ expect ( webLabels ) . not . toContain ( "stripprefix-test-app-1," ) ;
332+ expect ( webLabels ) . not . toContain ( "auth@file" ) ;
333+ expect ( webLabels ) . not . toContain ( "rate-limit@file" ) ;
334+ } ) ;
335+
336+ it ( "should include custom middlewares on websecure entrypoint" , async ( ) => {
337+ const customMiddlewareDomain = {
338+ ...baseDomain ,
339+ https : true ,
340+ middlewares : [ "auth@file" ] ,
341+ } ;
342+ const websecureLabels = await createDomainLabels (
343+ appName ,
344+ customMiddlewareDomain ,
345+ "websecure" ,
346+ ) ;
347+
348+ // Websecure should have custom middleware but not redirect-to-https
349+ expect ( websecureLabels ) . toContain (
350+ "traefik.http.routers.test-app-1-websecure.middlewares=auth@file" ,
351+ ) ;
352+ expect ( websecureLabels ) . not . toContain ( "redirect-to-https" ) ;
353+ } ) ;
354+
355+ it ( "should NOT include custom middlewares on web router when HTTPS is enabled (only redirect)" , async ( ) => {
356+ const domain = {
357+ ...baseDomain ,
358+ https : true ,
359+ middlewares : [ "rate-limit@file" , "auth@file" ] ,
360+ } ;
361+ const webLabels = await createDomainLabels ( appName , domain , "web" ) ;
362+
363+ // Web router with HTTPS should ONLY have redirect, not custom middlewares
364+ expect ( webLabels ) . toContain (
365+ "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file" ,
366+ ) ;
367+ expect ( webLabels ) . not . toContain ( "rate-limit@file" ) ;
368+ expect ( webLabels ) . not . toContain ( "auth@file" ) ;
369+ } ) ;
370+
245371 it ( "should create basic labels for custom entrypoint" , async ( ) => {
246372 const labels = await createDomainLabels (
247373 appName ,
0 commit comments