-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconnection_test.go
More file actions
121 lines (100 loc) · 2.65 KB
/
Copy pathconnection_test.go
File metadata and controls
121 lines (100 loc) · 2.65 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
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
package godatabend
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBuildDatabendConnInitializesViaLogin(t *testing.T) {
var (
mu sync.Mutex
loginCount int
logoutCount int
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/session/login":
mu.Lock()
loginCount++
mu.Unlock()
w.Header().Set(contentType, jsonMediaType)
w.Header().Set(DatabendSessionIDHeader, "session-login")
require.NoError(t, json.NewEncoder(w).Encode(LoginResponse{ServerMaxArrowResultVersion: ptrInt64(2)}))
case "/v1/session/logout":
mu.Lock()
logoutCount++
mu.Unlock()
w.WriteHeader(http.StatusOK)
default:
http.NotFound(w, r)
}
}))
defer server.Close()
cfg := testHTTPConfig(t, server.URL)
dc, err := buildDatabendConn(context.Background(), cfg)
require.NoError(t, err)
assert.Equal(t, "session-login", dc.rest.SessionID)
assert.True(t, dc.rest.httpArrowCapability())
assert.True(t, dc.rest.connectionInfoInitialized)
require.NoError(t, dc.Close())
mu.Lock()
defer mu.Unlock()
assert.Equal(t, 1, loginCount)
assert.Equal(t, 1, logoutCount)
}
func TestBuildDatabendConnDisablesArrowWhenLoginNotFound(t *testing.T) {
var (
mu sync.Mutex
loginCount int
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/session/login" {
http.NotFound(w, r)
return
}
mu.Lock()
loginCount++
mu.Unlock()
http.NotFound(w, r)
}))
defer server.Close()
cfg := testHTTPConfig(t, server.URL)
dc, err := buildDatabendConn(context.Background(), cfg)
require.NoError(t, err)
assert.False(t, dc.rest.httpArrowCapability())
assert.True(t, dc.rest.connectionInfoInitialized)
assert.False(t, dc.rest.sessionLoggedIn)
mu.Lock()
defer mu.Unlock()
assert.Equal(t, 1, loginCount)
}
func TestBuildDatabendConnSkipsLoginWhenDisabled(t *testing.T) {
var (
mu sync.Mutex
loginCount int
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/session/login" {
http.NotFound(w, r)
return
}
mu.Lock()
loginCount++
mu.Unlock()
http.NotFound(w, r)
}))
defer server.Close()
cfg := testHTTPConfig(t, server.URL)
cfg.LoginEnabled = false
dc, err := buildDatabendConn(context.Background(), cfg)
require.NoError(t, err)
assert.False(t, dc.rest.httpArrowCapability())
assert.True(t, dc.rest.connectionInfoInitialized)
mu.Lock()
defer mu.Unlock()
assert.Equal(t, 0, loginCount)
}