/
artifacts_test_common.go
43 lines (38 loc) · 1.3 KB
/
artifacts_test_common.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
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)
// PrepareGetArtifacts only for test
func PrepareGetArtifacts(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd,
jobName string, buildID int) (response *http.Response) {
path := ParseJobPath(jobName)
var api string
if buildID <= 0 {
api = fmt.Sprintf("%s/lastBuild/wfapi/artifacts", path)
} else {
api = fmt.Sprintf("%s/%d/wfapi/artifacts", path, buildID)
}
request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", rootURL, api), nil)
response = &http.Response{
StatusCode: 200,
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`[{"id":"n1","name":"a.log","path":"a.log","url":"/job/pipeline/1/artifact/a.log","size":0}]`)),
}
roundTripper.EXPECT().
RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd)
}
return
}
// PrepareGetEmptyArtifacts only for test
func PrepareGetEmptyArtifacts(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd,
jobName string, buildID int) (response *http.Response) {
response = PrepareGetArtifacts(roundTripper, rootURL, user, passwd, jobName, buildID)
response.Body = ioutil.NopCloser(bytes.NewBufferString(`[]`))
return
}