|
2 | 2 |
|
3 | 3 | package com.amazonaws.services.lambda.runtime.api.client.api; |
4 | 4 |
|
| 5 | +import com.amazonaws.services.lambda.runtime.ClientContext; |
| 6 | +import com.amazonaws.services.lambda.runtime.Context; |
| 7 | +import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer; |
| 8 | +import com.amazonaws.services.lambda.runtime.serialization.factories.GsonFactory; |
5 | 9 | import org.junit.jupiter.api.Test; |
6 | 10 |
|
| 11 | +import java.util.Map; |
| 12 | + |
7 | 13 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
8 | 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 17 |
|
10 | 18 | public class LambdaContextTest { |
@@ -58,4 +66,97 @@ private LambdaContext createContextWithDeadline(long deadlineTimeInMs) { |
58 | 66 | return new LambdaContext(MEMORY_LIMIT, deadlineTimeInMs, REQUEST_ID, LOG_GROUP_NAME, LOG_STREAM_NAME, |
59 | 67 | FUNCTION_NAME, IDENTITY, FUNCTION_VERSION, INVOKED_FUNCTION_ARN, TENANT_ID, X_RAY_TRACE_ID, CLIENT_CONTEXT); |
60 | 68 | } |
| 69 | + |
| 70 | + private LambdaContext w3cContext(ClientContext clientContext) { |
| 71 | + return new LambdaContext(MEMORY_LIMIT, System.currentTimeMillis() + 1000, REQUEST_ID, LOG_GROUP_NAME, |
| 72 | + LOG_STREAM_NAME, FUNCTION_NAME, IDENTITY, FUNCTION_VERSION, INVOKED_FUNCTION_ARN, TENANT_ID, |
| 73 | + X_RAY_TRACE_ID, clientContext); |
| 74 | + } |
| 75 | + |
| 76 | + private LambdaClientContext clientContextFromJson(String json) { |
| 77 | + PojoSerializer<LambdaClientContext> serializer = |
| 78 | + GsonFactory.getInstance().getSerializer(LambdaClientContext.class); |
| 79 | + return serializer.fromJson(json); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void w3c_returnsEmptyWhenClientContextIsNull() { |
| 84 | + assertTrue(w3cContext(null).w3c().isEmpty()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void w3c_returnsEmptyWhenNoW3cKey() { |
| 89 | + LambdaClientContext cc = clientContextFromJson("{\"custom\": {\"value\": \"test\"}}"); |
| 90 | + assertTrue(w3cContext(cc).w3c().isEmpty()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void w3c_returnsAllAllowlistedFields() { |
| 95 | + LambdaClientContext cc = clientContextFromJson( |
| 96 | + "{\"w3c\": {" + |
| 97 | + "\"traceparent\": \"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01\"," + |
| 98 | + "\"tracestate\": \"rojo=00f067aa0ba902b7\"," + |
| 99 | + "\"baggage\": \"userId=alice\"}}"); |
| 100 | + |
| 101 | + Map<String, String> w3c = w3cContext(cc).w3c(); |
| 102 | + |
| 103 | + assertEquals(3, w3c.size()); |
| 104 | + assertEquals("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", w3c.get("traceparent")); |
| 105 | + assertEquals("rojo=00f067aa0ba902b7", w3c.get("tracestate")); |
| 106 | + assertEquals("userId=alice", w3c.get("baggage")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void w3c_dropsNonAllowlistedKeysAndNonStringValues() { |
| 111 | + LambdaClientContext cc = clientContextFromJson( |
| 112 | + "{\"w3c\": {" + |
| 113 | + "\"baggage\": \"keep=me\"," + |
| 114 | + "\"unknownField\": \"drop-me\"," + |
| 115 | + "\"traceparent\": 42," + |
| 116 | + "\"tracestate\": null}}"); |
| 117 | + |
| 118 | + Map<String, String> w3c = w3cContext(cc).w3c(); |
| 119 | + |
| 120 | + assertEquals(1, w3c.size()); |
| 121 | + assertEquals("keep=me", w3c.get("baggage")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void w3c_treatsNonObjectPayloadAsEmpty() { |
| 126 | + assertTrue(w3cContext(clientContextFromJson("{\"w3c\": \"not-an-object\"}")).w3c().isEmpty()); |
| 127 | + assertTrue(w3cContext(clientContextFromJson("{\"w3c\": [\"baggage=abc\"]}")).w3c().isEmpty()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void w3c_isStrippedFromClientContextAndIsUnmodifiable() { |
| 132 | + LambdaClientContext cc = clientContextFromJson( |
| 133 | + "{\"custom\": {\"value\": \"test\"}, \"w3c\": {\"baggage\": \"userId=alice\"}}"); |
| 134 | + |
| 135 | + LambdaContext context = w3cContext(cc); |
| 136 | + |
| 137 | + assertEquals("userId=alice", context.w3c().get("baggage")); |
| 138 | + assertThrows(UnsupportedOperationException.class, () -> context.w3c().put("x", "y")); |
| 139 | + assertEquals("test", cc.getCustom().get("value")); |
| 140 | + assertTrue(w3cContext(cc).w3c().isEmpty()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void w3c_defaultsToEmptyOnBareContextInterface() { |
| 145 | + Context bare = new Context() { |
| 146 | + public String getAwsRequestId() { return null; } |
| 147 | + public String getLogGroupName() { return null; } |
| 148 | + public String getLogStreamName() { return null; } |
| 149 | + public String getFunctionName() { return null; } |
| 150 | + public String getFunctionVersion() { return null; } |
| 151 | + public String getInvokedFunctionArn() { return null; } |
| 152 | + public com.amazonaws.services.lambda.runtime.CognitoIdentity getIdentity() { return null; } |
| 153 | + public ClientContext getClientContext() { return null; } |
| 154 | + public int getRemainingTimeInMillis() { return 0; } |
| 155 | + public int getMemoryLimitInMB() { return 0; } |
| 156 | + public com.amazonaws.services.lambda.runtime.LambdaLogger getLogger() { return null; } |
| 157 | + }; |
| 158 | + |
| 159 | + assertTrue(bare.w3c().isEmpty()); |
| 160 | + assertFalse(bare.w3c() == null); |
| 161 | + } |
61 | 162 | } |
0 commit comments