Skip to content

Commit b7d9bcc

Browse files
test(bzz): enhance redundancy level tests for upload and download handlers
1 parent 8e2cd27 commit b7d9bcc

1 file changed

Lines changed: 120 additions & 13 deletions

File tree

pkg/api/bzz_test.go

Lines changed: 120 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,36 +1186,143 @@ func TestBzzDownloadHeaders(t *testing.T) {
11861186
)
11871187
}
11881188

1189-
func TestBzzRedundancyLevel(t *testing.T) {
1189+
func TestBzzUploadRedundancyLevel(t *testing.T) {
11901190
t.Parallel()
11911191

11921192
client, _, _, _ := newTestServer(t, testServerOptions{
11931193
Storer: mockstorer.New(),
11941194
Post: mockpost.New(mockpost.WithAcceptAll()),
11951195
})
11961196

1197+
const maxValidLevel = redundancy.PARANOID
1198+
11971199
tests := []struct {
1198-
name string
1199-
level string
1200-
wantStatus int
1200+
name string
1201+
level int
1202+
want *jsonhttp.StatusResponse
12011203
}{
1202-
{"level 0 (NONE) is valid", "0", http.StatusCreated},
1203-
{"level 1 (MEDIUM) is valid", "1", http.StatusCreated},
1204-
{"level 2 (STRONG) is valid", "2", http.StatusCreated},
1205-
{"level 3 (INSANE) is valid", "3", http.StatusCreated},
1206-
{"level 4 (PARANOID) is valid", "4", http.StatusCreated},
1207-
{"level 5 is invalid", "5", http.StatusBadRequest},
1204+
{"minimum level (NONE) is valid", int(redundancy.NONE), nil},
1205+
{"maximum valid level (PARANOID) is valid", int(maxValidLevel), nil},
1206+
{
1207+
"level below minimum is invalid", int(-1),
1208+
&jsonhttp.StatusResponse{
1209+
Code: http.StatusBadRequest,
1210+
Message: "invalid header params",
1211+
Reasons: []jsonhttp.Reason{
1212+
{
1213+
Field: "Swarm-Redundancy-Level",
1214+
Error: "invalid syntax",
1215+
},
1216+
},
1217+
},
1218+
},
1219+
{
1220+
"level above maximum is invalid", int(maxValidLevel + 1),
1221+
&jsonhttp.StatusResponse{
1222+
Code: http.StatusBadRequest,
1223+
Message: "invalid header params",
1224+
Reasons: []jsonhttp.Reason{
1225+
{
1226+
Field: "swarm-redundancy-level",
1227+
Error: fmt.Sprintf("want lte:%d", maxValidLevel),
1228+
},
1229+
},
1230+
},
1231+
},
12081232
}
12091233

12101234
for _, tt := range tests {
12111235
t.Run(tt.name, func(t *testing.T) {
1212-
jsonhttptest.Request(t, client, http.MethodPost, "/bzz", tt.wantStatus,
1236+
opts := []jsonhttptest.Option{
12131237
jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "text/plain"),
12141238
jsonhttptest.WithRequestHeader(api.SwarmDeferredUploadHeader, "true"),
12151239
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
1216-
jsonhttptest.WithRequestHeader(api.SwarmRedundancyLevelHeader, tt.level),
1240+
jsonhttptest.WithRequestHeader(api.SwarmRedundancyLevelHeader, strconv.Itoa(tt.level)),
12171241
jsonhttptest.WithRequestBody(bytes.NewReader([]byte("test"))),
1218-
)
1242+
}
1243+
var statusCode int
1244+
if tt.want == nil {
1245+
statusCode = http.StatusCreated
1246+
} else {
1247+
statusCode = tt.want.Code
1248+
opts = append(opts, jsonhttptest.WithExpectedJSONResponse(*tt.want))
1249+
}
1250+
jsonhttptest.Request(t, client, http.MethodPost, "/bzz", statusCode, opts...)
1251+
})
1252+
}
1253+
}
1254+
1255+
func TestBzzDownloadRedundancyLevel(t *testing.T) {
1256+
t.Parallel()
1257+
1258+
client, _, _, _ := newTestServer(t, testServerOptions{
1259+
Storer: mockstorer.New(),
1260+
Post: mockpost.New(mockpost.WithAcceptAll()),
1261+
})
1262+
1263+
testData := []byte("test download redundancy level")
1264+
var resp api.BzzUploadResponse
1265+
jsonhttptest.Request(t, client, http.MethodPost, "/bzz", http.StatusCreated,
1266+
jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "text/plain"),
1267+
jsonhttptest.WithRequestHeader(api.SwarmDeferredUploadHeader, "true"),
1268+
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
1269+
jsonhttptest.WithRequestBody(bytes.NewReader(testData)),
1270+
jsonhttptest.WithUnmarshalJSONResponse(&resp),
1271+
)
1272+
1273+
const maxValidLevel = redundancy.PARANOID
1274+
1275+
tests := []struct {
1276+
name string
1277+
level int
1278+
want *jsonhttp.StatusResponse
1279+
}{
1280+
{"minimum level (NONE) is valid", int(redundancy.NONE), nil},
1281+
{"maximum valid level (PARANOID) is valid", int(maxValidLevel), nil},
1282+
{
1283+
"level below minimum is invalid", int(-1),
1284+
&jsonhttp.StatusResponse{
1285+
Code: http.StatusBadRequest,
1286+
Message: "invalid header params",
1287+
Reasons: []jsonhttp.Reason{
1288+
{
1289+
Field: "Swarm-Redundancy-Level",
1290+
Error: "invalid syntax",
1291+
},
1292+
},
1293+
},
1294+
},
1295+
{
1296+
"level above maximum is invalid", int(maxValidLevel + 1),
1297+
&jsonhttp.StatusResponse{
1298+
Code: http.StatusBadRequest,
1299+
Message: "invalid header params",
1300+
Reasons: []jsonhttp.Reason{
1301+
{
1302+
Field: "swarm-redundancy-level",
1303+
Error: fmt.Sprintf("want lte:%d", maxValidLevel),
1304+
},
1305+
},
1306+
},
1307+
},
1308+
}
1309+
1310+
for _, tt := range tests {
1311+
t.Run(tt.name, func(t *testing.T) {
1312+
opts := []jsonhttptest.Option{
1313+
jsonhttptest.WithRequestHeader(api.SwarmRedundancyLevelHeader, strconv.Itoa(tt.level)),
1314+
}
1315+
var statusCode int
1316+
if tt.want == nil {
1317+
statusCode = http.StatusOK
1318+
opts = append(opts,
1319+
jsonhttptest.WithExpectedResponse(testData),
1320+
)
1321+
} else {
1322+
statusCode = tt.want.Code
1323+
opts = append(opts, jsonhttptest.WithExpectedJSONResponse(*tt.want))
1324+
}
1325+
jsonhttptest.Request(t, client, http.MethodGet, "/bzz/"+resp.Reference.String(), statusCode, opts...)
12191326
})
12201327
}
12211328
}

0 commit comments

Comments
 (0)