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

Commit

Permalink
Return json object with error message, when session is not created.
Browse files Browse the repository at this point in the history
  • Loading branch information
aandryashin committed Jul 5, 2016
1 parent 6f2ca6f commit fe998cb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

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

0 comments on commit fe998cb

Please sign in to comment.