|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +public class ContextSelectorTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testSingleApplicationSelector() { |
| 22 | + Jooby app = mock(Jooby.class); |
| 23 | + // Selector.create calls single() when list size is 1 |
| 24 | + Context.Selector selector = Context.Selector.create(Collections.singletonList(app)); |
| 25 | + |
| 26 | + assertEquals( |
| 27 | + app, selector.select("/any/path"), "Single app selector should always return the app"); |
| 28 | + assertEquals(app, selector.select("/"), "Single app selector should always return the app"); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testMultipleApplicationSelectorMatching() { |
| 33 | + Jooby mainApp = mock(Jooby.class); |
| 34 | + when(mainApp.getContextPath()).thenReturn("/"); |
| 35 | + |
| 36 | + Jooby apiApp = mock(Jooby.class); |
| 37 | + when(apiApp.getContextPath()).thenReturn("/api"); |
| 38 | + |
| 39 | + Jooby adminApp = mock(Jooby.class); |
| 40 | + when(adminApp.getContextPath()).thenReturn("/admin"); |
| 41 | + |
| 42 | + // Selector.create calls multiple() when size > 1 |
| 43 | + List<Jooby> apps = Arrays.asList(mainApp, apiApp, adminApp); |
| 44 | + Context.Selector selector = Context.Selector.create(apps); |
| 45 | + |
| 46 | + // Exact matches / Prefix matches |
| 47 | + assertEquals(apiApp, selector.select("/api"), "Should match /api"); |
| 48 | + assertEquals(apiApp, selector.select("/api/v1/users"), "Should match prefix /api"); |
| 49 | + assertEquals(adminApp, selector.select("/admin/settings"), "Should match prefix /admin"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testMultipleApplicationSelectorFallback() { |
| 54 | + Jooby mainApp = mock(Jooby.class); |
| 55 | + when(mainApp.getContextPath()).thenReturn("/"); |
| 56 | + |
| 57 | + Jooby otherApp = mock(Jooby.class); |
| 58 | + when(otherApp.getContextPath()).thenReturn("/other"); |
| 59 | + |
| 60 | + Context.Selector selector = Context.Selector.create(Arrays.asList(mainApp, otherApp)); |
| 61 | + |
| 62 | + // Fallback to the app defined with "/" context path |
| 63 | + assertEquals( |
| 64 | + mainApp, selector.select("/unknown"), "Should fallback to app with '/' context path"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testMultipleApplicationSelectorFallbackToFirst() { |
| 69 | + Jooby app1 = mock(Jooby.class); |
| 70 | + when(app1.getContextPath()).thenReturn("/foo"); |
| 71 | + |
| 72 | + Jooby app2 = mock(Jooby.class); |
| 73 | + when(app2.getContextPath()).thenReturn("/bar"); |
| 74 | + |
| 75 | + // List without a "/" context path |
| 76 | + Context.Selector selector = Context.Selector.create(Arrays.asList(app1, app2)); |
| 77 | + |
| 78 | + // If no app has "/" and no prefix matches, it returns the first app in the list |
| 79 | + assertEquals( |
| 80 | + app1, |
| 81 | + selector.select("/baz"), |
| 82 | + "Should fallback to the first app if no '/' context path is found"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testSelectorOrderPrecedence() { |
| 87 | + Jooby app1 = mock(Jooby.class); |
| 88 | + when(app1.getContextPath()).thenReturn("/v1"); |
| 89 | + |
| 90 | + Jooby app2 = mock(Jooby.class); |
| 91 | + when(app2.getContextPath()).thenReturn("/v1/api"); |
| 92 | + |
| 93 | + // Order matters in the provided implementation. It iterates and returns the first match. |
| 94 | + Context.Selector selector = Context.Selector.create(Arrays.asList(app1, app2)); |
| 95 | + |
| 96 | + // Since app1 (/v1) is first, it will consume /v1/api/test because it starts with /v1 |
| 97 | + assertEquals(app1, selector.select("/v1/api/test")); |
| 98 | + } |
| 99 | +} |
0 commit comments