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)
| 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. |
| 657 | func 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 |