@@ -39,13 +39,28 @@ describe("test proxy action",()=>{
3939 pathMatcherM . value = "" ;
4040 expectationM . matchers = [ pathMatcherM ] ;
4141 expectationM . activate = true ;
42+ expectationM . priority = 1 ;
4243 const proxyActionM = createProxyAction ( ) ;
4344 proxyActionM . host = "localhost:8085" ;
4445 expectationM . actions . push ( proxyActionM ) ;
4546
47+ const expectationCrossM = createExpectation ( ) ;
48+ const pathMatcherCrossM = createPathMatcher ( ) ;
49+ pathMatcherCrossM . conditions = MatcherCondition . START_WITH ;
50+ pathMatcherCrossM . value = "/testCross" ;
51+ expectationCrossM . matchers = [ pathMatcherCrossM ] ;
52+ expectationCrossM . activate = true ;
53+ expectationCrossM . priority = 2 ;
54+ const proxyCrossActionM = createProxyAction ( ) ;
55+ proxyCrossActionM . host = "localhost:8085" ;
56+ proxyCrossActionM . handleCross = true ;
57+ proxyCrossActionM . crossAllowCredentials = true ;
58+ proxyCrossActionM . headers ?. push ( [ "myToken" , "myTokenValue" ] )
59+ expectationCrossM . actions . push ( proxyCrossActionM ) ;
60+
4661
4762 await expectationCreation ( server , projectM , expectationM ) ;
48- const expectationMCollection = await getExpectationCollection ( projectM . id , 'test_db' ) ;
63+ await expectationCreation ( server , projectM , expectationCrossM ) ;
4964
5065 // set up the server
5166 await mockServer . start ( 8085 ) ;
@@ -71,6 +86,14 @@ describe("test proxy action",()=>{
7186 'token' :"json header" ,
7287 "Content-Type" :"application/json"
7388 } ) ;
89+ // response json
90+ await mockServer . forGet ( "/testCross" ) . thenReply ( 200 , JSON . stringify ( {
91+ name :"john" , age :20
92+ } ) , {
93+ 'token' :"json header" ,
94+ "myToken" :"origin value" ,
95+ "Content-Type" :"application/json"
96+ } ) ;
7497 await mockServer . forGet ( "/testJsonNoContentType" ) . thenReply ( 200 , JSON . stringify ( {
7598 name :"john" , age :20
7699 } ) , {
@@ -134,6 +157,30 @@ describe("test proxy action",()=>{
134157 const response = await request ( proxyServer ) . get ( "/testJsonNoContentType" ) . set ( { Accept :"application/json" } ) ;
135158 await testJsonResponse ( response , projectM ) ;
136159 } ) ;
160+
161+ test ( `test handle cross` , async ( ) => {
162+ const response = await request ( proxyServer ) . get ( "/testCross" )
163+ . set ( "Origin" , "www.google.com" )
164+ . set ( "AccessToken" , "aaa" ) . set ( "SecretName" , "bbb" ) ;
165+ await testJsonResponse ( response , projectM ) ;
166+ expect ( response . get ( "Access-Control-Allow-Origin" ) ) . toEqual ( "www.google.com" ) ;
167+ expect ( response . get ( "Access-Control-Allow-Headers" ) ) . toContain ( "accesstoken" ) ;
168+ expect ( response . get ( "Access-Control-Allow-Headers" ) ) . toContain ( "secretname" ) ;
169+ expect ( response . get ( "Access-Control-Allow-Credentials" ) ) . toContain ( "true" ) ;
170+ expect ( response . get ( "myToken" ) ) . toEqual ( "myTokenValue" ) ;
171+
172+ const optionResponse = await request ( proxyServer ) . options ( "/testCross" )
173+ . set ( "Origin" , "www.google.com" )
174+ . set ( "Access-Control-Request-Headers" , "accesstoken,secretname" )
175+ . set ( "Access-Control-Request-Methods" , "POST" ) ;
176+ expect ( optionResponse . statusCode ) . toBe ( 200 ) ;
177+ expect ( optionResponse . get ( "Access-Control-Allow-Origin" ) ) . toEqual ( "www.google.com" ) ;
178+ expect ( optionResponse . get ( "Access-Control-Allow-Headers" ) ) . toContain ( "accesstoken" ) ;
179+ expect ( optionResponse . get ( "Access-Control-Allow-Headers" ) ) . toContain ( "secretname" ) ;
180+ expect ( optionResponse . get ( "Access-Control-Allow-Credentials" ) ) . toContain ( "true" ) ;
181+ } ) ;
182+
183+
137184} ) ;
138185
139186async function getLastLog ( projectId :string , path :string ) {
0 commit comments