http_test.go 4.5 KB
Newer Older
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
package util

import (
	"bytes"
	"fmt"
	"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("http test", func() {
	var (
		ctrl           *gomock.Controller
		roundTripper   *mhttp.MockRoundTripper
		downloader     HTTPDownloader
		targetFilePath string
		responseBody   string
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
		targetFilePath = "test.log"
		downloader = HTTPDownloader{
			TargetFilePath: targetFilePath,
			RoundTripper:   roundTripper,
		}
		responseBody = "fake body"
	})

	AfterEach(func() {
		os.Remove(targetFilePath)
		ctrl.Finish()
	})

42
	Context("SetProxy", func() {
43
		It("basic test", func() {
44 45 46 47 48 49 50 51
			proxy, proxyAuth := "http://localhost", "admin:admin"

			tr := &http.Transport{}
			err := SetProxy(proxy, proxyAuth, tr)
			Expect(err).To(BeNil())
			Expect(tr.ProxyConnectHeader.Get("Proxy-Authorization")).To(Equal("Basic YWRtaW46YWRtaW4="))
		})

52
		It("empty proxy", func() {
53 54 55 56 57
			err := SetProxy("", "", nil)
			Expect(err).To(BeNil())
		})
	})

58 59 60 61 62 63 64 65 66 67 68
	Context("DownloadFile", func() {
		It("no progress indication", func() {
			request, _ := http.NewRequest("GET", "", nil)
			response := &http.Response{
				StatusCode: 200,
				Proto:      "HTTP/1.1",
				Header:     http.Header{},
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString(responseBody)),
			}
			roundTripper.EXPECT().
69
				RoundTrip((request)).Return(response, nil)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
			err := downloader.DownloadFile()
			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))
			return
		})

		It("with BasicAuth", func() {
			downloader = HTTPDownloader{
				TargetFilePath: targetFilePath,
				RoundTripper:   roundTripper,
				UserName:       "UserName",
				Password:       "Password",
			}

			request, _ := http.NewRequest("GET", "", nil)
			request.SetBasicAuth(downloader.UserName, downloader.Password)
			response := &http.Response{
				StatusCode: 200,
				Proto:      "HTTP/1.1",
				Header:     http.Header{},
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString(responseBody)),
			}
			roundTripper.EXPECT().
100
				RoundTrip((request)).Return(response, nil)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
			err := downloader.DownloadFile()
			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))
			return
		})

		It("with error request", func() {
			downloader = HTTPDownloader{
				URL: "fake url",
			}
			err := downloader.DownloadFile()
			Expect(err).To(HaveOccurred())
		})

		It("with error response", func() {
			downloader = HTTPDownloader{
				RoundTripper: roundTripper,
			}

			request, _ := http.NewRequest("GET", "", nil)
			response := &http.Response{}
			roundTripper.EXPECT().
129
				RoundTrip((request)).Return(response, fmt.Errorf("fake error"))
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
			err := downloader.DownloadFile()
			Expect(err).To(HaveOccurred())
		})

		It("status code isn't 200", func() {
			downloader = HTTPDownloader{
				RoundTripper: roundTripper,
				Debug:        true,
			}

			request, _ := http.NewRequest("GET", "", nil)
			response := &http.Response{
				StatusCode: 400,
				Proto:      "HTTP/1.1",
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString(responseBody)),
			}
			roundTripper.EXPECT().
148
				RoundTrip((request)).Return(response, nil)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
			err := downloader.DownloadFile()
			Expect(err).To(HaveOccurred())

			const debugFile = "debug-download.html"

			_, err = os.Stat(debugFile)
			Expect(err).To(BeNil())

			content, readErr := ioutil.ReadFile(debugFile)
			Expect(readErr).To(BeNil())
			Expect(string(content)).To(Equal(responseBody))

			defer os.Remove(debugFile)
		})

		It("showProgress", func() {
			downloader = HTTPDownloader{
				RoundTripper:   roundTripper,
				ShowProgress:   true,
				TargetFilePath: targetFilePath,
			}

			request, _ := http.NewRequest("GET", "", nil)
			response := &http.Response{
				StatusCode: 200,
				Proto:      "HTTP/1.1",
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString(responseBody)),
			}
			roundTripper.EXPECT().
179
				RoundTrip((request)).Return(response, nil)
180 181 182 183 184
			err := downloader.DownloadFile()
			Expect(err).To(BeNil())
		})
	})
})