Skip to content

Commit 3ffe90e

Browse files
Dragomir-Ivanovsmyrman
authored andcommitted
response_test: Fix HTTP headers to include ETag
1 parent 8cf5c7c commit 3ffe90e

7 files changed

+27
-21
lines changed

rest/method_item_delete_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestHandlerDeleteItemEtag(t *testing.T) {
9292
Fields: schema.Fields{"foo": {Filterable: true}},
9393
}, s, resource.DefaultConf)
9494
r, _ := http.NewRequest("DELETE", "/test/2", nil)
95-
r.Header.Set("If-Match", "a")
95+
r.Header.Set("If-Match", "W/a")
9696
rm := &RouteMatch{
9797
ResourcePath: []*ResourcePathComponent{
9898
&ResourcePathComponent{
@@ -123,7 +123,7 @@ func TestHandlerDeleteItemWrongEtag(t *testing.T) {
123123
Fields: schema.Fields{"foo": {Filterable: true}},
124124
}, s, resource.DefaultConf)
125125
r, _ := http.NewRequest("DELETE", "/test/2", nil)
126-
r.Header.Set("If-Match", "a")
126+
r.Header.Set("If-Match", "W/a")
127127
rm := &RouteMatch{
128128
ResourcePath: []*ResourcePathComponent{
129129
&ResourcePathComponent{

rest/method_item_get_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestHandlerGetItemEtagMatch(t *testing.T) {
134134
index := resource.NewIndex()
135135
test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
136136
r, _ := http.NewRequest("GET", "/test/2", nil)
137-
r.Header.Set("If-None-Match", "a")
137+
r.Header.Set("If-None-Match", "W/a")
138138
rm := &RouteMatch{
139139
ResourcePath: []*ResourcePathComponent{
140140
&ResourcePathComponent{
@@ -161,7 +161,7 @@ func TestHandlerGetItemEtagDontMatch(t *testing.T) {
161161
index := resource.NewIndex()
162162
test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
163163
r, _ := http.NewRequest("GET", "/test/2", nil)
164-
r.Header.Set("If-None-Match", "a")
164+
r.Header.Set("If-None-Match", "W/a")
165165
rm := &RouteMatch{
166166
ResourcePath: []*ResourcePathComponent{
167167
&ResourcePathComponent{

rest/method_item_patch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestHandlerPatchItemReplaceEtagMatch(t *testing.T) {
207207
})
208208
test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
209209
r, _ := http.NewRequest("PATCH", "/test/1", bytes.NewBufferString(`{"id": "1"}`))
210-
r.Header.Set("If-Match", "a")
210+
r.Header.Set("If-Match", "W/a")
211211
rm := &RouteMatch{
212212
ResourcePath: []*ResourcePathComponent{
213213
&ResourcePathComponent{
@@ -230,7 +230,7 @@ func TestHandlerPatchItemReplaceEtagDontMatch(t *testing.T) {
230230
})
231231
test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
232232
r, _ := http.NewRequest("PATCH", "/test/1", bytes.NewBufferString(`{"id": "1"}`))
233-
r.Header.Set("If-Match", "a")
233+
r.Header.Set("If-Match", "W/a")
234234
rm := &RouteMatch{
235235
ResourcePath: []*ResourcePathComponent{
236236
&ResourcePathComponent{

rest/method_item_put_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func TestHandlerPutItemReplaceEtagMatch(t *testing.T) {
272272
})
273273
test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
274274
r, _ := http.NewRequest("PUT", "/test/1", bytes.NewBufferString(`{"id": "1"}`))
275-
r.Header.Set("If-Match", "a")
275+
r.Header.Set("If-Match", "W/a")
276276
rm := &RouteMatch{
277277
ResourcePath: []*ResourcePathComponent{
278278
&ResourcePathComponent{
@@ -295,7 +295,7 @@ func TestHandlerPutItemReplaceEtagDontMatch(t *testing.T) {
295295
})
296296
test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
297297
r, _ := http.NewRequest("PUT", "/test/1", bytes.NewBufferString(`{"id": "1"}`))
298-
r.Header.Set("If-Match", "a")
298+
r.Header.Set("If-Match", "W/a")
299299
rm := &RouteMatch{
300300
ResourcePath: []*ResourcePathComponent{
301301
&ResourcePathComponent{

rest/response.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ func (f DefaultResponseFormatter) FormatList(ctx context.Context, headers http.H
9191
if l.Offset > 0 {
9292
headers.Set("X-Offset", strconv.Itoa(l.Offset))
9393
}
94+
95+
hash := md5.New()
96+
for _, item := range l.Items {
97+
if item.ETag != "" {
98+
hash.Write([]byte(item.ETag))
99+
}
100+
}
101+
headers.Set("ETag", `W/"`+fmt.Sprintf("%x", hash.Sum(nil))+`"`)
102+
94103
if !skipBody {
95104
payload := make([]map[string]interface{}, len(l.Items))
96-
hash := md5.New()
97105
for i, item := range l.Items {
98106
// Clone item payload to add the etag to the items in the list.
99107
d := map[string]interface{}{}
@@ -102,11 +110,9 @@ func (f DefaultResponseFormatter) FormatList(ctx context.Context, headers http.H
102110
}
103111
if item.ETag != "" {
104112
d["_etag"] = item.ETag
105-
hash.Write([]byte(item.ETag))
106113
}
107114
payload[i] = d
108115
}
109-
headers.Set("ETag", `W/"`+fmt.Sprintf("%x", hash.Sum(nil))+`"`)
110116
return ctx, payload
111117
}
112118
return ctx, nil

rest/response_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestDefaultResponseFormatterFormatItem(t *testing.T) {
8282

8383
h = http.Header{}
8484
rctx, payload = rf.FormatItem(ctx, h, &resource.Item{ETag: "1234"}, false)
85-
assert.Equal(t, http.Header{"Etag": []string{`"1234"`}}, h)
85+
assert.Equal(t, http.Header{"Etag": []string{`W/"1234"`}}, h)
8686
assert.Equal(t, rctx, ctx)
8787
assert.Equal(t, map[string]interface{}(nil), payload)
8888
}
@@ -95,7 +95,7 @@ func TestDefaultResponseFormatterFormatList(t *testing.T) {
9595
Total: -1,
9696
Items: []*resource.Item{{Payload: map[string]interface{}{"foo": "bar"}}},
9797
}, false)
98-
assert.Equal(t, http.Header{}, h)
98+
assert.Equal(t, http.Header{"Etag": []string{`W/"d41d8cd98f00b204e9800998ecf8427e"`}}, h)
9999
assert.Equal(t, rctx, ctx)
100100
assert.Equal(t, []map[string]interface{}{{"foo": "bar"}}, payload)
101101

@@ -104,7 +104,7 @@ func TestDefaultResponseFormatterFormatList(t *testing.T) {
104104
Total: -1,
105105
Items: []*resource.Item{{Payload: map[string]interface{}{"foo": "bar"}}},
106106
}, true)
107-
assert.Equal(t, http.Header{}, h)
107+
assert.Equal(t, http.Header{"Etag": []string{`W/"d41d8cd98f00b204e9800998ecf8427e"`}}, h)
108108
assert.Equal(t, rctx, ctx)
109109
assert.Equal(t, nil, payload)
110110

@@ -114,7 +114,7 @@ func TestDefaultResponseFormatterFormatList(t *testing.T) {
114114
Offset: 2,
115115
Items: []*resource.Item{{Payload: map[string]interface{}{"foo": "bar"}}},
116116
}, false)
117-
assert.Equal(t, http.Header{"X-Total": []string{"1"}, "X-Offset": []string{"2"}}, h)
117+
assert.Equal(t, http.Header{"X-Total": []string{"1"}, "X-Offset": []string{"2"}, "Etag": []string{`W/"d41d8cd98f00b204e9800998ecf8427e"`}}, h)
118118
assert.Equal(t, rctx, ctx)
119119
assert.Equal(t, []map[string]interface{}{{"foo": "bar"}}, payload)
120120

@@ -123,7 +123,7 @@ func TestDefaultResponseFormatterFormatList(t *testing.T) {
123123
Total: -1,
124124
Items: []*resource.Item{{ETag: "123", Payload: map[string]interface{}{"foo": "bar"}}},
125125
}, false)
126-
assert.Equal(t, http.Header{}, h)
126+
assert.Equal(t, http.Header{"Etag": []string{`W/"202cb962ac59075b964b07152d234b70"`}}, h)
127127
assert.Equal(t, rctx, ctx)
128128
assert.Equal(t, []map[string]interface{}{{"foo": "bar", "_etag": "123"}}, payload)
129129
}

rest/util_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ func TestSetAllowHeader(t *testing.T) {
141141
}
142142

143143
func TestCompareEtag(t *testing.T) {
144-
assert.True(t, compareEtag(`abc`, `abc`))
145-
assert.True(t, compareEtag(`"abc"`, `abc`))
146-
assert.False(t, compareEtag(`'abc'`, `abc`))
147-
assert.False(t, compareEtag(`"abc`, `abc`))
144+
assert.True(t, compareEtag(`W/abc`, `abc`))
145+
assert.True(t, compareEtag(`W/"abc"`, `abc`))
146+
assert.False(t, compareEtag(`W/'abc'`, `abc`))
147+
assert.False(t, compareEtag(`W/"abc`, `abc`))
148148
assert.False(t, compareEtag(``, `abc`))
149149
assert.False(t, compareEtag(`"cba"`, `abc`))
150150
}
@@ -219,7 +219,7 @@ func TestRequestCheckIntegrityEtagMissmatch(t *testing.T) {
219219

220220
func TestRequestCheckIntegrityEtagMatch(t *testing.T) {
221221
r, _ := http.NewRequest("GET", "/", nil)
222-
r.Header.Set("If-Match", "foo")
222+
r.Header.Set("If-Match", "W/foo")
223223
err := checkIntegrityRequest(r, &resource.Item{ETag: "foo"})
224224
assert.Nil(t, err)
225225
}

0 commit comments

Comments
 (0)