| 727 | } |
| 728 | |
| 729 | func TestNewCachedHTTPClientWithStatuses(t *testing.T) { |
| 730 | tests := []struct { |
| 731 | name string |
| 732 | statuses []int |
| 733 | wantStatuses string |
| 734 | }{ |
| 735 | { |
| 736 | name: "with statuses", |
| 737 | statuses: []int{http.StatusForbidden, http.StatusNotFound}, |
| 738 | wantStatuses: "403,404", |
| 739 | }, |
| 740 | { |
| 741 | name: "without statuses", |
| 742 | wantStatuses: "", |
| 743 | }, |
| 744 | } |
| 745 | |
| 746 | for _, tt := range tests { |
| 747 | t.Run(tt.name, func(t *testing.T) { |
| 748 | var gotHeader http.Header |
| 749 | httpClient := &http.Client{Transport: funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 750 | gotHeader = req.Header |
| 751 | return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Request: req}, nil |
| 752 | }}} |
| 753 | |
| 754 | client := NewCachedHTTPClientWithStatuses(httpClient, time.Hour, tt.statuses...) |
| 755 | res, err := client.Get("https://api.github.com/user") |
| 756 | require.NoError(t, err) |
| 757 | res.Body.Close() |
| 758 | |
| 759 | assert.Equal(t, "1h0m0s", gotHeader.Get("X-GH-CACHE-TTL")) |
| 760 | if tt.wantStatuses != "" { |
| 761 | assert.Equal(t, tt.wantStatuses, gotHeader.Get("X-GH-CACHE-STATUSES")) |
| 762 | } else { |
| 763 | // We can't use the Get method because it'll return the same value for both empty and non-existent |
| 764 | // headers and we want to make sure the header is actually not present. |
| 765 | assert.Empty(t, gotHeader.Values("X-GH-CACHE-STATUSES")) |
| 766 | } |
| 767 | }) |
| 768 | } |
| 769 | } |