/
center_download_test.go
65 lines (54 loc) · 1.5 KB
/
center_download_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)
var _ = Describe("center download command", func() {
var (
ctrl *gomock.Controller
roundTripper *mhttp.MockRoundTripper
targetFilePath string
responseBody string
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
roundTripper = mhttp.NewMockRoundTripper(ctrl)
targetFilePath = "jenkins.war"
responseBody = "fake response"
})
AfterEach(func() {
rootCmd.SetArgs([]string{})
os.Remove(targetFilePath)
ctrl.Finish()
})
Context("basic cases", func() {
It("should success", func() {
centerDownloadOption.RoundTripper = roundTripper
centerDownloadOption.LTS = false
request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war/latest/jenkins.war", nil)
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Header: http.Header{},
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
}
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
rootCmd.SetArgs([]string{"center", "download"})
_, err := rootCmd.ExecuteC()
Expect(err).To(BeNil())
_, err = os.Stat(targetFilePath)
Expect(err).To(BeNil())
content, readErr := ioutil.ReadFile(targetFilePath)
Expect(readErr).To(BeNil())
Expect(string(content)).To(Equal(responseBody))
})
})
})