@@ -4,11 +4,14 @@ import (
4
4
"fmt"
5
5
"net/http"
6
6
"net/http/httptest"
7
+ "net/url"
7
8
"testing"
8
9
9
10
"github.com/stretchr/testify/assert"
10
11
)
11
12
13
+ var needKeysInQueryParams = []string {"/v1/series" , "/v1/check_run" , "/v1/events" , "/v1/screen" }
14
+
12
15
func TestUriForApi (t * testing.T ) {
13
16
c := Client {
14
17
apiKey : "sample_api_key" ,
@@ -28,6 +31,47 @@ func TestUriForApi(t *testing.T) {
28
31
assert .Nil (t , err )
29
32
assert .Equal (t , "https://base.datadoghq.com/api/v1/events?api_key=sample_api_key&application_key=sample_app_key" , uri )
30
33
})
34
+ t .Run ("Test all endpoints that need keys in query params" , func (t * testing.T ) {
35
+ for _ , api := range needKeysInQueryParams {
36
+ uri , err := c .uriForAPI (api )
37
+ assert .Nil (t , err )
38
+ parsed , err := url .Parse (uri )
39
+ assert .Nil (t , err )
40
+ assert .Equal (t , parsed .Query ().Get ("api_key" ), "sample_api_key" )
41
+ assert .Equal (t , parsed .Query ().Get ("application_key" ), "sample_app_key" )
42
+ }
43
+ })
44
+ t .Run ("Test an endpoint that doesn't need keys in query params" , func (t * testing.T ) {
45
+ uri , err := c .uriForAPI ("/v1/dashboard" )
46
+ assert .Nil (t , err )
47
+ assert .Equal (t , "https://base.datadoghq.com/api/v1/dashboard" , uri )
48
+ })
49
+ }
50
+
51
+ func TestCreateRequest (t * testing.T ) {
52
+ c := Client {
53
+ apiKey : "sample_api_key" ,
54
+ appKey : "sample_app_key" ,
55
+ baseUrl : "https://base.datadoghq.com" ,
56
+ HttpClient : & http.Client {},
57
+ RetryTimeout : 1000 ,
58
+ }
59
+ t .Run ("Test an endpoint that doesn't need keys in query params" , func (t * testing.T ) {
60
+ req , err := c .createRequest ("GET" , "/v1/dashboard" , nil )
61
+ assert .Nil (t , err )
62
+ assert .Equal (t , "sample_api_key" , req .Header .Get ("DD-API-KEY" ))
63
+ assert .Equal (t , "sample_app_key" , req .Header .Get ("DD-APPLICATION-KEY" ))
64
+ })
65
+ t .Run ("Test endpoints that need keys in query params" , func (t * testing.T ) {
66
+ for _ , api := range needKeysInQueryParams {
67
+ req , err := c .createRequest ("GET" , api , nil )
68
+ assert .Nil (t , err )
69
+ // we make sure that we *don't* have keys in query params, because some endpoints
70
+ // fail if we send keys both in headers and query params
71
+ assert .Equal (t , "" , req .Header .Get ("DD-API-KEY" ))
72
+ assert .Equal (t , "" , req .Header .Get ("DD-APPLICATION-KEY" ))
73
+ }
74
+ })
31
75
}
32
76
33
77
func TestRedactError (t * testing.T ) {
0 commit comments