-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_common_test.go
91 lines (79 loc) · 2.93 KB
/
client_common_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
package fireboltgosdk
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func testProtocolVersion(t *testing.T, clientFactory func(string) Client) {
var protocolVersionValue = ""
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
protocolVersionValue = r.Header.Get(protocolVersionHeader)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
client := clientFactory(server.URL)
_, _ = client.Query(context.TODO(), server.URL, "SELECT 1", map[string]string{}, connectionControl{})
if protocolVersionValue != protocolVersion {
t.Errorf("Did not set Protocol-Version value correctly on a query request")
}
}
func testUpdateParameters(t *testing.T, clientFactory func(string) Client) {
var newDatabaseName = "new_database"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == ServiceAccountLoginURLSuffix {
_, _ = w.Write(getAuthResponse(10000))
} else if r.URL.Path == UsernamePasswordURLSuffix {
_, _ = w.Write(getAuthResponseV0(10000))
} else {
w.Header().Set(updateParametersHeader, fmt.Sprintf("%s=%s", "database", newDatabaseName))
w.WriteHeader(http.StatusOK)
}
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
client := clientFactory(server.URL)
params := map[string]string{
"database": "db",
}
_, err := client.Query(context.TODO(), server.URL, "SELECT 1", params, connectionControl{
updateParameters: func(key, value string) {
params[key] = value
},
})
if err != nil {
t.Errorf("Error during query execution with update parameters header in response %s", err)
}
if params["database"] != newDatabaseName {
t.Errorf("Database is not set correctly. Expected %s but was %s", newDatabaseName, params["database"])
}
}
func testAdditionalHeaders(t *testing.T, clientFactory func(string) Client) {
// Test that additional headers, passed in ctx are respected
var additionalHeaders = map[string]string{
"Firebolt-Test-Header": "test",
"Ignored-Header": "ignored",
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == ServiceAccountLoginURLSuffix {
_, _ = w.Write(getAuthResponse(10000))
} else if r.URL.Path == UsernamePasswordURLSuffix {
_, _ = w.Write(getAuthResponseV0(10000))
} else {
if r.Header.Get("Firebolt-Test-Header") != "test" {
t.Errorf("Did not set Firebolt-Test-Header value when passed in ctx")
}
if r.Header.Get("Ignored-Header") != "" {
t.Errorf("Did not ignore Ignored-Header value when passed in ctx")
}
w.WriteHeader(http.StatusOK)
}
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
client := clientFactory(server.URL)
ctx := context.WithValue(context.TODO(), ContextKey("additionalHeaders"), additionalHeaders)
_, _ = client.Query(ctx, server.URL, "SELECT 1", map[string]string{}, connectionControl{})
}