|
| 1 | +package io.swagger; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | + |
| 5 | +import io.swagger.jaxrs.utils.PathUtils; |
| 6 | + |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class PathUtilsTest { |
| 13 | + |
| 14 | + @Test(description = "parse regex with slash inside it from issue 1153") |
| 15 | + public void parseRegexWithSlashInside() { |
| 16 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 17 | + final String path = PathUtils.parsePath("/{itemId: [0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{3}/[A-Za-z0-9]+}", regexMap); |
| 18 | + assertEquals(path, "/{itemId}"); |
| 19 | + assertEquals(regexMap.get("itemId"), "[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{3}/[A-Za-z0-9]+"); |
| 20 | + } |
| 21 | + |
| 22 | + @Test(description = "parse two part path with one param") |
| 23 | + public void parseTwoPartPathWithOneParam() { |
| 24 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 25 | + final String path = PathUtils.parsePath("/api/{itemId: [0-9]{4}/[0-9]{2,4}/[A-Za-z0-9]+}", regexMap); |
| 26 | + assertEquals(path, "/api/{itemId}"); |
| 27 | + assertEquals(regexMap.get("itemId"), "[0-9]{4}/[0-9]{2,4}/[A-Za-z0-9]+"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test(description = "parse two part path with two params and white spaces around") |
| 31 | + public void parseTwoPartPathWithTwoParams() { |
| 32 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 33 | + final String path = PathUtils.parsePath("/{itemId: [0-9]{4}/[A-Za-z0-9]+}/{ api : [aA-zZ]+ }", regexMap); |
| 34 | + assertEquals(path, "/{itemId}/{api}"); |
| 35 | + assertEquals(regexMap.get("itemId"), "[0-9]{4}/[A-Za-z0-9]+"); |
| 36 | + assertEquals(regexMap.get("api"), "[aA-zZ]+"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test(description = "parse simple path") |
| 40 | + public void parseSimplePath() { |
| 41 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 42 | + final String path = PathUtils.parsePath("/api/itemId", regexMap); |
| 43 | + assertEquals(path, "/api/itemId"); |
| 44 | + assertEquals(regexMap.size(), 0); |
| 45 | + } |
| 46 | + |
| 47 | + @Test(description = "parse path with param without regex") |
| 48 | + public void parsePathWithoutRegex() { |
| 49 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 50 | + final String path = PathUtils.parsePath("/api/{name}", regexMap); |
| 51 | + assertEquals(path, "/api/{name}"); |
| 52 | + assertEquals(regexMap.size(), 0); |
| 53 | + } |
| 54 | + |
| 55 | + @Test(description = "parse path with two params in one part") |
| 56 | + public void parsePathWithTwoParamsInOnePart() { |
| 57 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 58 | + final String path = PathUtils.parsePath("/{a:\\w+}-{b:\\w+}/c", regexMap); |
| 59 | + assertEquals(path, "/{a}-{b}/c"); |
| 60 | + assertEquals(regexMap.get("a"), "\\w+"); |
| 61 | + assertEquals(regexMap.get("b"), "\\w+"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test(description = "parse path like /swagger.{json|yaml}") |
| 65 | + public void test() { |
| 66 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 67 | + final String path = PathUtils.parsePath("/swagger.{json|yaml}", regexMap); |
| 68 | + assertEquals(path, "/swagger.{json|yaml}"); |
| 69 | + assertEquals(regexMap.size(), 0); |
| 70 | + } |
| 71 | + |
| 72 | + @Test(description = "parse path with many braces and slashes iside") |
| 73 | + public void parsePathWithBracesAndSlashes() { |
| 74 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 75 | + final String path = PathUtils.parsePath("/api/{regex:/(?!\\{\\})\\w*|/\\{\\w+:*([^\\{\\}]*(\\{.*\\})*)*\\}}", regexMap); |
| 76 | + assertEquals(path, "/api/{regex}"); |
| 77 | + assertEquals(regexMap.get("regex"), "/(?!\\{\\})\\w*|/\\{\\w+:*([^\\{\\}]*(\\{.*\\})*)*\\}"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(description = "not fail when passed path is null") |
| 81 | + public void testNullPath() { |
| 82 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 83 | + final String path = PathUtils.parsePath(null, regexMap); |
| 84 | + assertEquals(path, null); |
| 85 | + } |
| 86 | + |
| 87 | + @Test(description = "not fail when regex is not valid") |
| 88 | + public void testInvalidRegex() { |
| 89 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 90 | + final String path = PathUtils.parsePath("/api/{fail: [a-z]", regexMap); |
| 91 | + assertEquals(path, null); |
| 92 | + } |
| 93 | + |
| 94 | + @Test(description = "not fail when passed path is empty") |
| 95 | + public void testEmptyPath() { |
| 96 | + final Map<String, String> regexMap = new HashMap<String, String>(); |
| 97 | + final String path = PathUtils.parsePath("", regexMap); |
| 98 | + assertEquals(path, "/"); |
| 99 | + } |
| 100 | +} |
0 commit comments