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

Commit

Permalink
/clipboard and /download are available as protocol extension commands (
Browse files Browse the repository at this point in the history
…fixes #399)
  • Loading branch information
vania-pooh committed Nov 20, 2021
1 parent 57c9044 commit 20a5115
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
13 changes: 12 additions & 1 deletion selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func getSessionTimeout(sessionTimeout string, maxTimeout time.Duration, defaultT
if sessionTimeout != "" {
st, err := time.ParseDuration(sessionTimeout)
if err != nil {
return 0, fmt.Errorf("Invalid sessionTimeout capability: %v", err)
return 0, fmt.Errorf("invalid sessionTimeout capability: %v", err)
}
if st <= maxTimeout {
return st, nil
Expand Down Expand Up @@ -470,6 +470,8 @@ func generateRandomFileName(extension string) string {
return "selenoid" + hex.EncodeToString(randBytes) + extension
}

const vendorPrefix = "aerokube"

func proxy(w http.ResponseWriter, r *http.Request) {
done := make(chan func())
go func() {
Expand All @@ -486,6 +488,15 @@ func proxy(w http.ResponseWriter, r *http.Request) {
id := fragments[2]
sess, ok := sessions.Get(id)
if ok {
if len(fragments) >= 4 && fragments[3] == vendorPrefix {
newFragments := []string{"", fragments[4], id}
if len(fragments) >= 5 {
newFragments = append(newFragments, fragments[5:]...)
}
r.URL.Host = (&request{r}).localaddr()
r.URL.Path = path.Clean(strings.Join(newFragments, slash))
return
}
sess.Lock.Lock()
defer sess.Lock.Unlock()
select {
Expand Down
30 changes: 27 additions & 3 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ func TestServeAndDeleteLogFile(t *testing.T) {
}

func TestFileDownload(t *testing.T) {
testFileDownload(t, func(sessionId string) string {
return fmt.Sprintf("/download/%s/testfile", sessionId)
})
}

func TestFileDownloadProtocolExtension(t *testing.T) {
testFileDownload(t, func(sessionId string) string {
return fmt.Sprintf("/wd/hub/session/%s/aerokube/download/testfile", sessionId)
})
}

func testFileDownload(t *testing.T, path func(string) string) {
manager = &HTTPTest{Handler: Selenium()}

resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte("{}")))
Expand All @@ -819,7 +831,7 @@ func TestFileDownload(t *testing.T) {
var sess map[string]string
AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&sess}})

rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/download/%s/testfile", sess["sessionId"])))
rsp, err := http.Get(With(srv.URL).Path(path(sess["sessionId"])))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
data, err := io.ReadAll(rsp.Body)
Expand All @@ -837,6 +849,18 @@ func TestFileDownloadMissingSession(t *testing.T) {
}

func TestClipboard(t *testing.T) {
testClipboard(t, func(sessionId string) string {
return fmt.Sprintf("/clipboard/%s", sessionId)
})
}

func TestClipboardProtocolExtension(t *testing.T) {
testClipboard(t, func(sessionId string) string {
return fmt.Sprintf("/wd/hub/session/%s/aerokube/clipboard", sessionId)
})
}

func testClipboard(t *testing.T, path func(string) string) {
manager = &HTTPTest{Handler: Selenium()}

resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte("{}")))
Expand All @@ -845,14 +869,14 @@ func TestClipboard(t *testing.T) {
var sess map[string]string
AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&sess}})

rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/clipboard/%s", sess["sessionId"])))
rsp, err := http.Get(With(srv.URL).Path(path(sess["sessionId"])))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
data, err := io.ReadAll(rsp.Body)
AssertThat(t, err, Is{nil})
AssertThat(t, string(data), EqualTo{"test-clipboard-value"})

rsp, err = http.Post(With(srv.URL).Path(fmt.Sprintf("/clipboard/%s", sess["sessionId"])), "text/plain", bytes.NewReader([]byte("any-data")))
rsp, err = http.Post(With(srv.URL).Path(path(sess["sessionId"])), "text/plain", bytes.NewReader([]byte("any-data")))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})

Expand Down

0 comments on commit 20a5115

Please sign in to comment.