(t *testing.T)
| 93 | } |
| 94 | |
| 95 | func 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() |
nothing calls this directly
no test coverage detected