(t *testing.T)
| 1424 | } |
| 1425 | |
| 1426 | func Test_apiRun_cache(t *testing.T) { |
| 1427 | // Given we have a test server that spies on the number of requests it receives |
| 1428 | requestCount := 0 |
| 1429 | s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1430 | requestCount++ |
| 1431 | w.WriteHeader(http.StatusNoContent) |
| 1432 | })) |
| 1433 | t.Cleanup(s.Close) |
| 1434 | |
| 1435 | ios, _, stdout, stderr := iostreams.Test() |
| 1436 | options := ApiOptions{ |
| 1437 | IO: ios, |
| 1438 | Config: func() (gh.Config, error) { |
| 1439 | return &ghmock.ConfigMock{ |
| 1440 | AuthenticationFunc: func() gh.AuthConfig { |
| 1441 | cfg := &config.AuthConfig{} |
| 1442 | // Required because the http client tries to get the active token and otherwise |
| 1443 | // this goes down to go-gh config and panics. Pretty bad solution, it would |
| 1444 | // be better if this were black box. |
| 1445 | cfg.SetActiveToken("token", "stub") |
| 1446 | return cfg |
| 1447 | }, |
| 1448 | // Cached responses are stored in a tempdir that gets automatically cleaned up |
| 1449 | CacheDirFunc: func() string { |
| 1450 | return t.TempDir() |
| 1451 | }, |
| 1452 | }, nil |
| 1453 | }, |
| 1454 | // You might think that we want to set Host: s.URL here, but you'd be wrong. |
| 1455 | // The host field is later used to evaluate an API URL e.g. https://api.host.com/graphql |
| 1456 | // The RequestPath field is used exactly as is, for the request if it includes a host. |
| 1457 | RequestPath: s.URL, |
| 1458 | CacheTTL: time.Minute, |
| 1459 | } |
| 1460 | |
| 1461 | // When we run the API behaviour twice |
| 1462 | require.NoError(t, apiRun(&options)) |
| 1463 | require.NoError(t, apiRun(&options)) |
| 1464 | |
| 1465 | // We only get one request to the http server because it uses the cached response |
| 1466 | assert.Equal(t, 1, requestCount) |
| 1467 | assert.Equal(t, "", stdout.String(), "stdout") |
| 1468 | assert.Equal(t, "", stderr.String(), "stderr") |
| 1469 | } |
| 1470 | |
| 1471 | func Test_apiRun_invokingAgent(t *testing.T) { |
| 1472 | var receivedUA string |
nothing calls this directly
no test coverage detected