@@ -17,11 +17,11 @@ const host = "test.com";
1717const base = `https://${ host } ` ;
1818const POST = "POST" ;
1919
20- const getResBodyTypes = ( store : RequestStore , host : string , path : string , propName = 'foo' ) => {
20+ const getResBodyJSONTypes = ( store : RequestStore , host : string , path : string , propName = 'foo' ) => {
2121 const match = store . get ( ) [ host ] . lookup ( path ) ;
2222 if ( ! match ) throw new Error ( "Could not match path" ) ;
2323 const properties =
24- match . data . methods [ POST ] [ 200 ] . responseBody ?. properties ?. [ propName ] . type ;
24+ match . data . methods [ POST ] [ 200 ] . response [ 'application/json' ] . body ?. properties ?. [ propName ] . type ;
2525 return properties ;
2626} ;
2727
@@ -35,7 +35,7 @@ it("parameterises and merges paths", () => {
3535 store . parameterise ( 2 , "/1/2/a" , host ) ;
3636 store . insert ( req3 , { foo : null } ) ;
3737 store . parameterise ( 1 , "/1/2/:param2" , host ) ;
38- const properties = getResBodyTypes ( store , host , "/1/zzz/asbds" ) ;
38+ const properties = getResBodyJSONTypes ( store , host , "/1/zzz/asbds" ) ;
3939 expect ( properties ) . toContain ( "string" ) ;
4040 expect ( properties ) . toContain ( "integer" ) ;
4141 expect ( properties ) . toContain ( "null" ) ;
@@ -46,7 +46,7 @@ it("inserts data and can retrieve it", () => {
4646 const store = new RequestStore ( ) ;
4747 const req = createSimpleRequest ( `${ base } /1/2/a` ) ;
4848 store . insert ( req , { foo : 1 } ) ;
49- const properties = getResBodyTypes ( store , host , "/1/2/a" ) ;
49+ const properties = getResBodyJSONTypes ( store , host , "/1/2/a" ) ;
5050 expect ( properties ) . toBe ( "integer" ) ;
5151} ) ;
5252
@@ -77,9 +77,9 @@ it("sets leafMap correctly after multiple add and parameterise operations", () =
7777 } ;
7878 // @ts -expect-error accessing private property
7979 expect ( store . leafMap ) . toEqual ( expected ) ;
80- expect ( getResBodyTypes ( store , host , "/1/x/x" ) ) . toEqual ( [ "null" , "integer" , "string" ] ) ;
81- expect ( getResBodyTypes ( store , host , "/dynamicPath/2/x" ) ) . toEqual ( [ "integer" , "string" ] ) ;
82- expect ( getResBodyTypes ( store , host , "/staticPath/2/3/4/5" ) ) . toBe ( 'string' ) ;
80+ expect ( getResBodyJSONTypes ( store , host , "/1/x/x" ) ) . toEqual ( [ "null" , "integer" , "string" ] ) ;
81+ expect ( getResBodyJSONTypes ( store , host , "/dynamicPath/2/x" ) ) . toEqual ( [ "integer" , "string" ] ) ;
82+ expect ( getResBodyJSONTypes ( store , host , "/staticPath/2/3/4/5" ) ) . toBe ( 'string' ) ;
8383} ) ;
8484
8585it ( "sets leafMap correctly after many parameterise operations" , ( ) => {
@@ -104,9 +104,9 @@ it("sets leafMap correctly after many parameterise operations", () => {
104104 } ;
105105 // @ts -expect-error accessing private property
106106 expect ( store . leafMap ) . toEqual ( expected ) ;
107- expect ( getResBodyTypes ( store , host , "/1/2/3/ANY/ANY" ) ) . toEqual ( [ "null" , "string" ] ) ;
108- expect ( getResBodyTypes ( store , host , "/1/2/b" ) ) . toBe ( "boolean" ) ;
109- expect ( getResBodyTypes ( store , host , "/1/x/y/ANY/b" ) ) . toBe ( 'integer' ) ;
107+ expect ( getResBodyJSONTypes ( store , host , "/1/2/3/ANY/ANY" ) ) . toEqual ( [ "null" , "string" ] ) ;
108+ expect ( getResBodyJSONTypes ( store , host , "/1/2/b" ) ) . toBe ( "boolean" ) ;
109+ expect ( getResBodyJSONTypes ( store , host , "/1/x/y/ANY/b" ) ) . toBe ( 'integer' ) ;
110110} ) ;
111111
112112it ( "collapses into a single route when paramaterised" , ( ) => {
@@ -126,7 +126,7 @@ it("collapses into a single route when paramaterised", () => {
126126 } ;
127127 // @ts -expect-error accessing private property
128128 expect ( store . leafMap ) . toEqual ( expected ) ;
129- expect ( getResBodyTypes ( store , host , "/1/2/3/ANY/ANY" ) ) . toEqual ( [ "null" , "integer" , "string" ] ) ;
129+ expect ( getResBodyJSONTypes ( store , host , "/1/2/3/ANY/ANY" ) ) . toEqual ( [ "null" , "integer" , "string" ] ) ;
130130} ) ;
131131
132132it ( "can parameterise paths that are subsets of another path" , ( ) => {
@@ -144,19 +144,22 @@ it("can parameterise paths that are subsets of another path", () => {
144144 } ;
145145 // @ts -expect-error accessing private property
146146 expect ( store . leafMap ) . toEqual ( expected ) ;
147- expect ( getResBodyTypes ( store , host , "/1/2/a" ) ) . toBe ( "string" ) ;
148- expect ( getResBodyTypes ( store , host , "/1/ANY" ) ) . toBe ( "integer" ) ;
147+ expect ( getResBodyJSONTypes ( store , host , "/1/2/a" ) ) . toBe ( "string" ) ;
148+ expect ( getResBodyJSONTypes ( store , host , "/1/ANY" ) ) . toBe ( "integer" ) ;
149149} ) ;
150150
151151it ( "can parameterise paths that exist along the same segment" , ( ) => {
152152 const store = new RequestStore ( ) ;
153153 const req1 = createSimpleRequest ( `${ base } /1/2/a` ) ;
154154 const req2 = createSimpleRequest ( `${ base } /1/2` ) ;
155- store . insert ( createSimpleRequest ( `${ base } /1` ) , { foo : null } ) ;
156- store . insert ( createSimpleRequest ( `${ base } /1/2/3/4` ) , { foo : null } ) ;
155+ const req3 = createSimpleRequest ( `${ base } /1` ) ;
156+ const req4 = createSimpleRequest ( `${ base } /1/2/3/4` ) ;
157157 store . insert ( req1 , { foo : "bar" } ) ;
158158 store . insert ( req2 , { foo : 1 } ) ;
159+ store . insert ( req3 , { foo : null } ) ;
160+ store . insert ( req4 , { foo : null } ) ;
159161 store . parameterise ( 1 , "/1/2/a" , host ) ;
162+ // Bug happens below. When /1/2 is parameterised, router.remove removes /1/2/3/4
160163 store . parameterise ( 1 , "/1/2" , host ) ;
161164 const expected = {
162165 [ host ] : {
@@ -168,8 +171,27 @@ it("can parameterise paths that exist along the same segment", () => {
168171 } ;
169172 // @ts -expect-error accessing private property
170173 expect ( store . leafMap ) . toEqual ( expected ) ;
171- expect ( getResBodyTypes ( store , host , "/1/ANY/a" ) ) . toBe ( "string" ) ;
172- expect ( getResBodyTypes ( store , host , "/1/ANY" ) ) . toBe ( "integer" ) ;
174+ expect ( getResBodyJSONTypes ( store , host , "/1/ANY/a" ) ) . toBe ( "string" ) ;
175+ expect ( getResBodyJSONTypes ( store , host , "/1/ANY" ) ) . toBe ( "integer" ) ;
176+ } ) ;
177+
178+ it ( "parameterising a path catches future requests to the same path" , ( ) => {
179+ const store = new RequestStore ( ) ;
180+ const req1 = createSimpleRequest ( `${ base } /1/2/a` ) ;
181+ const req2 = createSimpleRequest ( `${ base } /1/2/b` ) ;
182+ store . insert ( req1 , { foo : "bar" } ) ;
183+ store . insert ( req2 , { foo : "bar" } ) ;
184+ store . parameterise ( 1 , "/1/2/a" , host ) ;
185+ store . insert ( req1 , { foo : 1 } ) ;
186+ store . insert ( req2 , { foo : 1 } ) ;
187+ const expected = {
188+ [ host ] : {
189+ '/1/:param1/a' : expect . any ( Object ) ,
190+ '/1/2/b' : expect . any ( Object ) ,
191+ }
192+ } ;
193+ // @ts -expect-error accessing private property
194+ expect ( store . leafMap ) . toEqual ( expected ) ;
173195} ) ;
174196
175197it ( "parameterisation works after export and import" , ( ) => {
0 commit comments