@@ -256,6 +256,30 @@ def test_event_source_unsupported(self):
256256 self .assertEqual (event_source .to_string (), "unknown" )
257257 self .assertEqual (event_source_arn , None )
258258
259+ def test_event_source_with_non_dict_request_context (self ):
260+ # Test with requestContext as a string instead of a dict
261+ event = {"requestContext" : "not_a_dict" }
262+ event_source = parse_event_source (event )
263+ # Should still return a valid event source (unknown in this case)
264+ self .assertEqual (event_source .to_string (), "unknown" )
265+
266+ def test_event_source_with_invalid_domain_name (self ):
267+ # Test with domainName that isn't a string
268+ event = {"requestContext" : {"stage" : "prod" , "domainName" : 12345 }}
269+ event_source = parse_event_source (event )
270+ # Should detect as API Gateway since stage is present
271+ self .assertEqual (event_source .to_string (), "api-gateway" )
272+
273+ def test_detect_lambda_function_url_domain_with_invalid_input (self ):
274+ from datadog_lambda .trigger import detect_lambda_function_url_domain
275+
276+ # Test with non-string input
277+ self .assertFalse (detect_lambda_function_url_domain (None ))
278+ self .assertFalse (detect_lambda_function_url_domain (12345 ))
279+ self .assertFalse (detect_lambda_function_url_domain ({"not" : "a-string" }))
280+ # Test with string that would normally cause an exception when split
281+ self .assertFalse (detect_lambda_function_url_domain ("" ))
282+
259283
260284class GetTriggerTags (unittest .TestCase ):
261285 def test_extract_trigger_tags_api_gateway (self ):
@@ -530,6 +554,47 @@ def test_extract_trigger_tags_list_type_event(self):
530554 tags = extract_trigger_tags (event , ctx )
531555 self .assertEqual (tags , {})
532556
557+ def test_extract_http_tags_with_invalid_request_context (self ):
558+ from datadog_lambda .trigger import extract_http_tags
559+
560+ # Test with requestContext as a string instead of a dict
561+ event = {"requestContext" : "not_a_dict" , "path" : "/test" , "httpMethod" : "GET" }
562+ http_tags = extract_http_tags (event )
563+ # Should still extract valid tags from the event
564+ self .assertEqual (
565+ http_tags , {"http.url_details.path" : "/test" , "http.method" : "GET" }
566+ )
567+
568+ def test_extract_http_tags_with_invalid_apigateway_http (self ):
569+ from datadog_lambda .trigger import extract_http_tags
570+
571+ # Test with http in requestContext that's not a dict
572+ event = {
573+ "requestContext" : {"stage" : "prod" , "http" : "not_a_dict" },
574+ "version" : "2.0" ,
575+ }
576+ http_tags = extract_http_tags (event )
577+ # Should not raise an exception
578+ self .assertEqual (http_tags , {})
579+
580+ def test_extract_http_tags_with_invalid_headers (self ):
581+ from datadog_lambda .trigger import extract_http_tags
582+
583+ # Test with headers that's not a dict
584+ event = {"headers" : "not_a_dict" }
585+ http_tags = extract_http_tags (event )
586+ # Should not raise an exception
587+ self .assertEqual (http_tags , {})
588+
589+ def test_extract_http_tags_with_invalid_route (self ):
590+ from datadog_lambda .trigger import extract_http_tags
591+
592+ # Test with routeKey that would cause a split error
593+ event = {"routeKey" : 12345 } # Not a string
594+ http_tags = extract_http_tags (event )
595+ # Should not raise an exception
596+ self .assertEqual (http_tags , {})
597+
533598
534599class ExtractHTTPStatusCodeTag (unittest .TestCase ):
535600 def test_extract_http_status_code_tag_from_response_dict (self ):
0 commit comments