Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1078 from vania-pooh/master
Browse files Browse the repository at this point in the history
Removing only selenoid:options and preserving stuff like goog:chromeO…
  • Loading branch information
aandryashin authored Mar 26, 2021
2 parents a6ae0c7 + bc819b4 commit c6ca859
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
39 changes: 32 additions & 7 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,11 @@ func create(w http.ResponseWriter, r *http.Request) {
u := startedService.Url
cancel := startedService.Cancel

browser.Caps.ExtensionCapabilities = nil
browser.W3CCaps.Caps.ExtensionCapabilities = nil
for _, c := range browser.W3CCaps.FirstMatch {
c.ExtensionCapabilities = nil
}

var resp *http.Response
i := 1
for ; ; i++ {
r.URL.Host, r.URL.Path = u.Host, path.Join(u.Path, r.URL.Path)
newBody, _ := json.Marshal(browser)
newBody := removeSelenoidOptions(body)
req, _ := http.NewRequest(http.MethodPost, r.URL.String(), bytes.NewReader(newBody))
ctx, done := context.WithTimeout(r.Context(), newSessionAttemptTimeout)
defer done()
Expand Down Expand Up @@ -357,6 +351,37 @@ func create(w http.ResponseWriter, r *http.Request) {
log.Printf("[%d] [SESSION_CREATED] [%s] [%d] [%.2fs]", requestId, s.ID, i, util.SecondsSince(sessionStartTime))
}

func removeSelenoidOptions(input []byte) []byte {
body := make(map[string]interface{})
_ = json.Unmarshal(input, &body)
const selenoidOptions = "selenoid:options"
if raw, ok := body["desiredCapabilities"]; ok {
if dc, ok := raw.(map[string]interface{}); ok {
delete(dc, selenoidOptions)
}
}
if raw, ok := body["capabilities"]; ok {
if c, ok := raw.(map[string]interface{}); ok {
if raw, ok := c["alwaysMatch"]; ok {
if am, ok := raw.(map[string]interface{}); ok {
delete(am, selenoidOptions)
}
}
if raw, ok := c["firstMatch"]; ok {
if fm, ok := raw.([]interface{}); ok {
for _, raw := range fm {
if c, ok := raw.(map[string]interface{}); ok {
delete(c, selenoidOptions)
}
}
}
}
}
}
ret, _ := json.Marshal(body)
return ret
}

func preprocessSessionId(sid string) string {
if ggrHost != nil {
return ggrHost.Sum() + sid
Expand Down
24 changes: 18 additions & 6 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"github.com/aerokube/selenoid/session"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/rpcc"
"io/ioutil"
Expand Down Expand Up @@ -392,25 +391,38 @@ func TestSessionCreatedRedirect(t *testing.T) {
}

func TestSessionCreatedRemoveExtensionCapabilities(t *testing.T) {
extensionCapsMissing := true
desiredCapabilitiesPresent := true
alwaysMatchPresent := true
firstMatchPresent := true
chromeOptionsPresent := true

var browser struct {
Caps map[string]interface{} `json:"desiredCapabilities"`
W3CCaps struct {
Caps session.Caps `json:"alwaysMatch"`
AlwaysMatch map[string]interface{} `json:"alwaysMatch"`
FirstMatch []map[string]interface{} `json:"firstMatch"`
} `json:"capabilities"`
}

root := http.NewServeMux()
root.Handle("/wd/hub/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&browser)
AssertThat(t, err, Is{nil})
extensionCapsMissing = browser.W3CCaps.Caps.ExtensionCapabilities == nil
_, desiredCapabilitiesPresent = browser.Caps["selenoid:options"]
_, alwaysMatchPresent = browser.W3CCaps.AlwaysMatch["selenoid:options"]
_, chromeOptionsPresent = browser.W3CCaps.AlwaysMatch["goog:chromeOptions"]
AssertThat(t, len(browser.W3CCaps.FirstMatch), EqualTo{1})
_, firstMatchPresent = browser.W3CCaps.FirstMatch[0]["selenoid:options"]
}))
manager = &HTTPTest{Handler: root}

resp, err := httpClient.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte(`{"capabilities":{"alwaysMatch":{"browserName": "firefox", "selenoid:options":{"enableVNC": true}}}}`)))
resp, err := httpClient.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities": {"browserName": "chrome", "selenoid:options": {"enableVNC": true}}, "capabilities":{"alwaysMatch":{"browserName": "chrome", "goog:chromeOptions": {"args": ["headless"]}, "selenoid:options":{"enableVNC": true}}, "firstMatch": [{"platform": "linux", "selenoid:options": {"enableVideo": true}}]}}`)))
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{http.StatusOK})
AssertThat(t, extensionCapsMissing, Is{true})
AssertThat(t, desiredCapabilitiesPresent, Is{false})
AssertThat(t, alwaysMatchPresent, Is{false})
AssertThat(t, chromeOptionsPresent, Is{true})
AssertThat(t, firstMatchPresent, Is{false})
}

func TestProxySession(t *testing.T) {
Expand Down

0 comments on commit c6ca859

Please sign in to comment.