|
| 1 | +import{UriTemplate}from"./uriTemplate.js"; |
| 2 | + |
| 3 | +describe("UriTemplate",()=>{ |
| 4 | +describe("simple string expansion",()=>{ |
| 5 | +it("should expand simple string variables",()=>{ |
| 6 | +consttemplate=newUriTemplate("http://example.com/users/{username}"); |
| 7 | +expect(template.expand({username: "fred"})).toBe( |
| 8 | +"http://example.com/users/fred", |
| 9 | +); |
| 10 | +}); |
| 11 | + |
| 12 | +it("should handle multiple variables",()=>{ |
| 13 | +consttemplate=newUriTemplate("{x,y}"); |
| 14 | +expect(template.expand({x: "1024",y: "768"})).toBe("1024,768"); |
| 15 | +}); |
| 16 | + |
| 17 | +it("should encode reserved characters",()=>{ |
| 18 | +consttemplate=newUriTemplate("{var}"); |
| 19 | +expect(template.expand({var: "value with spaces"})).toBe( |
| 20 | +"value+with+spaces", |
| 21 | +); |
| 22 | +}); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("reserved expansion",()=>{ |
| 26 | +it("should not encode reserved characters with + operator",()=>{ |
| 27 | +consttemplate=newUriTemplate("{+path}/here"); |
| 28 | +expect(template.expand({path: "/foo/bar"})).toBe("/foo/bar/here"); |
| 29 | +}); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("fragment expansion",()=>{ |
| 33 | +it("should add # prefix and not encode reserved chars",()=>{ |
| 34 | +consttemplate=newUriTemplate("X{#var}"); |
| 35 | +expect(template.expand({var: "/test"})).toBe("X#/test"); |
| 36 | +}); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("label expansion",()=>{ |
| 40 | +it("should add . prefix",()=>{ |
| 41 | +consttemplate=newUriTemplate("X{.var}"); |
| 42 | +expect(template.expand({var: "test"})).toBe("X.test"); |
| 43 | +}); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("path expansion",()=>{ |
| 47 | +it("should add / prefix",()=>{ |
| 48 | +consttemplate=newUriTemplate("X{/var}"); |
| 49 | +expect(template.expand({var: "test"})).toBe("X/test"); |
| 50 | +}); |
| 51 | +}); |
| 52 | + |
| 53 | +describe("query expansion",()=>{ |
| 54 | +it("should add ? prefix and name=value format",()=>{ |
| 55 | +consttemplate=newUriTemplate("X{?var}"); |
| 56 | +expect(template.expand({var: "test"})).toBe("X?var=test"); |
| 57 | +}); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("form continuation expansion",()=>{ |
| 61 | +it("should add & prefix and name=value format",()=>{ |
| 62 | +consttemplate=newUriTemplate("X{&var}"); |
| 63 | +expect(template.expand({var: "test"})).toBe("X&var=test"); |
| 64 | +}); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("matching",()=>{ |
| 68 | +it("should match simple strings and extract variables",()=>{ |
| 69 | +consttemplate=newUriTemplate("http://example.com/users/{username}"); |
| 70 | +constmatch=template.match("http://example.com/users/fred"); |
| 71 | +expect(match).toEqual({username: "fred"}); |
| 72 | +}); |
| 73 | + |
| 74 | +it("should match multiple variables",()=>{ |
| 75 | +consttemplate=newUriTemplate("/users/{username}/posts/{postId}"); |
| 76 | +constmatch=template.match("/users/fred/posts/123"); |
| 77 | +expect(match).toEqual({username: "fred",postId: "123"}); |
| 78 | +}); |
| 79 | + |
| 80 | +it("should return null for non-matching URIs",()=>{ |
| 81 | +consttemplate=newUriTemplate("/users/{username}"); |
| 82 | +constmatch=template.match("/posts/123"); |
| 83 | +expect(match).toBeNull(); |
| 84 | +}); |
| 85 | + |
| 86 | +it("should handle exploded arrays",()=>{ |
| 87 | +consttemplate=newUriTemplate("{/list*}"); |
| 88 | +constmatch=template.match("/red,green,blue"); |
| 89 | +expect(match).toEqual({list: ["red","green","blue"]}); |
| 90 | +}); |
| 91 | +}); |
| 92 | + |
| 93 | +describe("edge cases",()=>{ |
| 94 | +it("should handle empty variables",()=>{ |
| 95 | +consttemplate=newUriTemplate("{empty}"); |
| 96 | +expect(template.expand({})).toBe(""); |
| 97 | +expect(template.expand({empty: ""})).toBe(""); |
| 98 | +}); |
| 99 | + |
| 100 | +it("should handle undefined variables",()=>{ |
| 101 | +consttemplate=newUriTemplate("{a}{b}{c}"); |
| 102 | +expect(template.expand({b: "2"})).toBe("2"); |
| 103 | +}); |
| 104 | + |
| 105 | +it("should handle special characters in variable names",()=>{ |
| 106 | +consttemplate=newUriTemplate("{$var_name}"); |
| 107 | +expect(template.expand({"$var_name": "value"})).toBe("value"); |
| 108 | +}); |
| 109 | +}); |
| 110 | + |
| 111 | +describe("complex patterns",()=>{ |
| 112 | +it("should handle nested path segments",()=>{ |
| 113 | +consttemplate=newUriTemplate("/api/{version}/{resource}/{id}"); |
| 114 | +expect(template.expand({ |
| 115 | +version: "v1", |
| 116 | +resource: "users", |
| 117 | +id: "123" |
| 118 | +})).toBe("/api/v1/users/123"); |
| 119 | +}); |
| 120 | + |
| 121 | +it("should handle query parameters with arrays",()=>{ |
| 122 | +consttemplate=newUriTemplate("/search{?tags*}"); |
| 123 | +expect(template.expand({ |
| 124 | +tags: ["nodejs","typescript","testing"] |
| 125 | +})).toBe("/search?tags=nodejs,typescript,testing"); |
| 126 | +}); |
| 127 | + |
| 128 | +it("should handle multiple query parameters",()=>{ |
| 129 | +consttemplate=newUriTemplate("/search{?q,page,limit}"); |
| 130 | +expect(template.expand({ |
| 131 | +q: "test", |
| 132 | +page: "1", |
| 133 | +limit: "10" |
| 134 | +})).toBe("/search?q=test&page=1&limit=10"); |
| 135 | +}); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("matching complex patterns",()=>{ |
| 139 | +it("should match nested path segments",()=>{ |
| 140 | +consttemplate=newUriTemplate("/api/{version}/{resource}/{id}"); |
| 141 | +constmatch=template.match("/api/v1/users/123"); |
| 142 | +expect(match).toEqual({ |
| 143 | +version: "v1", |
| 144 | +resource: "users", |
| 145 | +id: "123" |
| 146 | +}); |
| 147 | +}); |
| 148 | + |
| 149 | +it("should match query parameters",()=>{ |
| 150 | +consttemplate=newUriTemplate("/search{?q}"); |
| 151 | +constmatch=template.match("/search?q=test"); |
| 152 | +expect(match).toEqual({q: "test"}); |
| 153 | +}); |
| 154 | + |
| 155 | +it("should match multiple query parameters",()=>{ |
| 156 | +consttemplate=newUriTemplate("/search{?q,page}"); |
| 157 | +constmatch=template.match("/search?q=test&page=1"); |
| 158 | +expect(match).toEqual({q: "test",page: "1"}); |
| 159 | +}); |
| 160 | + |
| 161 | +it("should handle partial matches correctly",()=>{ |
| 162 | +consttemplate=newUriTemplate("/users/{id}"); |
| 163 | +expect(template.match("/users/123/extra")).toBeNull(); |
| 164 | +expect(template.match("/users")).toBeNull(); |
| 165 | +}); |
| 166 | +}); |
| 167 | +}); |
0 commit comments