Skip to content

Commit 09f8465

Browse files
feat(deis): only return an APIMismatch when controller is older than SDK (#63)
1 parent d8928d8 commit 09f8465

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *Client) Request(method string, path string, body []byte) (*http.Respons
6666
c.DeisVersion = res.Header.Get("DEIS_PLATFORM_VERSION")
6767

6868
// Return results along with api compatibility error
69-
return res, checkAPICompatibility(apiVersion)
69+
return res, checkAPICompatibility(apiVersion, APIVersion)
7070
}
7171

7272
// LimitedRequest allows limiting the number of responses in a request.
@@ -129,7 +129,7 @@ your deis version is correct.`
129129
apiVersion := res.Header.Get("DEIS_API_VERSION")
130130
c.ControllerAPIVersion = apiVersion
131131

132-
return checkAPICompatibility(apiVersion)
132+
return checkAPICompatibility(apiVersion, APIVersion)
133133
}
134134

135135
func addUserAgent(headers *http.Header, userAgent string) {

utils.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package deis
22

3-
func checkAPICompatibility(serverAPIVersion string) error {
4-
if serverAPIVersion != APIVersion {
3+
import "strings"
4+
5+
func checkAPICompatibility(serverAPIVersion, clientAPIVersion string) error {
6+
sVersion := strings.Split(serverAPIVersion, ".")
7+
aVersion := strings.Split(clientAPIVersion, ".")
8+
9+
// If API Versions are invalid, return a mismatch error.
10+
if len(sVersion) < 2 || len(aVersion) < 2 {
11+
return ErrAPIMismatch
12+
}
13+
14+
// If major versions are different, return a mismatch error.
15+
if sVersion[0] != aVersion[0] {
16+
return ErrAPIMismatch
17+
}
18+
19+
// If server is older than client, return mismatch error.
20+
if sVersion[1] < aVersion[1] {
521
return ErrAPIMismatch
622
}
723

utils_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package deis
2+
3+
import "testing"
4+
5+
type versionComparison struct {
6+
Client string
7+
Server string
8+
Error error
9+
}
10+
11+
func TestCheckAPIVersions(t *testing.T) {
12+
comparisons := []versionComparison{
13+
{"1.2", "2.1", ErrAPIMismatch},
14+
{"2.1", "1.2", ErrAPIMismatch},
15+
{"2.1", "2.2", ErrAPIMismatch},
16+
{"2.3", "2.0", nil},
17+
}
18+
19+
for _, check := range comparisons {
20+
err := checkAPICompatibility(check.Client, check.Server)
21+
22+
if err != check.Error {
23+
t.Errorf("%v: Expected %v, Got %v", check, check.Error, err)
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)