|
| 1 | +// Copyright 2026 NetApp, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +package api |
| 4 | + |
| 5 | +import ( |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "sync/atomic" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + |
| 14 | + tridentconfig "github.com/netapp/trident/config" |
| 15 | +) |
| 16 | + |
| 17 | +// roundTripFunc adapts a function into an http.RoundTripper for tests. |
| 18 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 19 | + |
| 20 | +func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } |
| 21 | + |
| 22 | +// TestClientAppHeaderValue verifies the computed header value matches the |
| 23 | +// documented format "Trident/<OrchestratorVersion.ShortString()>". |
| 24 | +func TestClientAppHeaderValue(t *testing.T) { |
| 25 | + want := "Trident/" + tridentconfig.OrchestratorVersion.ShortString() |
| 26 | + assert.Equal(t, want, ClientAppHeaderValue()) |
| 27 | + assert.True(t, strings.HasPrefix(ClientAppHeaderValue(), "Trident/"), |
| 28 | + "header value must start with 'Trident/'") |
| 29 | +} |
| 30 | + |
| 31 | +// TestClientAppHeaderName locks in the header name required by ONTAP. |
| 32 | +func TestClientAppHeaderName(t *testing.T) { |
| 33 | + assert.Equal(t, "X-Dot-Client-App", ClientAppHeader) |
| 34 | +} |
| 35 | + |
| 36 | +// TestClientAppTransport_RoundTrip verifies the transport stamps the header |
| 37 | +// on every outgoing request. |
| 38 | +func TestClientAppTransport_RoundTrip(t *testing.T) { |
| 39 | + const want = "Trident/26.06.0" |
| 40 | + |
| 41 | + var seen atomic.Int32 |
| 42 | + base := roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 43 | + seen.Add(1) |
| 44 | + assert.Equal(t, want, r.Header.Get("X-Dot-Client-App")) |
| 45 | + return &http.Response{ |
| 46 | + StatusCode: http.StatusOK, |
| 47 | + Body: io.NopCloser(strings.NewReader("")), |
| 48 | + Header: make(http.Header), |
| 49 | + }, nil |
| 50 | + }) |
| 51 | + |
| 52 | + tr := NewClientAppTransport(base, want) |
| 53 | + |
| 54 | + for i := 0; i < 3; i++ { |
| 55 | + req, err := http.NewRequest(http.MethodGet, "http://example.invalid/api", nil) |
| 56 | + assert.NoError(t, err) |
| 57 | + resp, err := tr.RoundTrip(req) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.NotNil(t, resp) |
| 60 | + _ = resp.Body.Close() |
| 61 | + |
| 62 | + // The caller's original request must not be mutated (RoundTripper contract). |
| 63 | + assert.Empty(t, req.Header.Get("X-Dot-Client-App"), |
| 64 | + "caller's request header must not be mutated") |
| 65 | + } |
| 66 | + |
| 67 | + assert.Equal(t, int32(3), seen.Load(), "base transport should be called once per RoundTrip") |
| 68 | +} |
| 69 | + |
| 70 | +// TestClientAppTransport_OverwritesExistingHeader verifies a pre-set header |
| 71 | +// on the incoming request is overwritten with our value (without mutating the |
| 72 | +// caller's request object). |
| 73 | +func TestClientAppTransport_OverwritesExistingHeader(t *testing.T) { |
| 74 | + const want = "Trident/26.06.1" |
| 75 | + |
| 76 | + base := roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 77 | + assert.Equal(t, want, r.Header.Get("X-Dot-Client-App")) |
| 78 | + return &http.Response{ |
| 79 | + StatusCode: http.StatusOK, |
| 80 | + Body: io.NopCloser(strings.NewReader("")), |
| 81 | + Header: make(http.Header), |
| 82 | + }, nil |
| 83 | + }) |
| 84 | + |
| 85 | + tr := NewClientAppTransport(base, want) |
| 86 | + req, err := http.NewRequest(http.MethodGet, "http://example.invalid/api", nil) |
| 87 | + assert.NoError(t, err) |
| 88 | + req.Header.Set("X-Dot-Client-App", "SomeOther/1.0") |
| 89 | + |
| 90 | + resp, err := tr.RoundTrip(req) |
| 91 | + assert.NoError(t, err) |
| 92 | + _ = resp.Body.Close() |
| 93 | + |
| 94 | + assert.Equal(t, "SomeOther/1.0", req.Header.Get("X-Dot-Client-App"), |
| 95 | + "caller's request header must be left alone") |
| 96 | +} |
0 commit comments