@@ -3,16 +3,80 @@ import { registerCleanupTask, mockClock, replaceMockable } from '@datadog/browse
33import type { Clock } from '@datadog/browser-core/test'
44import { getProbes , clearProbes } from './probes'
55import type { Probe } from './probes'
6- import { startDeliveryApiPolling , stopDeliveryApiPolling , clearDeliveryApiState } from './deliveryApi'
6+ import {
7+ buildDeliveryApiUrl ,
8+ startDeliveryApiPolling ,
9+ stopDeliveryApiPolling ,
10+ clearDeliveryApiState ,
11+ } from './deliveryApi'
712import type { DeliveryApiConfiguration } from './deliveryApi'
813
14+ describe ( 'buildDeliveryApiUrl' , ( ) => {
15+ it ( 'should default to datadoghq.com' , ( ) => {
16+ expect ( buildDeliveryApiUrl ( ) ) . toBe ( 'https://api.datadoghq.com/api/unstable/debugger/frontend/probes' )
17+ } )
18+
19+ it ( 'should build URL for US1 site' , ( ) => {
20+ expect ( buildDeliveryApiUrl ( 'datadoghq.com' ) ) . toBe ( 'https://api.datadoghq.com/api/unstable/debugger/frontend/probes' )
21+ } )
22+
23+ it ( 'should build URL for EU1 site' , ( ) => {
24+ expect ( buildDeliveryApiUrl ( 'datadoghq.eu' ) ) . toBe ( 'https://api.datadoghq.eu/api/unstable/debugger/frontend/probes' )
25+ } )
26+
27+ it ( 'should build URL for US3 site' , ( ) => {
28+ expect ( buildDeliveryApiUrl ( 'us3.datadoghq.com' ) ) . toBe (
29+ 'https://api.us3.datadoghq.com/api/unstable/debugger/frontend/probes'
30+ )
31+ } )
32+
33+ it ( 'should build URL for staging site' , ( ) => {
34+ expect ( buildDeliveryApiUrl ( 'datad0g.com' ) ) . toBe ( 'https://api.datad0g.com/api/unstable/debugger/frontend/probes' )
35+ } )
36+
37+ it ( 'should build URL for gov site' , ( ) => {
38+ expect ( buildDeliveryApiUrl ( 'ddog-gov.com' ) ) . toBe ( 'https://api.ddog-gov.com/api/unstable/debugger/frontend/probes' )
39+ } )
40+
41+ it ( 'should use proxy as origin when provided' , ( ) => {
42+ expect ( buildDeliveryApiUrl ( 'datadoghq.com' , 'http://localhost:9000' ) ) . toBe (
43+ 'http://localhost:9000/api/unstable/debugger/frontend/probes'
44+ )
45+ } )
46+
47+ it ( 'should ignore site when proxy is provided' , ( ) => {
48+ expect ( buildDeliveryApiUrl ( 'datadoghq.eu' , 'http://proxy.example.com' ) ) . toBe (
49+ 'http://proxy.example.com/api/unstable/debugger/frontend/probes'
50+ )
51+ } )
52+
53+ it ( 'should trim a trailing slash from a proxy origin to avoid a double-slash path' , ( ) => {
54+ expect ( buildDeliveryApiUrl ( 'datadoghq.com' , 'https://proxy.example.com/' ) ) . toBe (
55+ 'https://proxy.example.com/api/unstable/debugger/frontend/probes'
56+ )
57+ } )
58+
59+ it ( 'should trim a trailing slash from a proxy that has a sub-path' , ( ) => {
60+ expect ( buildDeliveryApiUrl ( 'datadoghq.com' , 'https://proxy.example.com/dd/' ) ) . toBe (
61+ 'https://proxy.example.com/dd/api/unstable/debugger/frontend/probes'
62+ )
63+ } )
64+
65+ it ( 'should preserve a proxy sub-path that has no trailing slash' , ( ) => {
66+ expect ( buildDeliveryApiUrl ( 'datadoghq.com' , 'https://proxy.example.com/dd' ) ) . toBe (
67+ 'https://proxy.example.com/dd/api/unstable/debugger/frontend/probes'
68+ )
69+ } )
70+ } )
71+
972describe ( 'deliveryApi' , ( ) => {
1073 let fetchSpy : jasmine . Spy
1174 let clock : Clock
1275
1376 function makeConfig ( overrides : Partial < DeliveryApiConfiguration > = { } ) : DeliveryApiConfiguration {
1477 return {
1578 service : 'test-service' ,
79+ clientToken : 'test-client-token' ,
1680 env : 'staging' ,
1781 version : '1.0.0' ,
1882 pollInterval : 5000 ,
@@ -57,11 +121,19 @@ describe('deliveryApi', () => {
57121
58122 expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 )
59123 const [ url , options ] = fetchSpy . calls . mostRecent ( ) . args
60- expect ( url ) . toBe ( '/ api/ui/ debugger/probe-delivery ' )
124+ expect ( url ) . toBe ( 'https:// api.datadoghq.com/api/unstable/ debugger/frontend/probes ' )
61125 expect ( options . method ) . toBe ( 'POST' )
62- expect ( options . credentials ) . toBe ( 'same-origin' )
126+ expect ( options . credentials ) . toBeUndefined ( )
63127 expect ( options . headers [ 'Content-Type' ] ) . toBe ( 'application/json; charset=utf-8' )
64128 expect ( options . headers [ 'Accept' ] ) . toBe ( 'application/vnd.datadog.debugger-probes+json; version=1' )
129+ expect ( options . headers [ 'dd-client-token' ] ) . toBe ( 'test-client-token' )
130+ } )
131+
132+ it ( 'should use the configured site for the request URL' , ( ) => {
133+ startDeliveryApiPolling ( makeConfig ( { site : 'datadoghq.eu' } ) )
134+
135+ const [ url ] = fetchSpy . calls . mostRecent ( ) . args
136+ expect ( url ) . toBe ( 'https://api.datadoghq.eu/api/unstable/debugger/frontend/probes' )
65137 } )
66138
67139 it ( 'should send the correct request body' , ( ) => {
0 commit comments