diff --git a/proxy.go b/proxy.go index c4ff792..090a204 100644 --- a/proxy.go +++ b/proxy.go @@ -141,6 +141,15 @@ func browserErrMsg(js map[string]interface{}) string { return msg } +func jsonErrMsg(msg string) string { + message := make(map[string]string) + message["message"] = msg + value := make(map[string]interface{}) + value["value"] = message + result, _ := json.Marshal(value) + return string(result) +} + func route(w http.ResponseWriter, r *http.Request) { start := time.Now() id := serial() @@ -194,7 +203,7 @@ loop: } } } - http.Error(w, fmt.Sprintf(`{"status": 13, "value" : {"message" : "cannot create session %s on any hosts after %d attempt(s)"}}`, fmtBrowser(browser, version), count), http.StatusInternalServerError) + http.Error(w, jsonErrMsg(fmt.Sprintf("cannot create session %s on any hosts after %d attempt(s)", fmtBrowser(browser, version), count)), http.StatusInternalServerError) log.Printf("[%d] [SESSION_NOT_CREATED] [%s] [%s] [%s]\n", id, user, remote, fmtBrowser(browser, version)) } diff --git a/proxy_test.go b/proxy_test.go index fca842e..d33d0a4 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -248,6 +248,36 @@ func TestStartSession(t *testing.T) { AssertThat(t, rsp, AllOf{Code{http.StatusOK}, Body{`{"sessionId":"` + node.sum() + `123"}`}}) } +func TestStartSessionWithJsonSpecChars(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"sessionId":"123"}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + config = Browsers{Browsers: []Browser{ + Browser{Name: "{browser}", DefaultVersion: "1.0", Versions: []Version{ + Version{Number: "1.0", Regions: []Region{ + Region{Hosts: Hosts{ + node, + }}, + }}, + }}}} + routes = linkRoutes(&config) + + rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"{browser}", "version":"1.0"}}`))) + + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp, AllOf{Code{http.StatusOK}, Body{`{"sessionId":"` + node.sum() + `123"}`}}) +} + func TestStartSessionFail(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) {