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

Function NewWithAPI

coderd/coderdtest/coderdtest.go:657–684  ·  view source on GitHub ↗

NewWithAPI constructs an in-memory API instance and returns a client to talk to it. Most tests never need a reference to the API, but AuthorizationTest in this module uses it. Do not expose the API or wrath shall descend upon thee.

(t testing.TB, options *Options)

Source from the content-addressed store, hash-verified

655// Most tests never need a reference to the API, but AuthorizationTest in this module uses it.
656// Do not expose the API or wrath shall descend upon thee.
657func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *coderd.API) {
658 if options == nil {
659 options = &Options{}
660 }
661 setHandler, cancelFunc, serverURL, newOptions := NewOptions(t, options)
662 // We set the handler after server creation for the access URL.
663 coderAPI := coderd.New(newOptions)
664 rootHandler := coderAPI.RootHandler
665 if options.APIMiddleware != nil {
666 r := chi.NewRouter()
667 r.Use(options.APIMiddleware)
668 r.Mount("/", rootHandler)
669 rootHandler = r
670 }
671 setHandler(rootHandler)
672 var provisionerCloser io.Closer = nopcloser{}
673 if options.IncludeProvisionerDaemon {
674 provisionerCloser = NewTaggedProvisionerDaemon(t, coderAPI, defaultTestDaemonName, options.ProvisionerDaemonTags, coderd.MemoryProvisionerWithVersionOverride(options.ProvisionerDaemonVersion))
675 }
676 client := codersdk.New(serverURL, codersdk.WithHTTPClient(NewIsolatedHTTPClient(serverURL)))
677 t.Cleanup(func() {
678 cancelFunc()
679 _ = provisionerCloser.Close()
680 _ = coderAPI.Close()
681 client.HTTPClient.CloseIdleConnections()
682 })
683 return client, provisionerCloser, coderAPI
684}
685
686// NewIsolatedHTTPClient returns a test client with its own transport.
687// Closing idle connections at test cleanup must not close http.DefaultTransport

Calls 10

NewFunction · 0.92
NewFunction · 0.92
WithHTTPClientFunction · 0.92
NewOptionsFunction · 0.85
NewIsolatedHTTPClientFunction · 0.85
CleanupMethod · 0.65
CloseMethod · 0.65
CloseIdleConnectionsMethod · 0.65