-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapp_test.go
More file actions
73 lines (67 loc) · 1.5 KB
/
Copy pathapp_test.go
File metadata and controls
73 lines (67 loc) · 1.5 KB
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
package gomeassistant
import (
"context"
stdhttp "net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewAppDoesNotPerformNetworkIO(t *testing.T) {
var requests atomic.Int32
server := httptest.NewServer(stdhttp.HandlerFunc(func(stdhttp.ResponseWriter, *stdhttp.Request) {
requests.Add(1)
}))
defer server.Close()
app, err := NewApp(context.Background(), NewAppRequest{
URL: server.URL,
HAAuthToken: "token",
})
require.NoError(t, err)
require.NotNil(t, app)
require.Nil(t, app.conn)
require.Equal(t, int32(0), requests.Load())
require.Equal(t, server.URL, app.baseURL.String())
require.Equal(t, "token", app.authToken)
require.Equal(t, "zone.home", app.homeZoneEntityID)
}
func TestNewAppRejectsInvalidArguments(t *testing.T) {
tests := []struct {
name string
request NewAppRequest
}{
{
name: "missing URL",
request: NewAppRequest{
HAAuthToken: "token",
},
},
{
name: "missing token",
request: NewAppRequest{
URL: "http://example.com",
},
},
{
name: "malformed URL",
request: NewAppRequest{
URL: "://",
HAAuthToken: "token",
},
},
{
name: "unsupported URL scheme",
request: NewAppRequest{
URL: "ws://example.com",
HAAuthToken: "token",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app, err := NewApp(context.Background(), tt.request)
require.ErrorIs(t, err, ErrInvalidArgs)
require.Nil(t, app)
})
}
}