未验证 提交 dd065398 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Add user-agent into http request (#149)

* Add user-agent into http request

* Add test cases

* Add setup test go file
上级 f021c094
package app
import (
"testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("test-app.xml")
RunSpecsWithDefaultAndCustomReporters(t, "app", []Reporter{junitReporter})
}
package app
import "fmt"
var (
version string
commit string
)
// GetVersion returns the version
func GetVersion() string {
return version
}
// GetCommit returns the commit id
func GetCommit() string {
return commit
}
// GetCombinedVersion returns the version and commit id
func GetCombinedVersion() string {
return fmt.Sprintf("jcli; %s; %s", GetVersion(), GetCommit())
}
package app
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("version test", func() {
Context("basic function", func() {
It("shoud success", func() {
Expect(GetVersion()).To(Equal(""))
Expect(GetCommit()).To(Equal(""))
Expect(GetCombinedVersion()).NotTo(Equal(""))
})
})
})
......@@ -10,6 +10,8 @@ import (
"log"
"net/http"
"net/url"
"github.com/jenkins-zh/jenkins-cli/app"
)
// JenkinsCore core informations of Jenkins
......@@ -32,6 +34,7 @@ type JenkinsCrumb struct {
Crumb string
}
// GetClient get the default http Jenkins client
func (j *JenkinsCore) GetClient() (client *http.Client) {
var roundTripper http.RoundTripper
if j.RoundTripper != nil {
......@@ -59,6 +62,7 @@ func (j *JenkinsCore) GetClient() (client *http.Client) {
return
}
// ProxyHandle takes care of the proxy setting
func (j *JenkinsCore) ProxyHandle(request *http.Request) {
if j.ProxyAuth != "" {
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(j.ProxyAuth))
......@@ -66,13 +70,20 @@ func (j *JenkinsCore) ProxyHandle(request *http.Request) {
}
}
// AuthHandle takes care of the auth
func (j *JenkinsCore) AuthHandle(request *http.Request) (err error) {
if j.UserName != "" && j.Token != "" {
request.SetBasicAuth(j.UserName, j.Token)
}
// not add the User-Agent for tests
if j.RoundTripper == nil {
request.Header.Set("User-Agent", app.GetCombinedVersion())
}
j.ProxyHandle(request)
// all post request to Jenkins must be has the crumb
if request.Method == "POST" {
err = j.CrumbHandle(request)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册