diff --git a/apps/apps.go b/apps/apps.go index d9db5f4..c4d0678 100644 --- a/apps/apps.go +++ b/apps/apps.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + "io" + "io/ioutil" "strconv" "strings" @@ -56,14 +58,18 @@ func New(c *deis.Client, id string) (api.App, error) { } } - resBody, err := c.BasicRequest("POST", "/v2/apps/", body) - + res, err := c.Request("POST", "/v2/apps/", body) if err != nil { return api.App{}, err } + // Fix json.Decoder bug in 199 && res.StatusCode < 400 { return nil } - // Read the response body if none was provided. - if body == "" { - defer res.Body.Close() - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - return err - } - body = string(resBody) - } - - // Unmarshal the response as JSON, or return the status and body. - bodyMap := make(map[string]interface{}) - if err := json.Unmarshal([]byte(body), &bodyMap); err != nil { - return fmt.Errorf("\n%s\n%s\n", res.Status, body) - } - - errorMessage := fmt.Sprintf("\n%s\n", res.Status) - for key, value := range bodyMap { - switch v := value.(type) { - case string: - errorMessage += fmt.Sprintf("%s: %s\n", key, v) - case []interface{}: - for _, subValue := range v { - switch sv := subValue.(type) { - case string: - errorMessage += fmt.Sprintf("%s: %s\n", key, sv) - default: - fmt.Printf("Unexpected type in %s error message array. Contents: %v", - reflect.TypeOf(value), sv) - } - } - default: - fmt.Printf("Cannot handle key %s in error message, type %s. Contents: %v", - key, reflect.TypeOf(value), bodyMap[key]) - } - } + // TEMPORARY: Close body because methods won't close it (for now) because + // they can't tell the difference between an HTTP error and a bad status code + // (which has a body to close) + res.Body.Close() - return errors.New(errorMessage) + //TODO: Implement better error system! + return errors.New(res.Status) } // CheckConnection checks that the user is connected to a network and the URL points to a valid controller. diff --git a/http_test.go b/http_test.go index f38a0ac..69feaa5 100644 --- a/http_test.go +++ b/http_test.go @@ -51,7 +51,7 @@ func (f fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { return } - if req.URL.Path == "/basic/" && req.Method == "POST" { + if req.URL.Path == "/request/" && req.Method == "POST" { eT := "token abc" if req.Header.Get("Authorization") != eT { fmt.Printf("Token Wrong: Expected %s, Got %s\n", eT, req.Header.Get("Authorization")) @@ -84,7 +84,7 @@ func (f fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { return } - res.Write([]byte("basic")) + res.Write([]byte("request")) return } @@ -146,15 +146,16 @@ func TestBasicRequest(t *testing.T) { } deis.UserAgent = "test" - body, err := deis.BasicRequest("POST", "/basic/", []byte("test")) - + res, err := deis.Request("POST", "/request/", []byte("test")) if err != nil { t.Fatal(err) } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) - expected := "basic" - if body != expected { - t.Errorf("Expected %s, Got %s", expected, body) + expected := "request" + if string(body) != expected { + t.Errorf("Expected %s, Got %s", expected, string(body)) } if deis.ControllerAPIVersion != handler.Version { @@ -167,73 +168,6 @@ func TestBasicRequest(t *testing.T) { } } -func TestCheckErrors(t *testing.T) { - t.Parallel() - - expected := ` -404 NOT FOUND -error: This is an error. -error_array: This is an array. -error_array: Foo! -` - altExpected := ` -404 NOT FOUND -error_array: This is an array. -error_array: Foo! -error: This is an error. -` - - body := ` -{ - "error": "This is an error.", - "error_array": [ - "This is an array.", - "Foo!" - ] -}` - - res := http.Response{ - StatusCode: 404, - Status: "404 NOT FOUND", - } - - actual := checkForErrors(&res, body).Error() - - if actual != expected && actual != altExpected { - t.Errorf("Expected %s or %s, Got %s", expected, altExpected, actual) - } - - expected = ` -503 Service Temporarily Unavailable - -503 Service Temporarily Unavailable - -

503 Service Temporarily Unavailable

-
nginx/1.9.4
- - -` - - body = ` -503 Service Temporarily Unavailable - -

503 Service Temporarily Unavailable

-
nginx/1.9.4
- -` - - res = http.Response{ - StatusCode: http.StatusServiceUnavailable, - Status: "503 Service Temporarily Unavailable", - } - - actual = checkForErrors(&res, body).Error() - - if actual != expected { - t.Errorf("Expected %s, Got %s", expected, actual) - } -} - func TestCheckErrorsReturnsNil(t *testing.T) { t.Parallel() @@ -250,7 +184,7 @@ func TestCheckErrorsReturnsNil(t *testing.T) { } for _, res := range responses { - if err := checkForErrors(&res, ""); err != nil { + if err := checkForErrors(&res); err != nil { t.Fatal(err) } } diff --git a/keys/keys.go b/keys/keys.go index 8e7ce7f..a7dc429 100644 --- a/keys/keys.go +++ b/keys/keys.go @@ -3,6 +3,8 @@ package keys import ( "encoding/json" "fmt" + "io" + "io/ioutil" deis "github.com/deis/controller-sdk-go" "github.com/deis/controller-sdk-go/api" @@ -29,14 +31,18 @@ func New(c *deis.Client, id string, pubKey string) (api.Key, error) { req := api.KeyCreateRequest{ID: id, Public: pubKey} body, err := json.Marshal(req) - resBody, err := c.BasicRequest("POST", "/v2/keys/", body) - + res, err := c.Request("POST", "/v2/keys/", body) if err != nil { return api.Key{}, err } + // Fix json.Decoder bug in