1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
// SPDX-FileCopyrightText: 2018 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0
package tests
import (
"net/http"
"strconv"
"testing"
"go.xyrillian.de/schwift/v2"
)
func TestFieldString(t *testing.T) {
hdr := schwift.NewAccountHeaders()
expectBool(t, hdr.TempURLKey().Exists(), false)
expectString(t, hdr.TempURLKey().Get(), "")
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Meta-Temp-Url-Key"] = ""
expectBool(t, hdr.TempURLKey().Exists(), false)
expectString(t, hdr.TempURLKey().Get(), "")
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Meta-Temp-Url-Key"] = "foo"
expectBool(t, hdr.TempURLKey().Exists(), true)
expectString(t, hdr.TempURLKey().Get(), "foo")
expectSuccess(t, hdr.Validate())
hdr.TempURLKey().Set("bar")
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Temp-Url-Key": "bar",
})
hdr.TempURLKey().Clear()
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Temp-Url-Key": "",
})
hdr.TempURLKey().Del()
expectHeaders(t, hdr.Headers, nil)
hdr.TempURLKey().Clear()
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Temp-Url-Key": "",
})
}
////////////////////////////////////////////////////////////////////////////////
func TestFieldTimestamp(t *testing.T) {
testWithAccount(t, func(a *schwift.Account) {
hdr, err := a.Headers(t.Context())
if !expectSuccess(t, err) {
return
}
expectBool(t, hdr.CreatedAt().Exists(), true)
actual := float64(hdr.CreatedAt().Get().UnixNano()) / 1e9
expected, _ := strconv.ParseFloat(hdr.Headers["X-Timestamp"], 64) //nolint:errcheck
expectFloat64(t, actual, expected)
})
hdr := schwift.NewAccountHeaders()
expectBool(t, hdr.CreatedAt().Exists(), false)
expectBool(t, hdr.CreatedAt().Get().IsZero(), true)
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Timestamp"] = "wtf"
expectBool(t, hdr.CreatedAt().Exists(), true)
expectBool(t, hdr.CreatedAt().Get().IsZero(), true)
expectError(t, hdr.Validate(), `Bad header X-Timestamp: strconv.ParseFloat: parsing "wtf": invalid syntax`)
}
func TestFieldHTTPTimestamp(t *testing.T) {
testWithContainer(t, func(c *schwift.Container) {
obj := c.Object("test")
err := obj.Upload(t.Context(), nil, nil, nil)
if !expectSuccess(t, err) {
return
}
hdr, err := obj.Headers(t.Context())
if !expectSuccess(t, err) {
return
}
expectBool(t, hdr.UpdatedAt().Exists(), true)
actual := hdr.UpdatedAt().Get()
expected, _ := http.ParseTime(hdr.Get("Last-Modified")) //nolint:errcheck
expectInt64(t, actual.Unix(), expected.Unix())
})
hdr := schwift.NewObjectHeaders()
expectBool(t, hdr.UpdatedAt().Exists(), false)
expectBool(t, hdr.UpdatedAt().Get().IsZero(), true)
expectSuccess(t, hdr.Validate())
hdr.Headers["Last-Modified"] = "wtf"
expectBool(t, hdr.UpdatedAt().Exists(), true)
expectBool(t, hdr.UpdatedAt().Get().IsZero(), true)
expectError(t, hdr.Validate(), `Bad header Last-Modified: parsing time "wtf" as "Mon Jan _2 15:04:05 2006": cannot parse "wtf" as "Mon"`)
}
////////////////////////////////////////////////////////////////////////////////
func TestFieldUint64(t *testing.T) {
hdr := schwift.NewAccountHeaders()
expectBool(t, hdr.BytesUsedQuota().Exists(), false)
expectUint64(t, hdr.BytesUsedQuota().Get(), 0)
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Meta-Quota-Bytes"] = "23"
expectBool(t, hdr.BytesUsedQuota().Exists(), true)
expectUint64(t, hdr.BytesUsedQuota().Get(), 23)
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Meta-Quota-Bytes"] = "-23"
expectBool(t, hdr.BytesUsedQuota().Exists(), true)
expectUint64(t, hdr.BytesUsedQuota().Get(), 0)
expectError(t, hdr.Validate(), `Bad header X-Account-Meta-Quota-Bytes: strconv.ParseUint: parsing "-23": invalid syntax`)
hdr.BytesUsedQuota().Set(9001)
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Quota-Bytes": "9001",
})
hdr.BytesUsedQuota().Clear()
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Quota-Bytes": "",
})
hdr.BytesUsedQuota().Del()
expectHeaders(t, hdr.Headers, nil)
hdr.BytesUsedQuota().Clear()
expectHeaders(t, hdr.Headers, map[string]string{
"X-Account-Meta-Quota-Bytes": "",
})
}
func TestFieldUint64Readonly(t *testing.T) {
hdr := schwift.NewAccountHeaders()
expectBool(t, hdr.BytesUsed().Exists(), false)
expectUint64(t, hdr.BytesUsed().Get(), 0)
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Bytes-Used"] = "23"
expectBool(t, hdr.BytesUsed().Exists(), true)
expectUint64(t, hdr.BytesUsed().Get(), 23)
expectSuccess(t, hdr.Validate())
hdr.Headers["X-Account-Bytes-Used"] = "-23"
expectBool(t, hdr.BytesUsed().Exists(), true)
expectUint64(t, hdr.BytesUsed().Get(), 0)
expectError(t, hdr.Validate(), `Bad header X-Account-Bytes-Used: strconv.ParseUint: parsing "-23": invalid syntax`)
}
|