| 708 | } |
| 709 | |
| 710 | func TestCacheablePass(t *testing.T) { |
| 711 | tests := []struct { |
| 712 | name string |
| 713 | headers http.Header |
| 714 | wantTTL time.Duration |
| 715 | wantOK bool |
| 716 | }{{ |
| 717 | name: "valid Cache-Control", |
| 718 | headers: http.Header{ |
| 719 | "Cache-Control": []string{"max-age=100"}, |
| 720 | }, |
| 721 | wantTTL: 100 * time.Second, |
| 722 | wantOK: true, |
| 723 | }, { |
| 724 | name: "valid Date and Expires", |
| 725 | headers: http.Header{ |
| 726 | "Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
| 727 | "Expires": []string{"Fri, 02 Dec 1983 01:00:00 GMT"}, |
| 728 | }, |
| 729 | wantTTL: 10800 * time.Second, |
| 730 | wantOK: true, |
| 731 | }, { |
| 732 | name: "Cache-Control supersedes Date and Expires", |
| 733 | headers: http.Header{ |
| 734 | "Cache-Control": []string{"max-age=100"}, |
| 735 | "Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
| 736 | "Expires": []string{"Fri, 02 Dec 1983 01:00:00 GMT"}, |
| 737 | }, |
| 738 | wantTTL: 100 * time.Second, |
| 739 | wantOK: true, |
| 740 | }, { |
| 741 | name: "no caching headers", |
| 742 | headers: http.Header{}, |
| 743 | wantOK: false, |
| 744 | }} |
| 745 | |
| 746 | for _, tc := range tests { |
| 747 | t.Run(tc.name, func(t *testing.T) { |
| 748 | ttl, ok, err := cacheable(tc.headers) |
| 749 | require.NoError(t, err, "Error getting expiry") |
| 750 | assert.Equal(t, tc.wantTTL, ttl, "TTL mismatch") |
| 751 | assert.Equal(t, tc.wantOK, ok, " incorrect ok value") |
| 752 | }) |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | func TestCacheableFail(t *testing.T) { |
| 757 | tests := []struct { |