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

Function TestDERPHeaders

cli/root_test.go:202–293  ·  view source on GitHub ↗

TestDERPHeaders ensures that the client sends the global `--header`s and `--header-command` to the DERP server when connecting.

(t *testing.T)

Source from the content-addressed store, hash-verified

200// TestDERPHeaders ensures that the client sends the global `--header`s and
201// `--header-command` to the DERP server when connecting.
202func TestDERPHeaders(t *testing.T) {
203 t.Parallel()
204
205 // Create a coderd API instance the hard way since we need to change the
206 // handler to inject our custom /derp handler.
207 dv := coderdtest.DeploymentValues(t)
208 dv.DERP.Config.BlockDirect = true
209 setHandler, cancelFunc, serverURL, newOptions := coderdtest.NewOptions(t, &coderdtest.Options{
210 DeploymentValues: dv,
211 })
212
213 // We set the handler after server creation for the access URL.
214 coderAPI := coderd.New(newOptions)
215 setHandler(coderAPI.RootHandler)
216 provisionerCloser := coderdtest.NewProvisionerDaemon(t, coderAPI)
217 t.Cleanup(func() {
218 _ = provisionerCloser.Close()
219 })
220 client := codersdk.New(serverURL, codersdk.WithHTTPClient(coderdtest.NewIsolatedHTTPClient(serverURL)))
221 t.Cleanup(func() {
222 cancelFunc()
223 _ = provisionerCloser.Close()
224 _ = coderAPI.Close()
225 client.HTTPClient.CloseIdleConnections()
226 })
227
228 var (
229 admin = coderdtest.CreateFirstUser(t, client)
230 member, memberUser = coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
231 workspace = runAgent(t, client, memberUser.ID, newOptions.Database)
232 )
233
234 // Inject custom /derp handler so we can inspect the headers.
235 var (
236 expectedHeaders = map[string]string{
237 "X-Test-Header": "test-value",
238 "Cool-Header": "Dean was Here!",
239 "X-Process-Testing": "very-wow",
240 }
241 derpCalled atomic.Int64
242 )
243 setHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
244 if strings.HasPrefix(r.URL.Path, "/derp") {
245 ok := true
246 for k, v := range expectedHeaders {
247 if r.Header.Get(k) != v {
248 ok = false
249 break
250 }
251 }
252 if ok {
253 // Only increment if all the headers are set, because the agent
254 // calls derp also.
255 derpCalled.Add(1)
256 }
257 }
258
259 coderAPI.RootHandler.ServeHTTP(w, r)

Callers

nothing calls this directly

Calls 15

DeploymentValuesFunction · 0.92
NewOptionsFunction · 0.92
NewFunction · 0.92
NewProvisionerDaemonFunction · 0.92
NewFunction · 0.92
WithHTTPClientFunction · 0.92
NewIsolatedHTTPClientFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
NewFunction · 0.92
SetupConfigFunction · 0.92
NewFunction · 0.92

Tested by

no test coverage detected