forked from einride/balena-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalena_test.go
146 lines (133 loc) · 3.53 KB
/
balena_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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package balena
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"gotest.tools/v3/assert"
)
func TestNewRequest(t *testing.T) {
// given
c := New(nil, "")
inURL, outURL := "foo", defaultBaseURL+"foo"
inBody, outBody := &struct {
Message string
}{
Message: "hello world",
},
`{"Message":"hello world"}`+"\n"
// when
req, _ := c.NewRequest(context.Background(), http.MethodGet, inURL, "", inBody)
// then
// test relative URL was expanded
assert.Equal(t, req.URL.String(), outURL)
// test body was JSON encoded
body, _ := io.ReadAll(req.Body)
assert.Equal(t, string(body), outBody)
// test default user-agent is attached to the request
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("NewRequest() User-Agent = %v, expected %v", userAgent, c.UserAgent)
}
}
func TestNewRequest_badURL(t *testing.T) {
// given
c := New(nil, "")
// when
_, err := c.NewRequest(context.Background(), http.MethodGet, ":", "", nil)
// then
assert.Assert(t, err != nil)
}
func TestNewRequest_Query(t *testing.T) {
// given
c := New(nil, "")
// when
req, err := c.NewRequest(context.Background(), http.MethodGet, "foo", "key=value", nil)
// then
assert.NilError(t, err)
assert.Equal(t, "key=value", req.URL.RawQuery)
}
func TestDo(t *testing.T) {
// given
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := New(nil, "")
url, _ := url.Parse(server.URL + "/")
client.BaseURL = url
defer server.Close()
type foo struct {
A string
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if m := http.MethodGet; m != r.Method {
t.Errorf("Request method = %v, expected %v", r.Method, m)
}
fmt.Fprint(w, `{"A":"a"}`)
})
req, err := client.NewRequest(context.Background(), http.MethodGet, "/", "", nil)
assert.NilError(t, err)
body := &foo{}
// when
err = client.Do(req, body)
// then
assert.NilError(t, err)
expected := &foo{"a"}
assert.DeepEqual(t, expected, body)
}
func newFixture() (*Client, *http.ServeMux, func()) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := New(nil, "")
url, _ := url.Parse(server.URL + "/")
client.BaseURL = url
return client, mux, func() {
server.Close()
}
}
// supervisorV2Fixture returns a SupervisorService as if it was used locally inside balena.
// Caller is expected to set the following environmebt variables before
//
// BALENA_SUPERVISOR_API_KEY
// BALENA_APP_ID
// BALENA_DEVICE_UUID.
func supervisorV2Fixture(t *testing.T) (*SupervisorV2Service, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client, err := NewSupervisorV2(nil)
assert.NilError(t, err)
url, _ := url.Parse(server.URL + "/")
client.client.BaseURL = url
t.Cleanup(func() {
server.Close()
})
return client, mux
}
// supervisorV1Fixture returns a SupervisorService as if it was used locally inside balena.
// Caller is expected to set the following environmebt variables before
//
// BALENA_SUPERVISOR_API_KEY
// BALENA_APP_ID
// BALENA_DEVICE_UUID.
func supervisorV1Fixture(t *testing.T) (*SupervisorV1Service, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client, err := NewSupervisorV1(nil)
assert.NilError(t, err)
url, _ := url.Parse(server.URL + "/")
client.client.BaseURL = url
t.Cleanup(func() {
server.Close()
})
return client, mux
}
func testMethod(t *testing.T, r *http.Request, expected string) {
t.Helper()
if expected != r.Method {
t.Errorf("Request method = %v, expected %v", r.Method, expected)
}
}