File: client_test.go

package info (click to toggle)
golang-github-influxdb-usage-client 0.0~git20151204.0.475977e-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 152 kB
  • sloc: makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,176 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package client_test

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/influxdata/usage-client/v1"
	"github.com/stretchr/testify/require"
)

type SimpleSaveable struct {
}

func (s SimpleSaveable) Path() string {
	return "/foo"
}

func Test_ClientSave(t *testing.T) {
	r := require.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
	}))
	defer ts.Close()

	c := client.New("")
	c.URL = ts.URL

	res, err := c.Save(SimpleSaveable{})
	r.NoError(err)
	r.Equal(200, res.StatusCode)
}

func Test_ClientSave_AuthHeaderSet(t *testing.T) {
	r := require.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.Header.Get("X-Authorization")))
	}))
	defer ts.Close()

	c := client.New("my-token")
	c.URL = ts.URL

	res, err := c.Save(SimpleSaveable{})
	r.NoError(err)
	r.Equal(200, res.StatusCode)

	b, _ := ioutil.ReadAll(res.Body)
	r.Equal("my-token", string(b))
}

func Test_ClientSave_500(t *testing.T) {
	r := require.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
		fmt.Fprint(w, `{"error":"oops!"}`)
	}))
	defer ts.Close()

	c := client.New("")
	c.URL = ts.URL

	res, err := c.Save(SimpleSaveable{})
	r.Equal(500, res.StatusCode)
	r.Error(err)
	r.Equal("oops!", err.Error())
}

func Test_ClientSave_422(t *testing.T) {
	r := require.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(422)
		fmt.Fprint(w, `
{
  "errors": {
    "cluster_id": [
      "ClusterID can not be blank."
    ],
    "host": [
      "Host can not be blank."
    ],
    "product": [
      "Product can not be blank."
    ],
    "server_id": [
      "ServerID can not be blank."
    ],
    "version": [
      "Version can not be blank."
    ]
  }
}
`)
	}))
	defer ts.Close()

	c := client.New("")
	c.URL = ts.URL

	res, err := c.Save(SimpleSaveable{})
	r.Equal(422, res.StatusCode)
	r.Error(err)

	ve := err.(client.ValidationErrors)
	r.Equal([]string{"Version can not be blank."}, ve.Errors["version"])
}