Skip to content

Commit

Permalink
empty resp and stricter checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmacro committed Oct 24, 2023
1 parent 325be82 commit 9c841bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion tests/unit/utils/safeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { recoverListing } = require('../../../utils/safeList');
describe('test safeListObjectVersions', () => {
const testCases = [
{
description: 'should parse empty response',
description: 'should parse valid empty response',
response: {
ListVersionsResult: {
Name: 'my-bucket',
Expand All @@ -22,6 +22,11 @@ describe('test safeListObjectVersions', () => {
CommonPrefixes: [],
},
},
{
description: 'should return error for invalid empty response',
response: {},
expectedError: new Error('im an original error'),
},
{
description: 'should parse truncated response',
response: {
Expand Down
10 changes: 5 additions & 5 deletions utils/safeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function extractKeys(data, keys) {
* @returns {ExtractResult} - extracted value
*/
function safeExtract(key, data) {
if (data[key] && data[key].length > 0) {
if (Array.isArray(data[key]) && data[key].length > 0) {
return { value: data[key][0] };
}
return { value: NO_VALUE };
Expand All @@ -62,7 +62,7 @@ function safeExtract(key, data) {
* @returns {ExtractResult} - extracted value
*/
function safeExtractInt(key, data) {
if (data[key] && data[key].length > 0) {
if (Array.isArray(data[key]) && data[key].length > 0) {
const parsed = parseInt(data[key][0], 10);
if (!Number.isNaN(parsed)) {
return { value: parsed };
Expand All @@ -82,7 +82,7 @@ function safeExtractInt(key, data) {
* @returns {ExtractResult} - extracted value
*/
function safeExtractDate(key, data) {
if (data[key] && data[key].length > 0) {
if (Array.isArray(data[key]) && data[key].length > 0) {
const parsed = new Date(data[key][0]);
if (Number.isNaN(parsed.getTime())) {
return { value: data[key][0] };
Expand All @@ -101,7 +101,7 @@ function safeExtractDate(key, data) {
* @returns {ExtractResult} - extracted value
*/
function safeExtractBool(key, data) {
if (data[key] && data[key].length > 0) {
if (Array.isArray(data[key]) && data[key].length > 0) {
return { value: data[key][0] === 'true' };
}
return { value: NO_VALUE };
Expand Down Expand Up @@ -141,7 +141,7 @@ const ownerKeys = {
* @returns {ExtractResult} - extracted value
*/
function extractOwner(key, data) {
if (!data[key] || data[key].length === 0) {
if (!Array.isArray(data[key]) || data[key].length === 0) {
return { value: NO_VALUE };
}

Expand Down

0 comments on commit 9c841bd

Please sign in to comment.