Debian Codegraph @

auth/internal/transport/headers/headers_test.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package headers
16
17 import (
18 "net/http/httptest"
19 "testing"
20
21 "cloud.google.com/go/auth"
22 "cloud.google.com/go/auth/internal"
23 "github.com/google/go-cmp/cmp"
24 )
25
26 func TestSetAuth(t *testing.T) {
27 tests := []struct {
28 name string
29 baseToken *auth.Token
30 tbd *internal.TrustBoundaryData
31 wantAuthHeader string
32 wantTBHeader bool
33 wantTBHeaderVal string
34 wantGRPCMeta map[string]string
35 }{
36 {
37 name: "auth only",
38 baseToken: &auth.Token{Value: "token_val", Type: "Bearer"},
39 wantAuthHeader: "Bearer token_val",
40 wantTBHeader: false,
41 wantGRPCMeta: map[string]string{"authorization": "Bearer token_val"},
42 },
43 {
44 name: "auth with empty tbd",
45 baseToken: &auth.Token{
46 Value: "token_val",
47 Type: "Bearer",
48 },
49 tbd: &internal.TrustBoundaryData{},
50 wantAuthHeader: "Bearer token_val",
51 wantTBHeader: false,
52 wantGRPCMeta: map[string]string{"authorization": "Bearer token_val"},
53 },
54 {
55 name: "auth with no-op tbd",
56 baseToken: &auth.Token{
57 Value: "token_val",
58 Type: "Bearer",
59 },
60 tbd: internal.NewNoOpTrustBoundaryData(),
61 wantAuthHeader: "Bearer token_val",
62 wantTBHeader: true,
63 wantTBHeaderVal: "",
64 wantGRPCMeta: map[string]string{"authorization": "Bearer token_val", "x-allowed-locations": ""},
65 },
66 {
67 name: "auth with tbd",
68 baseToken: &auth.Token{
69 Value: "token_val",
70 Type: "Bearer",
71 },
72 tbd: internal.NewTrustBoundaryData(nil, "some_value"),
73 wantAuthHeader: "Bearer token_val",
74 wantTBHeader: true,
75 wantTBHeaderVal: "some_value",
76 wantGRPCMeta: map[string]string{"authorization": "Bearer token_val", "x-allowed-locations": "some_value"},
77 },
78 }
79 for _, tt := range tests {
80 t.Run(tt.name, func(t *testing.T) {
81 token := &auth.Token{
82 Value: tt.baseToken.Value,
83 Type: tt.baseToken.Type,
84 }
85 if tt.tbd != nil {
86 token.Metadata = map[string]interface{}{internal.TrustBoundaryDataKey: *tt.tbd}
87 }
88 // Test HTTP
89 req := httptest.NewRequest("GET", "/", nil)
90 SetAuthHeader(token, req)
91 if got := req.Header.Get("Authorization"); got != tt.wantAuthHeader {
92 t.Errorf("Authorization header: got %q, want %q", got, tt.wantAuthHeader)
93 }
94 if got, ok := req.Header["X-Allowed-Locations"]; ok != tt.wantTBHeader || (ok && got[0] != tt.wantTBHeaderVal) {
95 t.Errorf("X-Allowed-Locations header: got %q, want %q (present: %v)", got, tt.wantTBHeaderVal, tt.wantTBHeader)
96 }
97
98 // Test gRPC
99 gotMeta := make(map[string]string)
100 SetAuthMetadata(token, gotMeta)
101 if diff := cmp.Diff(tt.wantGRPCMeta, gotMeta); diff != "" {
102 t.Errorf("gRPC metadata mismatch (-want +got):\n%s", diff)
103 }
104 })
105 }
106 }