@@ -10,7 +10,7 @@ import {
1010 partialBreezeResponse ,
1111 successfulBreezeResponse
1212} from "../breezeTestUtils" ;
13- import { FileSystemPersist } from "../../../src/platform" ;
13+ import { FileSystemPersist , HttpSender } from "../../../src/platform" ;
1414import { TelemetryItem as Envelope } from "../../../src/generated" ;
1515import nock from "nock" ;
1616
@@ -150,9 +150,8 @@ describe("#AzureMonitorBaseExporter", () => {
150150 assert . strictEqual ( exporter [ "_retryTimer" ] , "foo" ) ;
151151 } ) ;
152152
153- it ( "should handle redirects in Azure Monitor" , async ( ) => {
153+ it ( "should handle permanent redirects in Azure Monitor" , async ( ) => {
154154 const exporter = new TestExporter ( ) ;
155-
156155 let redirectHost = "https://ukwest-0.in.applicationinsights.azure.com" ;
157156 let redirectLocation = redirectHost + "/v2/track" ;
158157 // Redirect endpoint
@@ -163,17 +162,81 @@ describe("#AzureMonitorBaseExporter", () => {
163162 scope . reply ( 308 , { } , { location : redirectLocation } ) ;
164163
165164 let result = await exporter . exportEnvelopesPrivate ( [ envelope ] ) ;
166- // Redirect triggered so telemetry must be persisted
165+ let persistedEnvelopes = ( await exporter [ "_persister" ] . shift ( ) ) as Envelope [ ] ;
166+ assert . strictEqual ( persistedEnvelopes , null ) ;
167167 assert . strictEqual ( result . code , ExportResultCode . SUCCESS ) ;
168+ assert . strictEqual (
169+ ( < HttpSender > exporter [ "_sender" ] ) [ "_appInsightsClient" ] [ "host" ] ,
170+ redirectHost
171+ ) ;
172+ } ) ;
173+
174+ it ( "should handle temporary redirects in Azure Monitor" , async ( ) => {
175+ const exporter = new TestExporter ( ) ;
176+ let redirectHost = "https://ukwest-0.in.applicationinsights.azure.com" ;
177+ let redirectLocation = redirectHost + "/v2/track" ;
178+ // Redirect endpoint
179+ const redirectScope = nock ( redirectHost ) . post ( "/v2/track" , ( ) => {
180+ return true ;
181+ } ) ;
182+ redirectScope . reply ( 200 , JSON . stringify ( successfulBreezeResponse ( 1 ) ) ) ;
183+ scope . reply ( 307 , { } , { location : redirectLocation } ) ;
184+
185+ let result = await exporter . exportEnvelopesPrivate ( [ envelope ] ) ;
168186 let persistedEnvelopes = ( await exporter [ "_persister" ] . shift ( ) ) as Envelope [ ] ;
169- assert . strictEqual ( persistedEnvelopes ?. length , 1 ) ;
170- assert . deepStrictEqual ( persistedEnvelopes [ 0 ] , toObject ( envelope ) ) ;
171- assert . strictEqual ( exporter [ "_numConsecutiveRedirects" ] , 1 ) ;
172- // After redirect return 200
187+ assert . strictEqual ( persistedEnvelopes , null ) ;
188+ assert . strictEqual ( result . code , ExportResultCode . SUCCESS ) ;
189+ assert . strictEqual (
190+ ( < HttpSender > exporter [ "_sender" ] ) [ "_appInsightsClient" ] [ "host" ] ,
191+ redirectHost
192+ ) ;
193+ } ) ;
194+
195+ it ( "should use redirect URL for following requests" , async ( ) => {
196+ const exporter = new TestExporter ( ) ;
197+ let redirectHost = "https://ukwest-0.in.applicationinsights.azure.com" ;
198+ let redirectLocation = redirectHost + "/v2/track" ;
199+ // Redirect endpoint
200+ const redirectScope = nock ( redirectHost ) . post ( "/v2/track" , ( ) => {
201+ return true ;
202+ } ) ;
203+ redirectScope . twice ( ) . reply ( 200 , JSON . stringify ( successfulBreezeResponse ( 1 ) ) ) ;
204+ scope . reply ( 307 , { } , { location : redirectLocation } ) ;
205+ let result = await exporter . exportEnvelopesPrivate ( [ envelope ] ) ;
206+ assert . strictEqual ( result . code , ExportResultCode . SUCCESS ) ;
207+ assert . strictEqual (
208+ ( < HttpSender > exporter [ "_sender" ] ) [ "_appInsightsClient" ] [ "host" ] ,
209+ redirectHost
210+ ) ;
173211 result = await exporter . exportEnvelopesPrivate ( [ envelope ] ) ;
174212 assert . strictEqual ( result . code , ExportResultCode . SUCCESS ) ;
175- persistedEnvelopes = ( await exporter [ "_persister" ] . shift ( ) ) as Envelope [ ] ;
176- assert . strictEqual ( persistedEnvelopes , null ) ;
213+ assert . strictEqual (
214+ ( < HttpSender > exporter [ "_sender" ] ) [ "_appInsightsClient" ] [ "host" ] ,
215+ redirectHost
216+ ) ;
217+ } ) ;
218+
219+ it ( "should stop redirecting when circular redirect is triggered" , async ( ) => {
220+ const exporter = new TestExporter ( ) ;
221+ let redirectHost = "https://ukwest-0.in.applicationinsights.azure.com" ;
222+ let redirectLocation = redirectHost + "/v2/track" ;
223+ // Redirect endpoint
224+ const redirectScope = nock ( redirectHost ) . post ( "/v2/track" , ( ) => {
225+ return true ;
226+ } ) ;
227+ // Circle redirect
228+ scope
229+ . reply ( 307 , JSON . stringify ( successfulBreezeResponse ( 1 ) ) , { location : redirectLocation } )
230+ . persist ( ) ;
231+ redirectScope
232+ . reply ( 307 , JSON . stringify ( successfulBreezeResponse ( 1 ) ) , {
233+ location : DEFAULT_BREEZE_ENDPOINT
234+ } )
235+ . persist ( ) ;
236+
237+ let result = await exporter . exportEnvelopesPrivate ( [ envelope ] ) ;
238+ assert . strictEqual ( result . code , ExportResultCode . FAILED ) ;
239+ assert . strictEqual ( result . error ?. message , "Circular redirect" ) ;
177240 } ) ;
178241 } ) ;
179242 } ) ;
0 commit comments