|
| 1 | +package context; |
| 2 | + |
| 3 | +import dev.aikido.agent_api.context.JavalinContextObject; |
| 4 | +import dev.aikido.agent_api.context.RouteMetadata; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +class JavalinContextObjectTest { |
| 13 | + |
| 14 | + private JavalinContextObject contextObject; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + void setUp() { |
| 18 | + String method = "GET"; |
| 19 | + String url = "http://example.com/api/resource"; |
| 20 | + String rawIp = "192.168.1.1"; |
| 21 | + Map<String, List<String>> queryParams = new HashMap<>(); |
| 22 | + queryParams.put("param1", List.of("value1")); |
| 23 | + Map<String, String> cookies = new HashMap<>(); |
| 24 | + cookies.put("sessionId", "abc123"); |
| 25 | + Map<String, String> headers = new HashMap<>(); |
| 26 | + headers.put("Content-Type", "application/json"); |
| 27 | + |
| 28 | + contextObject = new JavalinContextObject(method, url, rawIp, queryParams, cookies, headers); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testConstructor() { |
| 33 | + assertEquals("GET", contextObject.getMethod()); |
| 34 | + assertEquals("http://example.com/api/resource", contextObject.getUrl()); |
| 35 | + assertEquals("192.168.1.1", contextObject.getRemoteAddress()); |
| 36 | + assertEquals("application/json", contextObject.getHeaders().get("content-type")); |
| 37 | + assertEquals(1, contextObject.getQuery().size()); |
| 38 | + assertEquals("value1", contextObject.getQuery().get("param1").get(0)); |
| 39 | + assertEquals(1, contextObject.getCookies().size()); |
| 40 | + assertEquals("abc123", contextObject.getCookies().get("sessionId").get(0)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSetParams() { |
| 45 | + Object params = new HashMap<String, String>() {{ |
| 46 | + put("key", "value"); |
| 47 | + }}; |
| 48 | + contextObject.setParams(params); |
| 49 | + assertEquals(params, contextObject.getParams()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testSetBody() { |
| 54 | + Object body = new HashMap<String, String>() {{ |
| 55 | + put("field", "data"); |
| 56 | + }}; |
| 57 | + contextObject.setBody(body); |
| 58 | + assertEquals(body, contextObject.getBody()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testSetExecutedMiddleware() { |
| 63 | + contextObject.setExecutedMiddleware(true); |
| 64 | + assertTrue(contextObject.middlewareExecuted()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testToJson() { |
| 69 | + String json = contextObject.toJson(); |
| 70 | + assertNotNull(json); |
| 71 | + assertTrue(json.contains("GET")); |
| 72 | + assertTrue(json.contains("http://example.com/api/resource")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testGetRouteMetadata() { |
| 77 | + RouteMetadata metadata = contextObject.getRouteMetadata(); |
| 78 | + assertNotNull(metadata); |
| 79 | + assertEquals(contextObject.getRoute(), metadata.route()); |
| 80 | + assertEquals(contextObject.getUrl(), metadata.url()); |
| 81 | + assertEquals(contextObject.getMethod(), metadata.method()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testHeadersExtraction() { |
| 86 | + // Test headers extraction through the constructor |
| 87 | + assertEquals("application/json", contextObject.getHeaders().get("content-type")); |
| 88 | + assertEquals(1, contextObject.getHeaders().size()); |
| 89 | + assertTrue(contextObject.getHeaders().containsKey("content-type")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testCookiesExtraction() { |
| 94 | + // Test cookies extraction through the constructor |
| 95 | + assertEquals(1, contextObject.getCookies().size()); |
| 96 | + assertTrue(contextObject.getCookies().containsKey("sessionId")); |
| 97 | + assertEquals("abc123", contextObject.getCookies().get("sessionId").get(0)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testMultipleCookiesExtraction() { |
| 102 | + // Test with multiple cookies |
| 103 | + Map<String, String> cookies = new HashMap<>(); |
| 104 | + cookies.put("sessionId", "abc123"); |
| 105 | + cookies.put("userId", "user456"); |
| 106 | + contextObject = new JavalinContextObject("GET", "http://example.com", "192.168.1.1", new HashMap<>(), cookies, new HashMap<>()); |
| 107 | + |
| 108 | + assertEquals("abc123", contextObject.getCookies().get("sessionId").get(0)); |
| 109 | + assertEquals("user456", contextObject.getCookies().get("userId").get(0)); |
| 110 | + assertEquals(2, contextObject.getCookies().size()); |
| 111 | + } |
| 112 | +} |
0 commit comments