|
19 | 19 |
|
20 | 20 | import io.swagger.v3.oas.models.OpenAPI; |
21 | 21 | import io.swagger.v3.oas.models.servers.Server; |
| 22 | +import io.swagger.v3.oas.models.servers.ServerVariable; |
| 23 | +import io.swagger.v3.oas.models.servers.ServerVariables; |
22 | 24 |
|
23 | 25 | import org.testng.Assert; |
24 | 26 | import org.testng.annotations.Test; |
25 | 27 |
|
26 | 28 | import java.net.URL; |
| 29 | +import java.util.Arrays; |
27 | 30 |
|
28 | 31 | public class URLPathUtilsTest { |
29 | 32 |
|
@@ -75,4 +78,40 @@ public void testSanitizeUrl() throws Exception { |
75 | 78 | Assert.assertEquals(URLPathUtils.getServerURL(openAPI).toString(), t[1]); |
76 | 79 | } |
77 | 80 | } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetServerURLWithVariables() throws Exception { |
| 84 | + Server s1 = new Server().url("http://localhost:{port}/").variables(new ServerVariables().addServerVariable("port", new ServerVariable()._default("8080").description("the server port"))); |
| 85 | + Assert.assertEquals(URLPathUtils.getServerURL(s1).toString(), "http://localhost:8080/"); |
| 86 | + |
| 87 | + Server s2 = new Server().url("http://{version}.test.me/{version}").variables(new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1"))); |
| 88 | + Assert.assertEquals(URLPathUtils.getServerURL(s2).toString(), "http://v1.test.me/v1"); |
| 89 | + |
| 90 | + Server s3 = new Server().url("http://localhost:{port}/{version}").variables( |
| 91 | + new ServerVariables().addServerVariable("version", new ServerVariable()._default("v4")) |
| 92 | + .addServerVariable("port", new ServerVariable()._default("8080")) |
| 93 | + .addServerVariable("other", new ServerVariable()._default("something")) |
| 94 | + ); |
| 95 | + Assert.assertEquals(URLPathUtils.getServerURL(s3).toString(), "http://localhost:8080/v4"); |
| 96 | + |
| 97 | + Server s4 = new Server().url("http://91.161.147.64/{targetEnv}").variables(new ServerVariables().addServerVariable("targetEnv", new ServerVariable().description("target environment")._enum(Arrays.asList("dev", "int", "prd"))._default("prd"))); |
| 98 | + Assert.assertEquals(URLPathUtils.getServerURL(s4).toString(), "http://91.161.147.64/prd"); |
| 99 | + |
| 100 | + Server s5 = new Server().url("https://api.stats.com/{country1}").variables(new ServerVariables().addServerVariable("country1", new ServerVariable()._enum(Arrays.asList("france", "germany", "italy")))); |
| 101 | + Assert.assertEquals(URLPathUtils.getServerURL(s5).toString(), "https://api.stats.com/france"); |
| 102 | + |
| 103 | + Server s6 = new Server().url("https://api.example.com/{wrong}"); |
| 104 | + Assert.assertEquals(URLPathUtils.getServerURL(s6).toString(), "https://api.example.com/"); |
| 105 | + |
| 106 | + Server s7 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables()); |
| 107 | + Assert.assertEquals(URLPathUtils.getServerURL(s7).toString(), "https://api.example.com/"); |
| 108 | + |
| 109 | + Server s8 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables().addServerVariable("other", new ServerVariable()._default("something"))); |
| 110 | + Assert.assertEquals(URLPathUtils.getServerURL(s8).toString(), "https://api.example.com/"); |
| 111 | + |
| 112 | + Server s9 = new Server().url("https://{user}.example.com/{version}").variables( |
| 113 | + new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1")) |
| 114 | + .addServerVariable("user", new ServerVariable()._default("{user}"))); |
| 115 | + Assert.assertEquals(URLPathUtils.getServerURL(s9).toString(), "https://{user}.example.com/v1"); |
| 116 | + } |
78 | 117 | } |
0 commit comments