From 9a11aeb6837f0353d0f5345a631b2a9fb67d927c Mon Sep 17 00:00:00 2001 From: Alexander Andryashin Date: Sat, 24 Mar 2018 17:40:53 +0300 Subject: [PATCH] W3C protocol session creation --- selenoid.go | 11 +++++++++-- selenoid_test.go | 24 ++++++++++++++++++++++-- session/session.go | 5 ++--- utils_test.go | 14 +++++++------- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/selenoid.go b/selenoid.go index 8a5d704f..24be0c03 100644 --- a/selenoid.go +++ b/selenoid.go @@ -23,11 +23,12 @@ import ( "crypto/rand" "encoding/hex" + "github.com/aerokube/selenoid/session" "github.com/aerokube/util" "github.com/docker/docker/api/types" - "golang.org/x/net/websocket" "github.com/docker/docker/pkg/stdcopy" + "golang.org/x/net/websocket" ) var ( @@ -126,7 +127,10 @@ func create(w http.ResponseWriter, r *http.Request) { return } var browser struct { - Caps session.Caps `json:"desiredCapabilities"` + Caps session.Caps `json:"desiredCapabilities"` + W3CCaps struct { + Caps session.Caps `json:"alwaysMatch"` + } `json:"capabilities"` } err = json.Unmarshal(body, &browser) if err != nil { @@ -135,6 +139,9 @@ func create(w http.ResponseWriter, r *http.Request) { queue.Drop() return } + if browser.W3CCaps.Caps.Name != "" { + browser.Caps = browser.W3CCaps.Caps + } browser.Caps.ProcessExtensionCapabilities() resolution, err := getScreenResolution(browser.Caps.ScreenResolution) if err != nil { diff --git a/selenoid_test.go b/selenoid_test.go index 3219cefc..a6ed53c7 100644 --- a/selenoid_test.go +++ b/selenoid_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "github.com/aerokube/selenoid/config" "io/ioutil" "log" "net/http" @@ -14,10 +13,13 @@ import ( "testing" "time" + "github.com/aerokube/selenoid/config" + "encoding/json" + "path/filepath" + . "github.com/aandryashin/matchers" . "github.com/aandryashin/matchers/httpresp" - "path/filepath" ) var ( @@ -188,6 +190,24 @@ func TestSessionCreated(t *testing.T) { queue.Release() } +func TestSessionCreatedW3C(t *testing.T) { + manager = &HTTPTest{Handler: Selenium()} + + resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte(`{"capabilities":{"alwaysMatch":{"acceptInsecureCerts":true,"browserName":"firefox"}}}`))) + AssertThat(t, err, Is{nil}) + var sess map[string]string + AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&sess}}) + + resp, err = http.Get(With(srv.URL).Path("/status")) + AssertThat(t, err, Is{nil}) + var state config.State + AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&state}}) + AssertThat(t, state.Used, EqualTo{1}) + AssertThat(t, queue.Used(), EqualTo{1}) + sessions.Remove(sess["sessionId"]) + queue.Release() +} + func TestSessionCreatedWdHub(t *testing.T) { root := http.NewServeMux() root.Handle("/wd/hub/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/session/session.go b/session/session.go index aa26ece9..e70a3036 100644 --- a/session/session.go +++ b/session/session.go @@ -24,14 +24,13 @@ type Caps struct { HostsEntries string `json:"hostsEntries"` Labels string `json:"labels"` ExtensionCapabilities map[string]interface{} `json:"selenoid:options"` - AlwaysMatch map[string]interface{} `json:"alwaysMatch"` } func (c *Caps) ProcessExtensionCapabilities() { if c.W3CVersion != "" { c.Version = c.W3CVersion } - if len(c.ExtensionCapabilities) > 0 || len(c.AlwaysMatch) > 0 { + if len(c.ExtensionCapabilities) > 0 { s := reflect.ValueOf(c).Elem() tagToFieldMap := make(map[string]reflect.StructField) @@ -43,7 +42,7 @@ func (c *Caps) ProcessExtensionCapabilities() { } //NOTE: entries from the first maps have less priority than then next ones - nestedMaps := []map[string]interface{}{c.ExtensionCapabilities, c.AlwaysMatch} + nestedMaps := []map[string]interface{}{c.ExtensionCapabilities} for _, nm := range nestedMaps { for k, v := range nm { value := reflect.ValueOf(v) diff --git a/utils_test.go b/utils_test.go index dce4279e..b6c5a913 100644 --- a/utils_test.go +++ b/utils_test.go @@ -13,11 +13,12 @@ import ( "time" + "testing" + . "github.com/aandryashin/matchers" "github.com/aerokube/selenoid/service" "github.com/aerokube/selenoid/session" "github.com/pborman/uuid" - "testing" ) type HTTPTest struct { @@ -128,10 +129,8 @@ func Selenium() http.Handler { func TestProcessExtensionCapabilities(t *testing.T) { capsJson := `{ - "browserVersion": "57.0", - "alwaysMatch": { - "browserName": "firefox" - }, + "version": "57.0", + "browserName": "firefox", "selenoid:options": { "name": "ExampleTestName", "enableVNC": true, @@ -142,9 +141,10 @@ func TestProcessExtensionCapabilities(t *testing.T) { var caps session.Caps err := json.Unmarshal([]byte(capsJson), &caps) AssertThat(t, err, Is{nil}) - AssertThat(t, caps.Name, EqualTo{""}) - AssertThat(t, caps.Version, EqualTo{""}) + AssertThat(t, caps.Name, EqualTo{"firefox"}) + AssertThat(t, caps.Version, EqualTo{"57.0"}) AssertThat(t, caps.TestName, EqualTo{""}) + caps.ProcessExtensionCapabilities() AssertThat(t, caps.Name, EqualTo{"firefox"}) AssertThat(t, caps.Version, EqualTo{"57.0"})