From fe998cb393ec4209ff7cdbb046f9f21df75076e2 Mon Sep 17 00:00:00 2001 From: Alexander Andryashin Date: Wed, 6 Jul 2016 00:23:43 +0300 Subject: [PATCH] Return json object with error message, when session is not created. --- proxy.go | 4 ++-- proxy_test.go | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/proxy.go b/proxy.go index e63c230..c4ff792 100644 --- a/proxy.go +++ b/proxy.go @@ -188,13 +188,13 @@ loop: excludes = append(excludes, h.region) hosts = config.find(browser, version, excludes...) } - log.Printf("[%d] [SESSION_FAILED] [%s] [%s] [%s] [%s] - %s\n", id, user, remote, fmtBrowser(browser, version), h.net(), browserErrMsg(resp)) + log.Printf("[%d] [SESSION_FAILED] [%s] [%s] [%s] [%s] %s\n", id, user, remote, fmtBrowser(browser, version), h.net(), browserErrMsg(resp)) if len(hosts) == 0 { break loop } } } - http.Error(w, fmt.Sprintf("cannot create session %s on any hosts after %d attempt(s)", fmtBrowser(browser, version), count), http.StatusInternalServerError) + 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) 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 18409d6..fca842e 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -49,6 +49,33 @@ func (m Body) String() string { return fmt.Sprintf("response body %v", m.B) } +type Message struct { + B string +} + +func (m Message) Match(i interface{}) bool { + rsp := i.(*http.Response) + var reply map[string]interface{} + err := json.NewDecoder(rsp.Body).Decode(&reply) + rsp.Body.Close() + if err != nil { + return false + } + val, ok := reply["value"].(map[string]interface{}) + if !ok { + return false + } + msg, ok := val["message"].(string) + if !ok { + return false + } + return EqualTo{m.B}.Match(msg) +} + +func (m Message) String() string { + return fmt.Sprintf("json error message %v", m.B) +} + func hostport(u string) string { uri, _ := url.Parse(u) return uri.Host @@ -154,9 +181,8 @@ func TestCreateSessionNoHosts(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestCreateSessionHostDown(t *testing.T) { @@ -174,9 +200,8 @@ func TestCreateSessionHostDown(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestSessionEmptyHash(t *testing.T) { @@ -250,7 +275,7 @@ func TestStartSessionFail(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestStartSessionBrowserFail(t *testing.T) { @@ -281,7 +306,7 @@ func TestStartSessionBrowserFail(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 5 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 5 attempt(s)"}}) } func TestStartSessionBrowserFailUnknownError(t *testing.T) { @@ -312,7 +337,7 @@ func TestStartSessionBrowserFailUnknownError(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestStartSessionBrowserFailWrongValue(t *testing.T) { @@ -343,7 +368,7 @@ func TestStartSessionBrowserFailWrongValue(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestStartSessionBrowserFailWrongMsg(t *testing.T) { @@ -374,7 +399,7 @@ func TestStartSessionBrowserFailWrongMsg(t *testing.T) { 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.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Message{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) } func TestDeleteSession(t *testing.T) {