MCPcopy Index your code
hub / github.com/coder/coder / Test_wrapTransportWithVersionCheck

Function Test_wrapTransportWithVersionCheck

cli/root_internal_test.go:95–187  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

93}
94
95func Test_wrapTransportWithVersionCheck(t *testing.T) {
96 t.Parallel()
97
98 t.Run("NoOutput", func(t *testing.T) {
99 t.Parallel()
100 r := &RootCmd{}
101 cmd, err := r.Command(nil)
102 require.NoError(t, err)
103 var buf bytes.Buffer
104 inv := cmd.Invoke()
105 inv.Stderr = &buf
106 rt := wrapTransportWithVersionCheck(roundTripper(func(req *http.Request) (*http.Response, error) {
107 return &http.Response{
108 StatusCode: http.StatusOK,
109 Header: http.Header{
110 // Provider a version that will not match!
111 codersdk.BuildVersionHeader: []string{"v2.0.0"},
112 },
113 Body: io.NopCloser(nil),
114 }, nil
115 }), inv, "v2.0.0", nil)
116 req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
117 res, err := rt.RoundTrip(req)
118 require.NoError(t, err)
119 defer res.Body.Close()
120 require.Equal(t, "", buf.String())
121 })
122
123 t.Run("CustomUpgradeMessage", func(t *testing.T) {
124 t.Parallel()
125
126 r := &RootCmd{}
127
128 cmd, err := r.Command(nil)
129 require.NoError(t, err)
130
131 var buf bytes.Buffer
132 inv := cmd.Invoke()
133 inv.Stderr = &buf
134 expectedUpgradeMessage := "My custom upgrade message"
135 rt := wrapTransportWithVersionCheck(roundTripper(func(req *http.Request) (*http.Response, error) {
136 return &http.Response{
137 StatusCode: http.StatusOK,
138 Header: http.Header{
139 // Provider a version that will not match!
140 codersdk.BuildVersionHeader: []string{"v1.0.0"},
141 },
142 Body: io.NopCloser(nil),
143 }, nil
144 }), inv, "v2.0.0", func(ctx context.Context) (codersdk.BuildInfoResponse, error) {
145 return codersdk.BuildInfoResponse{
146 UpgradeMessage: expectedUpgradeMessage,
147 }, nil
148 })
149 req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
150 res, err := rt.RoundTrip(req)
151 require.NoError(t, err)
152 defer res.Body.Close()

Callers

nothing calls this directly

Calls 10

CommandMethod · 0.95
roundTripperFuncType · 0.70
RunMethod · 0.65
CloseMethod · 0.65
InvokeMethod · 0.45
RoundTripMethod · 0.45
EqualMethod · 0.45
StringMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected