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 Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// A helper for testing commands
// Author: bratseth
package cmd
import (
"bytes"
"github.com/vespa-engine/vespa/utils"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"
"time"
)
func executeCommand(t *testing.T, client *mockHttpClient, args []string, moreArgs []string) (standardout string) {
utils.ActiveHttpClient = client
// Reset - persistent flags in Cobra persists over tests
rootCmd.SetArgs([]string{"status", "-t", ""})
rootCmd.Execute()
b := bytes.NewBufferString("")
utils.Out = b
rootCmd.SetArgs(append(args, moreArgs...))
rootCmd.Execute()
out, err := ioutil.ReadAll(b)
assert.Empty(t, err, "No error")
return string(out)
}
type mockHttpClient struct {
// The HTTP status code that will be returned from the next invocation. Default: 200
nextStatus int
// The response body code that will be returned from the next invocation. Default: ""
nextBody string
// A recording of the last HTTP request made through this
lastRequest *http.Request
}
func (c *mockHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) {
if c.nextStatus == 0 {
c.nextStatus = 200
}
c.lastRequest = request
return &http.Response{
StatusCode: c.nextStatus,
Body: ioutil.NopCloser(bytes.NewBufferString(c.nextBody)),
Header: make(http.Header),
},
nil
}
|