Skip to content

Commit b9d467a

Browse files
authored
Merge pull request #70 from nitrous-io/download-api-json
Change download API to respond with JSON instead of a redirect
2 parents 83e6735 + 8b4bf87 commit b9d467a

File tree

2 files changed

+13
-38
lines changed

2 files changed

+13
-38
lines changed

apiserver/controllers/deployments/deployments.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ func Download(c *gin.Context) {
464464
return
465465
}
466466

467-
c.Redirect(http.StatusFound, url)
467+
c.JSON(http.StatusOK, gin.H{
468+
"url": url,
469+
})
468470
}
469471

470472
// Rollback either rolls back a project to the previous deployment, or to a

apiserver/controllers/deployments/deployments_test.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package deployments_test
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -994,39 +993,8 @@ var _ = Describe("Deployments", func() {
994993

995994
doRequest := func() {
996995
s = httptest.NewServer(server.New())
997-
uri := fmt.Sprintf("%s/projects/foo-bar-express/deployments/%d/download", s.URL, depl.ID)
998-
999-
req, err := http.NewRequest("GET", uri, nil)
1000-
Expect(err).To(BeNil())
1001-
1002-
if headers != nil {
1003-
for k, v := range headers {
1004-
for _, h := range v {
1005-
req.Header.Add(k, h)
1006-
}
1007-
}
1008-
}
1009-
1010-
// Hack so that we don't follow redirects.
1011-
// In Go 1.7, we can use http.ErrUseLastResponse (see https://github.com/golang/go/commit/8f13080267d0ddbb50da9029339796841224116a).
1012-
redirectErr := errors.New("redirect received")
1013-
client := &http.Client{
1014-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
1015-
if len(via) > 0 {
1016-
return redirectErr
1017-
}
1018-
return nil
1019-
},
1020-
}
1021-
1022-
res, err = client.Do(req)
1023-
if err != nil {
1024-
if e, ok := err.(*url.Error); ok {
1025-
Expect(e.Err).To(Equal(redirectErr))
1026-
return
1027-
}
1028-
}
1029-
996+
url := fmt.Sprintf("%s/projects/foo-bar-express/deployments/%d/download", s.URL, depl.ID)
997+
res, err = testhelper.MakeRequest("GET", url, nil, headers, nil)
1030998
Expect(err).To(BeNil())
1031999
}
10321000

@@ -1149,7 +1117,7 @@ var _ = Describe("Deployments", func() {
11491117
fakeS3.PresignedURLReturn = "https://s3-us-west-2.amazonaws.com/deployments/abcd/raw-bundle.zip?abc=123"
11501118
})
11511119

1152-
It("redirects to a pre-signed download URL of the raw bundle in S3", func() {
1120+
It("responds with a pre-signed download URL of the raw bundle in S3", func() {
11531121
doRequest()
11541122

11551123
Expect(fakeS3.PresignedURLCalls.Count()).To(Equal(1))
@@ -1159,8 +1127,13 @@ var _ = Describe("Deployments", func() {
11591127
Expect(call.Arguments[1]).To(Equal(s3client.BucketName))
11601128
Expect(call.Arguments[2]).To(Equal(bun.UploadedPath))
11611129

1162-
Expect(res.StatusCode).To(Equal(http.StatusFound))
1163-
Expect(res.Header.Get("Location")).To(Equal(fakeS3.PresignedURLReturn))
1130+
b := &bytes.Buffer{}
1131+
_, err = b.ReadFrom(res.Body)
1132+
1133+
Expect(res.StatusCode).To(Equal(http.StatusOK))
1134+
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
1135+
"url": "%s"
1136+
}`, fakeS3.PresignedURLReturn)))
11641137
})
11651138
})
11661139
})

0 commit comments

Comments
 (0)