From b97e406c5c56983901cb0486c799f9f4e207e1b3 Mon Sep 17 00:00:00 2001 From: Georgij Boljuba Date: Thu, 22 Jul 2021 12:36:32 +0200 Subject: [PATCH] Propagate Content-Type header if client sends it --- selenoid.go | 4 ++++ selenoid_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/selenoid.go b/selenoid.go index 5016bb53..5a1ba33d 100644 --- a/selenoid.go +++ b/selenoid.go @@ -206,6 +206,10 @@ func create(w http.ResponseWriter, r *http.Request) { r.URL.Host, r.URL.Path = u.Host, path.Join(u.Path, r.URL.Path) newBody := removeSelenoidOptions(body) req, _ := http.NewRequest(http.MethodPost, r.URL.String(), bytes.NewReader(newBody)) + contentType := r.Header.Get("Content-Type") + if len(contentType) > 0 { + req.Header.Set("Content-Type", contentType) + } ctx, done := context.WithTimeout(r.Context(), newSessionAttemptTimeout) defer done() log.Printf("[%d] [SESSION_ATTEMPTED] [%s] [%d]", requestId, u.String(), i) diff --git a/selenoid_test.go b/selenoid_test.go index 057c2202..c9172dfa 100644 --- a/selenoid_test.go +++ b/selenoid_test.go @@ -304,6 +304,30 @@ func TestSessionCreatedWdHub(t *testing.T) { queue.Release() } +func TestSessionWithContentTypeCreatedWdHub(t *testing.T) { + root := http.NewServeMux() + root.Handle("/wd/hub/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.URL.Path = strings.TrimPrefix(r.URL.Path, "/wd/hub") + AssertThat(t, r.Header.Get("Content-Type"), Is{"application/json; charset=utf-8"}) + Selenium().ServeHTTP(w, r) + })) + manager = &HTTPTest{Handler: root} + + resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "application/json; charset=utf-8", bytes.NewReader([]byte("{}"))) + 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 TestSessionFailedAfterTimeout(t *testing.T) { newSessionAttemptTimeout = 10 * time.Millisecond manager = &HTTPTest{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {