コード例 #1
0
ファイル: client.go プロジェクト: dieucao/notifications
func (client Client) makeRequest(requestArgs requestArguments) (int, []byte, error) {
	if requestArgs.AcceptableStatusCodes == nil {
		panic("No acceptable status codes were assigned in the request arguments")
	}

	jsonBody, err := json.Marshal(requestArgs.Body)
	if err != nil {
		return 0, []byte{}, newRequestBodyMarshalError(err)
	}

	requestURL, err := url.Parse(client.Config.Host)
	if err != nil {
		return 0, []byte{}, newRequestConfigurationError(err)
	}

	requestURL.Path = requestArgs.Path
	requestURL.RawQuery = requestArgs.Query.Encode()

	request, err := http.NewRequest(requestArgs.Method, requestURL.String(), bytes.NewBuffer(jsonBody))
	if err != nil {
		return 0, []byte{}, newRequestConfigurationError(err)
	}

	request.Header.Set("Authorization", "Bearer "+requestArgs.Token)

	client.printRequest(request)

	networkClient := network.GetClient(network.Config{SkipVerifySSL: client.Config.SkipVerifySSL})
	response, err := networkClient.Do(request)
	if err != nil {
		return 0, []byte{}, newRequestHTTPError(err)
	}

	responseBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return 0, []byte{}, newResponseReadError(err)
	}

	client.printResponse(response.StatusCode, responseBody)

	if response.StatusCode == 404 {
		return 0, []byte{}, newNotFoundError(responseBody)
	}

	if response.StatusCode == 401 {
		return 0, []byte{}, newUnauthorizedError(responseBody)
	}

	for _, acceptableCode := range requestArgs.AcceptableStatusCodes {
		if response.StatusCode == acceptableCode {
			return response.StatusCode, responseBody, nil
		}
	}

	return response.StatusCode, responseBody, newUnexpectedStatusError(response.StatusCode, responseBody)
}
コード例 #2
0
ファイル: client_test.go プロジェクト: dieucao/notifications
	"reflect"
	"time"

	"github.com/pivotal-cf-experimental/rainmaker/internal/network"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("using a global HTTP client", func() {
	BeforeEach(func() {
		network.ClearHTTPClient()
	})

	It("retrieves the exact same client reference", func() {
		client1 := network.GetClient(network.Config{})
		client2 := network.GetClient(network.Config{})

		Expect(client1).To(BeAssignableToTypeOf(&http.Client{}))
		Expect(client1).ToNot(BeNil())
		Expect(reflect.ValueOf(client1).Pointer()).To(Equal(reflect.ValueOf(client2).Pointer()))

	})

	It("uses the configuration to configure the HTTP client", func() {
		config := network.Config{
			SkipVerifySSL: true,
		}
		transport := network.GetClient(config).Transport.(*http.Transport)

		Expect(transport.TLSClientConfig.InsecureSkipVerify).To(BeTrue())