Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion syntheticsclientv2/common_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions syntheticsclientv2/get_chromeflags.go
Original file line number Diff line number Diff line change
@@ -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
}
112 changes: 112 additions & 0 deletions syntheticsclientv2/get_chromeflags_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 11 additions & 0 deletions syntheticsclientv2/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading