@@ -1178,3 +1178,144 @@ func TestBzzDownloadHeaders(t *testing.T) {
11781178 jsonhttptest .WithExpectedResponseHeader (api .ContentTypeHeader , "text/html; charset=utf-8" ),
11791179 )
11801180}
1181+
1182+ func TestBzzUploadRedundancyLevel (t * testing.T ) {
1183+ t .Parallel ()
1184+
1185+ client , _ , _ , _ := newTestServer (t , testServerOptions {
1186+ Storer : mockstorer .New (),
1187+ Post : mockpost .New (mockpost .WithAcceptAll ()),
1188+ })
1189+
1190+ const maxValidLevel = redundancy .PARANOID
1191+
1192+ tests := []struct {
1193+ name string
1194+ level int
1195+ want * jsonhttp.StatusResponse
1196+ }{
1197+ {"minimum level (NONE) is valid" , int (redundancy .NONE ), nil },
1198+ {"maximum valid level (PARANOID) is valid" , int (maxValidLevel ), nil },
1199+ {
1200+ "level below minimum is invalid" , int (- 1 ),
1201+ & jsonhttp.StatusResponse {
1202+ Code : http .StatusBadRequest ,
1203+ Message : "invalid header params" ,
1204+ Reasons : []jsonhttp.Reason {
1205+ {
1206+ Field : "Swarm-Redundancy-Level" ,
1207+ Error : "invalid syntax" ,
1208+ },
1209+ },
1210+ },
1211+ },
1212+ {
1213+ "level above maximum is invalid" , int (maxValidLevel + 1 ),
1214+ & jsonhttp.StatusResponse {
1215+ Code : http .StatusBadRequest ,
1216+ Message : "invalid header params" ,
1217+ Reasons : []jsonhttp.Reason {
1218+ {
1219+ Field : "swarm-redundancy-level" ,
1220+ Error : fmt .Sprintf ("want redundancy level to be between %d and %d" , int (redundancy .NONE ), int (redundancy .PARANOID )),
1221+ },
1222+ },
1223+ },
1224+ },
1225+ }
1226+
1227+ for _ , tt := range tests {
1228+ t .Run (tt .name , func (t * testing.T ) {
1229+ opts := []jsonhttptest.Option {
1230+ jsonhttptest .WithRequestHeader (api .ContentTypeHeader , "text/plain" ),
1231+ jsonhttptest .WithRequestHeader (api .SwarmDeferredUploadHeader , "true" ),
1232+ jsonhttptest .WithRequestHeader (api .SwarmPostageBatchIdHeader , batchOkStr ),
1233+ jsonhttptest .WithRequestHeader (api .SwarmRedundancyLevelHeader , strconv .Itoa (tt .level )),
1234+ jsonhttptest .WithRequestBody (bytes .NewReader ([]byte ("test" ))),
1235+ }
1236+ var statusCode int
1237+ if tt .want == nil {
1238+ statusCode = http .StatusCreated
1239+ } else {
1240+ statusCode = tt .want .Code
1241+ opts = append (opts , jsonhttptest .WithExpectedJSONResponse (* tt .want ))
1242+ }
1243+ jsonhttptest .Request (t , client , http .MethodPost , "/bzz" , statusCode , opts ... )
1244+ })
1245+ }
1246+ }
1247+
1248+ func TestBzzDownloadRedundancyLevel (t * testing.T ) {
1249+ t .Parallel ()
1250+
1251+ client , _ , _ , _ := newTestServer (t , testServerOptions {
1252+ Storer : mockstorer .New (),
1253+ Post : mockpost .New (mockpost .WithAcceptAll ()),
1254+ })
1255+
1256+ testData := []byte ("test download redundancy level" )
1257+ var resp api.BzzUploadResponse
1258+ jsonhttptest .Request (t , client , http .MethodPost , "/bzz" , http .StatusCreated ,
1259+ jsonhttptest .WithRequestHeader (api .ContentTypeHeader , "text/plain" ),
1260+ jsonhttptest .WithRequestHeader (api .SwarmDeferredUploadHeader , "true" ),
1261+ jsonhttptest .WithRequestHeader (api .SwarmPostageBatchIdHeader , batchOkStr ),
1262+ jsonhttptest .WithRequestBody (bytes .NewReader (testData )),
1263+ jsonhttptest .WithUnmarshalJSONResponse (& resp ),
1264+ )
1265+
1266+ const maxValidLevel = redundancy .PARANOID
1267+
1268+ tests := []struct {
1269+ name string
1270+ level int
1271+ want * jsonhttp.StatusResponse
1272+ }{
1273+ {"minimum level (NONE) is valid" , int (redundancy .NONE ), nil },
1274+ {"maximum valid level (PARANOID) is valid" , int (maxValidLevel ), nil },
1275+ {
1276+ "level below minimum is invalid" , int (- 1 ),
1277+ & jsonhttp.StatusResponse {
1278+ Code : http .StatusBadRequest ,
1279+ Message : "invalid header params" ,
1280+ Reasons : []jsonhttp.Reason {
1281+ {
1282+ Field : "Swarm-Redundancy-Level" ,
1283+ Error : "invalid syntax" ,
1284+ },
1285+ },
1286+ },
1287+ },
1288+ {
1289+ "level above maximum is invalid" , int (maxValidLevel + 1 ),
1290+ & jsonhttp.StatusResponse {
1291+ Code : http .StatusBadRequest ,
1292+ Message : "invalid header params" ,
1293+ Reasons : []jsonhttp.Reason {
1294+ {
1295+ Field : "swarm-redundancy-level" ,
1296+ Error : fmt .Sprintf ("want redundancy level to be between %d and %d" , int (redundancy .NONE ), int (redundancy .PARANOID )),
1297+ },
1298+ },
1299+ },
1300+ },
1301+ }
1302+
1303+ for _ , tt := range tests {
1304+ t .Run (tt .name , func (t * testing.T ) {
1305+ opts := []jsonhttptest.Option {
1306+ jsonhttptest .WithRequestHeader (api .SwarmRedundancyLevelHeader , strconv .Itoa (tt .level )),
1307+ }
1308+ var statusCode int
1309+ if tt .want == nil {
1310+ statusCode = http .StatusOK
1311+ opts = append (opts ,
1312+ jsonhttptest .WithExpectedResponse (testData ),
1313+ )
1314+ } else {
1315+ statusCode = tt .want .Code
1316+ opts = append (opts , jsonhttptest .WithExpectedJSONResponse (* tt .want ))
1317+ }
1318+ jsonhttptest .Request (t , client , http .MethodGet , "/bzz/" + resp .Reference .String (), statusCode , opts ... )
1319+ })
1320+ }
1321+ }
0 commit comments