-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
55 lines (42 loc) · 1.31 KB
/
request_test.go
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
package uiiclient
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_defaultRequest(t *testing.T) {
assert := assert.New(t)
t.Run("success", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("success"))
}))
b, err := c.defaultRequest(http.MethodGet, server.URL, nil)
assert.NoError(err)
assert.Equal("success", string(b))
})
t.Run("failure-no-body", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
_, err = c.defaultRequest(http.MethodGet, server.URL, nil)
assert.Error(err)
})
t.Run("failure-status-code", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
b, err := json.Marshal(UIIError{Errors: []string{"some error"}})
assert.NoError(err)
_, _ = w.Write(b)
}))
_, err = c.defaultRequest(http.MethodGet, server.URL, nil)
assert.Error(err)
})
}