11package client
22
3-
43import (
5- "testing"
6- "regexp"
7- "strings"
4+ "encoding/json"
85 "net/http"
96 "net/http/httptest"
10- "encoding/json"
117 "os"
8+ "regexp"
9+ "strings"
10+ "testing"
1211
1312 "github.com/google/go-cmp/cmp"
1413)
@@ -23,41 +22,41 @@ func TestAlerts(t *testing.T) {
2322 json .Unmarshal (ropen , & aopen )
2423 json .Unmarshal (rall , & aall )
2524 server := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
26- urlall := regexp .MustCompile (`^/api/([0-9]+.[0-9]+)?/alerts$` )
27- urlopen := regexp .MustCompile (`^/api/([0-9]+.[0-9]+)?/alerts\?filter=state%3D%27open%27$` )
28- if r .URL .Path == "/api/api_version" {
29- w .Header ().Set ("Content-Type" , "application/json" )
30- w .WriteHeader (http .StatusOK )
25+ urlall := regexp .MustCompile (`^/api/([0-9]+.[0-9]+)?/alerts$` )
26+ urlopen := regexp .MustCompile (`^/api/([0-9]+.[0-9]+)?/alerts\?filter=state%3D%27open%27$` )
27+ if r .URL .Path == "/api/api_version" {
28+ w .Header ().Set ("Content-Type" , "application/json" )
29+ w .WriteHeader (http .StatusOK )
3130 w .Write ([]byte (vers ))
32- } else if urlopen .MatchString (r .URL .Path + "?" + r .URL .RawQuery ) {
31+ } else if urlopen .MatchString (r .URL .Path + "?" + r .URL .RawQuery ) {
3332 w .Header ().Set ("x-auth-token" , "faketoken" )
3433 w .Header ().Set ("Content-Type" , "application/json" )
3534 w .WriteHeader (http .StatusOK )
3635 w .Write ([]byte (ropen ))
37- } else if urlall .MatchString (r .URL .Path ) {
36+ } else if urlall .MatchString (r .URL .Path ) {
3837 w .Header ().Set ("x-auth-token" , "faketoken" )
3938 w .Header ().Set ("Content-Type" , "application/json" )
4039 w .WriteHeader (http .StatusOK )
4140 w .Write ([]byte (rall ))
4241 }
43- }))
44- endp := strings .Split (server .URL , "/" )
45- e := endp [len (endp )- 1 ]
46- t .Run ("alerts_open" , func (t * testing.T ) {
47- c := NewRestClient (e , "fake-api-token" , "latest" , "test-user-agent-string" , false )
48- al := c .GetAlerts ("state='open'" )
49- if diff := cmp .Diff (al .Items , aopen .Items ); diff != "" {
50- t .Errorf ("Mismatch (-want +got):\n %s" , diff )
51- server .Close ()
52- }
53- })
54- t .Run ("alerts_all" , func (t * testing.T ) {
55- c := NewRestClient (e , "fake-api-token" , "latest" , "test-user-agent-string" , false )
56- al := c .GetAlerts ("" )
57- if diff := cmp .Diff (al .Items , aall .Items ); diff != "" {
58- t .Errorf ("Mismatch (-want +got):\n %s" , diff )
59- server .Close ()
60- }
61- })
62- server .Close ()
42+ }))
43+ endp := strings .Split (server .URL , "/" )
44+ e := endp [len (endp )- 1 ]
45+ t .Run ("alerts_open" , func (t * testing.T ) {
46+ c := NewRestClient (e , "fake-api-token" , "latest" , "test-user-agent-string" , false )
47+ al := c .GetAlerts ("state='open'" )
48+ if diff := cmp .Diff (al .Items , aopen .Items ); diff != "" {
49+ t .Errorf ("Mismatch (-want +got):\n %s" , diff )
50+ server .Close ()
51+ }
52+ })
53+ t .Run ("alerts_all" , func (t * testing.T ) {
54+ c := NewRestClient (e , "fake-api-token" , "latest" , "test-user-agent-string" , false )
55+ al := c .GetAlerts ("" )
56+ if diff := cmp .Diff (al .Items , aall .Items ); diff != "" {
57+ t .Errorf ("Mismatch (-want +got):\n %s" , diff )
58+ server .Close ()
59+ }
60+ })
61+ server .Close ()
6362}
0 commit comments