MCPcopy Create free account
hub / github.com/cli/cli / Test_apiRun_cache

Function Test_apiRun_cache

pkg/cmd/api/api_test.go:1528–1571  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1526}
1527
1528func Test_apiRun_cache(t *testing.T) {
1529 // Given we have a test server that spies on the number of requests it receives
1530 requestCount := 0
1531 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1532 requestCount++
1533 w.WriteHeader(http.StatusNoContent)
1534 }))
1535 t.Cleanup(s.Close)
1536
1537 ios, _, stdout, stderr := iostreams.Test()
1538 options := ApiOptions{
1539 IO: ios,
1540 Config: func() (gh.Config, error) {
1541 return &ghmock.ConfigMock{
1542 AuthenticationFunc: func() gh.AuthConfig {
1543 cfg := &config.AuthConfig{}
1544 // Required because the http client tries to get the active token and otherwise
1545 // this goes down to go-gh config and panics. Pretty bad solution, it would
1546 // be better if this were black box.
1547 cfg.SetActiveToken("token", "stub")
1548 return cfg
1549 },
1550 // Cached responses are stored in a tempdir that gets automatically cleaned up
1551 CacheDirFunc: func() string {
1552 return t.TempDir()
1553 },
1554 }, nil
1555 },
1556 // You might think that we want to set Host: s.URL here, but you'd be wrong.
1557 // The host field is later used to evaluate an API URL e.g. https://api.host.com/graphql
1558 // The RequestPath field is used exactly as is, for the request if it includes a host.
1559 RequestPath: s.URL,
1560 CacheTTL: time.Minute,
1561 }
1562
1563 // When we run the API behaviour twice
1564 require.NoError(t, apiRun(&options))
1565 require.NoError(t, apiRun(&options))
1566
1567 // We only get one request to the http server because it uses the cached response
1568 assert.Equal(t, 1, requestCount)
1569 assert.Equal(t, "", stdout.String(), "stdout")
1570 assert.Equal(t, "", stderr.String(), "stderr")
1571}
1572
1573func Test_apiRun_invokingAgent(t *testing.T) {
1574 var receivedUA string

Callers

nothing calls this directly

Calls 5

SetActiveTokenMethod · 0.95
TestFunction · 0.92
apiRunFunction · 0.85
EqualMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected