aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/src/utils/http_test.go
blob: 0383f5c925772a64f0c7948d5f37b6b844a5c7c3 (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
// Copyright Verizon Media. 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 utils

import (
    "bytes"
    "github.com/stretchr/testify/assert"
	"io/ioutil"
    "net/http"
    "testing"
    "time"
)

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 TestHttpRequest(t *testing.T) {
    ActiveHttpClient = mockHttpClient{}

    response := HttpGet("http://host", "/okpath", "description")
    assert.Equal(t, 200, response.StatusCode)

    response = HttpGet("http://host", "/otherpath", "description")
    assert.Equal(t, 500, response.StatusCode)
}