-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
68 lines (53 loc) · 1.68 KB
/
client_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
package client
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata"
)
func Test_PomeriumAuthCredentials(t *testing.T) {
token := "mysecuretesttoken"
creds := NewPomeriumAuthCredentials(token)
meta, err := creds.GetRequestMetadata(context.Background(), "/something")
require.NoError(t, err)
authHeader, ok := meta["authorization"]
assert.True(t, ok)
assert.Equal(t, authHeader, AuthorizationHeader(token))
}
func Test_dial(t *testing.T) {
token := "mysecuretesttoken"
srv := grpc.NewServer()
h := httptest.NewUnstartedServer(srv)
h.EnableHTTP2 = true
h.StartTLS()
defer h.Close()
hURL, err := url.Parse(h.URL)
require.NoError(t, err)
hCA := x509.NewCertPool()
hCA.AddCert(h.Certificate())
testService := &testServiceServer{}
srv.RegisterService(&grpc_testing.TestService_ServiceDesc, testService)
ctx := context.Background()
conn, err := dial(ctx, hURL.Host, token, WithTlsConfig(&tls.Config{RootCAs: hCA}))
require.NoError(t, err)
defer conn.Close()
client := grpc_testing.NewTestServiceClient(conn)
_, err = client.EmptyCall(context.Background(), &grpc_testing.Empty{})
assert.Contains(t, testService.md.Get("authorization"), AuthorizationHeader(token))
assert.NoError(t, err)
}
type testServiceServer struct {
grpc_testing.TestServiceServer
md metadata.MD
}
func (t *testServiceServer) EmptyCall(ctx context.Context, msg *grpc_testing.Empty) (*grpc_testing.Empty, error) {
t.md, _ = metadata.FromIncomingContext(ctx)
return &grpc_testing.Empty{}, nil
}