@@ -359,6 +359,74 @@ def test_get_server_url_with_servers_coerces_variable_argument_to_string():
359359 assert operation .get_server_url (arguments = arguments ) == expected_url
360360
361361
362+ def test_get_server_url_with_server_variable_enum_rejects_invalid_argument ():
363+ operation = RestApiOperation (
364+ id = "test" ,
365+ method = "GET" ,
366+ servers = [
367+ {
368+ "url" : "https://{region}.api.vendor.example/v1" ,
369+ "variables" : {"region" : {"default" : "us" , "enum" : ["us" , "eu" ]}},
370+ }
371+ ],
372+ path = "/resource/{id}" ,
373+ )
374+ arguments = {"region" : "external.example/" }
375+ with pytest .raises (FunctionExecutionException , match = "server variable 'region' is not one of the allowed values" ):
376+ operation .get_server_url (arguments = arguments )
377+
378+
379+ def test_get_server_url_with_server_variable_enum_allows_valid_argument ():
380+ operation = RestApiOperation (
381+ id = "test" ,
382+ method = "GET" ,
383+ servers = [
384+ {
385+ "url" : "https://{region}.api.vendor.example/v1" ,
386+ "variables" : {"region" : {"default" : "us" , "enum" : ["us" , "eu" ]}},
387+ }
388+ ],
389+ path = "/resource/{id}" ,
390+ )
391+ arguments = {"region" : "eu" }
392+ expected_url = "https://eu.api.vendor.example/v1/"
393+ assert operation .get_server_url (arguments = arguments ) == expected_url
394+
395+
396+ def test_get_server_url_with_server_variable_enum_rejects_invalid_default ():
397+ operation = RestApiOperation (
398+ id = "test" ,
399+ method = "GET" ,
400+ servers = [
401+ {
402+ "url" : "https://{region}.api.vendor.example/v1" ,
403+ "variables" : {"region" : {"default" : "external.example/" , "enum" : ["us" , "eu" ]}},
404+ }
405+ ],
406+ path = "/resource/{id}" ,
407+ )
408+ with pytest .raises (FunctionExecutionException , match = "server variable 'region' is not one of the allowed values" ):
409+ operation .get_server_url ()
410+
411+
412+ def test_get_server_url_with_server_variable_encodes_reserved_characters ():
413+ operation = RestApiOperation (
414+ id = "test" ,
415+ method = "GET" ,
416+ servers = [
417+ {
418+ "url" : "https://{region}.api.vendor.example/v1" ,
419+ "variables" : {"region" : {"default" : "us" }},
420+ }
421+ ],
422+ path = "/resource/{id}" ,
423+ )
424+ arguments = {"region" : "external.example/" }
425+ result = operation .get_server_url (arguments = arguments )
426+ assert result == "https://external.example%2F.api.vendor.example/v1/"
427+ assert urlparse (result ).hostname != "external.example"
428+
429+
362430def test_get_server_url_with_servers_and_default_variable ():
363431 operation = RestApiOperation (
364432 id = "test" ,
@@ -381,6 +449,17 @@ def test_get_server_url_with_servers_coerces_default_variable_to_string():
381449 assert operation .get_server_url () == expected_url
382450
383451
452+ def test_get_server_url_with_servers_encodes_default_variable ():
453+ operation = RestApiOperation (
454+ id = "test" ,
455+ method = "GET" ,
456+ servers = [{"url" : "https://example.com/{version}" , "variables" : {"version" : {"default" : "v1/beta" }}}],
457+ path = "/resource/{id}" ,
458+ )
459+ expected_url = "https://example.com/v1%2Fbeta/"
460+ assert operation .get_server_url () == expected_url
461+
462+
384463def test_get_server_url_with_override ():
385464 operation = RestApiOperation (
386465 id = "test" ,
0 commit comments