Skip to content

Commit 14bdcc1

Browse files
committed
improve coverage
1 parent 50eca11 commit 14bdcc1

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

internal-api/src/test/java/datadog/trace/api/gateway/InferredProxySpanTests.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,108 @@ void testStoreAndFromContext() {
9292

9393
assertNull(fromContext(root()), "fromContext on empty context should be null");
9494
}
95+
96+
@Test
97+
@DisplayName("Invalid start time should return extracted context")
98+
void testInvalidStartTime() {
99+
Map<String, String> headers = new HashMap<>();
100+
headers.put(PROXY_START_TIME_MS, "invalid-number");
101+
headers.put(PROXY_SYSTEM, "aws-apigateway");
102+
103+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
104+
assertTrue(inferredProxySpan.isValid());
105+
assertNull(inferredProxySpan.start(null), "Invalid start time should return null");
106+
}
107+
108+
@Test
109+
@DisplayName("Service name should fallback to config when domain name is null")
110+
void testServiceNameFallbackNull() {
111+
Map<String, String> headers = new HashMap<>();
112+
headers.put(PROXY_START_TIME_MS, "12345");
113+
headers.put(PROXY_SYSTEM, "aws-apigateway");
114+
headers.put(InferredProxySpan.PROXY_HTTP_METHOD, "GET");
115+
headers.put(InferredProxySpan.PROXY_PATH, "/test");
116+
117+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
118+
assertNotNull(inferredProxySpan.start(null));
119+
// Service name should use Config.get().getServiceName() when domain name is null
120+
}
121+
122+
@Test
123+
@DisplayName("Service name should fallback to config when domain name is empty")
124+
void testServiceNameFallbackEmpty() {
125+
Map<String, String> headers = new HashMap<>();
126+
headers.put(PROXY_START_TIME_MS, "12345");
127+
headers.put(PROXY_SYSTEM, "aws-apigateway");
128+
headers.put(InferredProxySpan.PROXY_DOMAIN_NAME, "");
129+
headers.put(InferredProxySpan.PROXY_HTTP_METHOD, "GET");
130+
headers.put(InferredProxySpan.PROXY_PATH, "/test");
131+
132+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
133+
assertNotNull(inferredProxySpan.start(null));
134+
// Service name should use Config.get().getServiceName() when domain name is empty
135+
}
136+
137+
@Test
138+
@DisplayName("HTTP URL should use path only when domain name is null")
139+
void testHttpUrlWithoutDomain() {
140+
Map<String, String> headers = new HashMap<>();
141+
headers.put(PROXY_START_TIME_MS, "12345");
142+
headers.put(PROXY_SYSTEM, "aws-apigateway");
143+
headers.put(InferredProxySpan.PROXY_HTTP_METHOD, "GET");
144+
headers.put(InferredProxySpan.PROXY_PATH, "/test");
145+
146+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
147+
assertNotNull(inferredProxySpan.start(null));
148+
// HTTP URL should be just the path when domain name is null
149+
}
150+
151+
@Test
152+
@DisplayName("Resource name should be null when httpMethod is null")
153+
void testResourceNameNullMethod() {
154+
Map<String, String> headers = new HashMap<>();
155+
headers.put(PROXY_START_TIME_MS, "12345");
156+
headers.put(PROXY_SYSTEM, "aws-apigateway");
157+
headers.put(InferredProxySpan.PROXY_PATH, "/test");
158+
159+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
160+
assertNotNull(inferredProxySpan.start(null));
161+
// Resource name should be null when httpMethod is null
162+
}
163+
164+
@Test
165+
@DisplayName("Resource name should be null when path is null")
166+
void testResourceNameNullPath() {
167+
Map<String, String> headers = new HashMap<>();
168+
headers.put(PROXY_START_TIME_MS, "12345");
169+
headers.put(PROXY_SYSTEM, "aws-apigateway");
170+
headers.put(InferredProxySpan.PROXY_HTTP_METHOD, "GET");
171+
172+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
173+
assertNotNull(inferredProxySpan.start(null));
174+
// Resource name should be null when path is null
175+
}
176+
177+
@Test
178+
@DisplayName("Finish should handle null span gracefully")
179+
void testFinishWithNullSpan() {
180+
InferredProxySpan inferredProxySpan = fromHeaders(null);
181+
// Should not throw exception when span is null
182+
inferredProxySpan.finish();
183+
assertFalse(inferredProxySpan.isValid());
184+
}
185+
186+
@Test
187+
@DisplayName("Finish should clear span after finishing")
188+
void testFinishClearsSpan() {
189+
Map<String, String> headers = new HashMap<>();
190+
headers.put(PROXY_START_TIME_MS, "12345");
191+
headers.put(PROXY_SYSTEM, "aws-apigateway");
192+
193+
InferredProxySpan inferredProxySpan = fromHeaders(headers);
194+
assertNotNull(inferredProxySpan.start(null));
195+
inferredProxySpan.finish();
196+
// Span should be cleared after finish, so calling finish again should be safe
197+
inferredProxySpan.finish();
198+
}
95199
}

0 commit comments

Comments
 (0)