diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index 66cc6e4..09c5451 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -114,8 +114,15 @@ type ChromeFlag struct { Value *string `json:"value,omitempty"` } +type ChromeFlagOption struct { + Name string `json:"name"` + Label string `json:"label"` + Description string `json:"description"` + AcceptsValue bool `json:"acceptsValue"` +} + type ChromeFlagsResponse struct { - ChromeFlags []ChromeFlag `json:"chromeFlags"` + ChromeFlags []ChromeFlagOption `json:"chromeFlags"` } type ExcludedFileTypesResponse struct { diff --git a/syntheticsclientv2/get_chromeflags.go b/syntheticsclientv2/get_chromeflags.go new file mode 100644 index 0000000..9a81aff --- /dev/null +++ b/syntheticsclientv2/get_chromeflags.go @@ -0,0 +1,48 @@ +// Copyright 2021 Splunk, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package syntheticsclientv2 + +import ( + "bytes" + "encoding/json" +) + +func parseChromeFlagsResponse(response string) (*ChromeFlagsResponse, error) { + var chromeFlags ChromeFlagsResponse + err := json.Unmarshal([]byte(response), &chromeFlags) + if err != nil { + return nil, err + } + + return &chromeFlags, err +} + +func (c Client) GetChromeFlags() (*ChromeFlagsResponse, *RequestDetails, error) { + details, err := c.makePublicAPICall("GET", + "/chrome_flags", + bytes.NewBufferString("{}"), + nil) + + if err != nil { + return nil, details, err + } + + chromeFlags, err := parseChromeFlagsResponse(details.ResponseBody) + if err != nil { + return chromeFlags, details, err + } + + return chromeFlags, details, nil +} diff --git a/syntheticsclientv2/get_chromeflags_test.go b/syntheticsclientv2/get_chromeflags_test.go new file mode 100644 index 0000000..b5abbf9 --- /dev/null +++ b/syntheticsclientv2/get_chromeflags_test.go @@ -0,0 +1,112 @@ +// Copyright 2021 Splunk, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package syntheticsclientv2 + +import ( + "net/http" + "reflect" + "testing" +) + +var getChromeFlagsBody = `{"chromeFlags":[{"name":"--disable-http2","label":"Disable h2","description":"Disables HTTP/2.0.","acceptsValue":false},{"name":"--proxy-server","label":"Proxy server","description":"Use a specified proxy server.","acceptsValue":true}]}` + +func TestGetChromeFlags(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/chrome_flags", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + if r.URL.Path != "/chrome_flags" { + t.Fatalf("path = %q, want /chrome_flags", r.URL.Path) + } + _, err := w.Write([]byte(getChromeFlagsBody)) + if err != nil { + t.Fatal(err) + } + }) + + resp, details, err := testClient.GetChromeFlags() + if err != nil { + t.Fatal(err) + } + if details.StatusCode != http.StatusOK { + t.Fatalf("status code = %d, want %d", details.StatusCode, http.StatusOK) + } + + expected := []ChromeFlagOption{ + {Name: "--disable-http2", Label: "Disable h2", Description: "Disables HTTP/2.0.", AcceptsValue: false}, + {Name: "--proxy-server", Label: "Proxy server", Description: "Use a specified proxy server.", AcceptsValue: true}, + } + if !reflect.DeepEqual(resp.ChromeFlags, expected) { + t.Fatalf("ChromeFlags = %#v, want %#v", resp.ChromeFlags, expected) + } +} + +func TestParseChromeFlagsResponse(t *testing.T) { + resp, err := parseChromeFlagsResponse(getChromeFlagsBody) + if err != nil { + t.Fatal(err) + } + + expected := []ChromeFlagOption{ + {Name: "--disable-http2", Label: "Disable h2", Description: "Disables HTTP/2.0.", AcceptsValue: false}, + {Name: "--proxy-server", Label: "Proxy server", Description: "Use a specified proxy server.", AcceptsValue: true}, + } + if !reflect.DeepEqual(resp.ChromeFlags, expected) { + t.Fatalf("ChromeFlags = %#v, want %#v", resp.ChromeFlags, expected) + } +} + +func TestGetChromeFlagsReturnsErrorOnMalformedResponse(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/chrome_flags", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + _, err := w.Write([]byte("{not valid json")) + if err != nil { + t.Fatal(err) + } + }) + + resp, details, err := testClient.GetChromeFlags() + if err == nil { + t.Fatal("expected a parse error, but got none") + } + if details == nil { + t.Fatal("expected request details") + } + if resp != nil { + t.Errorf("expected nil response, got %#v", resp) + } +} + +func TestGetChromeFlagsReturnsErrorOnNetworkFailure(t *testing.T) { + unreachableClient := NewConfigurableClient("apiKey", "realm", ClientArgs{publicBaseUrl: "http://127.0.0.1:1"}) + _, _, err := unreachableClient.GetChromeFlags() + if err == nil { + t.Fatal("expected a connection error") + } +} + +func TestParseChromeFlagsResponseReturnsErrorOnMalformedJSON(t *testing.T) { + resp, err := parseChromeFlagsResponse("{not valid json") + if err == nil { + t.Fatal("expected a parse error, but got none") + } + if resp != nil { + t.Errorf("expected nil response, got %#v", resp) + } +} diff --git a/syntheticsclientv2/integration_test.go b/syntheticsclientv2/integration_test.go index d088255..08d027b 100644 --- a/syntheticsclientv2/integration_test.go +++ b/syntheticsclientv2/integration_test.go @@ -1829,6 +1829,17 @@ func TestLiveGetExcludedFileTypesV2(t *testing.T) { JsonPrint(res) } +func TestLiveGetChromeFlags(t *testing.T) { + //Create your client with the token + c := NewClient(token, realm) + + res, _, err := c.GetChromeFlags() + if err != nil { + t.Fatal(err) + } + JsonPrint(res) +} + func liveCreateHttpCheckV2WithNullablePortInput(name string) *HttpCheckV2InputWithNullablePort { input := &HttpCheckV2InputWithNullablePort{} input.Test.Name = name