(t *testing.T)
| 1456 | } |
| 1457 | |
| 1458 | func Test_apiRun_cache(t *testing.T) { |
| 1459 | // Given we have a test server that spies on the number of requests it receives |
| 1460 | requestCount := 0 |
| 1461 | s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1462 | requestCount++ |
| 1463 | w.WriteHeader(http.StatusNoContent) |
| 1464 | })) |
| 1465 | t.Cleanup(s.Close) |
| 1466 | |
| 1467 | ios, _, stdout, stderr := iostreams.Test() |
| 1468 | options := ApiOptions{ |
| 1469 | IO: ios, |
| 1470 | Config: func() (gh.Config, error) { |
| 1471 | return &ghmock.ConfigMock{ |
| 1472 | AuthenticationFunc: func() gh.AuthConfig { |
| 1473 | cfg := &config.AuthConfig{} |
| 1474 | // Required because the http client tries to get the active token and otherwise |
| 1475 | // this goes down to go-gh config and panics. Pretty bad solution, it would |
| 1476 | // be better if this were black box. |
| 1477 | cfg.SetActiveToken("token", "stub") |
| 1478 | return cfg |
| 1479 | }, |
| 1480 | // Cached responses are stored in a tempdir that gets automatically cleaned up |
| 1481 | CacheDirFunc: func() string { |
| 1482 | return t.TempDir() |
| 1483 | }, |
| 1484 | }, nil |
| 1485 | }, |
| 1486 | // You might think that we want to set Host: s.URL here, but you'd be wrong. |
| 1487 | // The host field is later used to evaluate an API URL e.g. https://api.host.com/graphql |
| 1488 | // The RequestPath field is used exactly as is, for the request if it includes a host. |
| 1489 | RequestPath: s.URL, |
| 1490 | CacheTTL: time.Minute, |
| 1491 | } |
| 1492 | |
| 1493 | // When we run the API behaviour twice |
| 1494 | require.NoError(t, apiRun(&options)) |
| 1495 | require.NoError(t, apiRun(&options)) |
| 1496 | |
| 1497 | // We only get one request to the http server because it uses the cached response |
| 1498 | assert.Equal(t, 1, requestCount) |
| 1499 | assert.Equal(t, "", stdout.String(), "stdout") |
| 1500 | assert.Equal(t, "", stderr.String(), "stderr") |
| 1501 | } |
| 1502 | |
| 1503 | func Test_apiRun_invokingAgent(t *testing.T) { |
| 1504 | var receivedUA string |
nothing calls this directly
no test coverage detected