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

Function Test_apiRun_cache

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

Source from the content-addressed store, hash-verified

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