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

Commit

Permalink
Proper json format.
Browse files Browse the repository at this point in the history
  • Loading branch information
aandryashin committed Jul 5, 2016
1 parent fe998cb commit bc5c0aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
}

Expand Down
30 changes: 30 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bc5c0aa

Please sign in to comment.