aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/util/http_test.go
blob: ccb809d198b4f2028a7676ea56b5bee1c9f59dc4 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Basic testing of our HTTP client wrapper
// Author: bratseth

package util

import (
	"bytes"
	"crypto/tls"
	"io/ioutil"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

type mockHttpClient struct{}

func (c mockHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) {
	var status int
	var body string
	if request.URL.String() == "http://host/okpath" {
		status = 200
		body = "OK body"
	} else {
		status = 500
		body = "Unexpected url body"
	}

	return &http.Response{
			StatusCode: status,
			Header:     make(http.Header),
			Body:       ioutil.NopCloser(bytes.NewBufferString(body)),
		},
		nil
}

func (c mockHttpClient) UseCertificate(certificates []tls.Certificate) {}

func TestHttpRequest(t *testing.T) {
	ActiveHttpClient = mockHttpClient{}

	req, err := http.NewRequest("GET", "http://host/okpath", nil)
	assert.Nil(t, err)
	response, err := HttpDo(req, time.Second*10, "description")
	assert.Nil(t, err)
	assert.Equal(t, 200, response.StatusCode)

	req, err = http.NewRequest("GET", "http://host/otherpath", nil)
	assert.Nil(t, err)
	response, err = HttpDo(req, time.Second*10, "description")
	assert.Nil(t, err)
	assert.Equal(t, 500, response.StatusCode)
}