github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/utils/etag_test.go (about) 1 package utils 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func Test_CheckPreconditions_with_matching_etag(t *testing.T) { 12 r := httptest.NewRequest(http.MethodGet, "http://localhost/foo/bar", nil) 13 w := httptest.NewRecorder() 14 15 r.Header.Set("If-None-Match", `"some-etag"`) 16 17 done := CheckPreconditions(w, r, `"some-etag"`) 18 19 assert.True(t, done) 20 assert.Equal(t, http.StatusNotModified, w.Result().StatusCode) 21 } 22 23 func Test_CheckPreconditions_with_no_etag(t *testing.T) { 24 r := httptest.NewRequest(http.MethodGet, "http://localhost/foo/bar", nil) 25 w := httptest.NewRecorder() 26 27 done := CheckPreconditions(w, r, `"some-etag"`) 28 29 assert.False(t, done) 30 assert.Equal(t, http.StatusOK, w.Result().StatusCode) 31 } 32 33 func Test_CheckPreconditions_with_non_matching_etag(t *testing.T) { 34 r := httptest.NewRequest(http.MethodGet, "http://localhost/foo/bar", nil) 35 w := httptest.NewRecorder() 36 37 r.Header.Set("If-None-Match", `"some-etag"`) 38 39 done := CheckPreconditions(w, r, `"some-other-etag"`) 40 41 assert.False(t, done) 42 assert.Equal(t, http.StatusOK, w.Result().StatusCode) 43 } 44 45 func Test_checkIfNoneMatch(t *testing.T) { 46 var tests = []struct { 47 Name string 48 IfNoneMatch string 49 Etag string 50 Match bool 51 }{ 52 { 53 Name: "strong inm with string etag", 54 IfNoneMatch: `"some-etag"`, 55 Etag: `"some-etag"`, 56 Match: true, 57 }, 58 { 59 Name: "weak inm with strong etag", 60 IfNoneMatch: `W/"some-etag"`, 61 Etag: `"some-etag"`, 62 Match: true, 63 }, 64 { 65 Name: "strong inm with weak etag", 66 IfNoneMatch: `"some-etag"`, 67 Etag: `W/"some-etag"`, 68 Match: true, 69 }, 70 { 71 Name: "multiple inm values match etag", 72 IfNoneMatch: `"first-etag","second-etag"`, 73 Etag: `"second-etag"`, 74 Match: true, 75 }, 76 // TODO: This doesn't pass with the current implem, propose a new implem 77 // with working with this case. 78 // { 79 // Name: "multiple inm values are trimmed", 80 // IfNoneMatch: `"first-etag" , "second-etag"`, 81 // Etag: `"second-etag"`, 82 // Match: true, 83 // }, 84 { 85 Name: "inm is trimmed", 86 IfNoneMatch: ` "second-etag"\t`, 87 Etag: `"second-etag"`, 88 Match: true, 89 }, 90 { 91 Name: "inm with some invalid content is discarded", 92 IfNoneMatch: "\"secon\fd-etag\"", 93 Etag: "\"secon\fd-etag\"", 94 Match: false, 95 }, 96 { 97 Name: "multiple inm values not matching etag", 98 IfNoneMatch: `"first-etag","second-etag`, 99 Etag: `"third-etag"`, 100 Match: false, 101 }, 102 { 103 Name: "inm not matching with etag", 104 IfNoneMatch: `"some-etag"`, 105 Etag: `"some-other-etag"`, 106 Match: false, 107 }, 108 { 109 Name: "* match every etag", 110 IfNoneMatch: `*`, 111 Etag: `"some--etag"`, 112 Match: true, 113 }, 114 { 115 Name: "Invalid etag quote", 116 IfNoneMatch: `"some-etag`, 117 Etag: `W/"some-etag"`, 118 Match: false, 119 }, 120 { 121 Name: "Invalid etag quote", 122 IfNoneMatch: `some-etag`, 123 Etag: `W/"some-etag"`, 124 Match: false, 125 }, 126 { 127 Name: "Invalid etag quote", 128 IfNoneMatch: `"some-etag"`, 129 Etag: `some-etag`, 130 Match: false, 131 }, 132 } 133 134 for _, test := range tests { 135 t.Run(test.Name, func(t *testing.T) { 136 assert.Equal(t, test.Match, checkIfNoneMatch(test.IfNoneMatch, test.Etag)) 137 }) 138 } 139 }