Skip to content

Commit 0d653ed

Browse files
committed
using helper functions for statusForObject + removed unnecessary parts from httpHandler
1 parent ae270e5 commit 0d653ed

16 files changed

+67
-83
lines changed

tests/remote_js/httpHandler.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
const fetch = require('node-fetch');
22
var config = require('config');
3-
const http = require("node:http");
4-
const https = require("node:https");
5-
6-
7-
// http(s) agents with keepAlive=true used to prevent socket from hanging
8-
// due to bug in node-fetch: https://github.com/node-fetch/node-fetch/issues/1735
9-
const httpAgent = new http.Agent({ keepAlive: true });
10-
const httpsAgent = new https.Agent({ keepAlive: true });
11-
const agentSelector = function (_parsedURL) {
12-
if (_parsedURL.protocol == 'http:') {
13-
return httpAgent;
14-
}
15-
else {
16-
return httpsAgent;
17-
}
18-
};
193

204
class HTTP {
215
static verbose = config.verbose;
@@ -43,9 +27,7 @@ class HTTP {
4327
url = url.replace(localIPRegex, 'localhost');
4428
}
4529

46-
// workaround to prevent socket from hanging due to but in node-fetch: https://github.com/node-fetch/node-fetch/issues/1735
47-
await new Promise(resolve => setTimeout(resolve, 1));
48-
let response = await fetch(url, Object.assign(options, { agent: agentSelector }));
30+
let response = await fetch(url, options);
4931

5032

5133
// Fetch doesn't automatically parse the response body, so we have to do that manually

tests/remote_js/test/2/generalTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('GeneralTests', function () {
5252
);
5353

5454
Helpers.assertStatusCode(response, 200);
55-
Helpers.assertStatusForObject(response, 'success', 0);
55+
Helpers.assert200ForObject(response);
5656

5757
response = await API.userPost(
5858
config.userID,

tests/remote_js/test/2/itemsTest.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var config = require('config');
44
const API = require('../../api2.js');
55
const Helpers = require('../../helpers2.js');
66
const { API2Before, API2After } = require("../shared.js");
7+
const {post}=require('../../httpHandler.js');
78

89
describe('ItemsTests', function () {
910
this.timeout(config.timeout * 2);
@@ -367,7 +368,7 @@ describe('ItemsTests', function () {
367368
}),
368369
{ "Content-Type": "application/json" }
369370
);
370-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "'itemType' property not provided");
371+
Helpers.assert400ForObject(response, { message: "'itemType' property not provided" });
371372

372373
// contentType on non-attachment
373374
const json3 = { ...json };
@@ -380,7 +381,7 @@ describe('ItemsTests', function () {
380381
}),
381382
{ "Content-Type": "application/json" }
382383
);
383-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "'contentType' is valid only for attachment items");
384+
Helpers.assert400ForObject(response, { message: "'contentType' is valid only for attachment items" });
384385
});
385386

386387
it('testEditTopLevelNote', async function () {
@@ -486,7 +487,7 @@ describe('ItemsTests', function () {
486487
items: [json],
487488
}),
488489
);
489-
Helpers.assertStatusForObject(response, 'success', 0);
490+
Helpers.assert200ForObject(response);
490491
const xml2 = await API.getItemXML(json.itemKey);
491492
const data2 = API.parseDataFromAtomEntry(xml2);
492493
const json2 = JSON.parse(data2.content);
@@ -660,7 +661,7 @@ describe('ItemsTests', function () {
660661
}),
661662
{ "Content-Type": "application/json" }
662663
);
663-
Helpers.assertStatusForObject(newResponse, 'failed', 0, 400, "'invalidName' is not a valid linkMode");
664+
Helpers.assert400ForObject(newResponse, { message: "'invalidName' is not a valid linkMode" });
664665

665666
// Missing linkMode
666667
delete json.linkMode;
@@ -672,7 +673,7 @@ describe('ItemsTests', function () {
672673
}),
673674
{ "Content-Type": "application/json" }
674675
);
675-
Helpers.assertStatusForObject(missingResponse, 'failed', 0, 400, "'linkMode' property not provided");
676+
Helpers.assert400ForObject(missingResponse, { message: "'linkMode' property not provided" });
676677
});
677678
it('testNewAttachmentItemMD5OnLinkedURL', async function () {
678679
const newItemData = await testNewEmptyBookItem();
@@ -691,7 +692,7 @@ describe('ItemsTests', function () {
691692
}),
692693
{ "Content-Type": "application/json" }
693694
);
694-
Helpers.assertStatusForObject(postResponse, 'failed', 0, 400, "'md5' is valid only for imported and embedded-image attachments");
695+
Helpers.assert400ForObject(postResponse, { message: "'md5' is valid only for imported and embedded-image attachments" });
695696
});
696697
it('testNewAttachmentItemModTimeOnLinkedURL', async function () {
697698
const newItemData = await testNewEmptyBookItem();
@@ -710,7 +711,7 @@ describe('ItemsTests', function () {
710711
}),
711712
{ "Content-Type": "application/json" }
712713
);
713-
Helpers.assertStatusForObject(postResponse, 'failed', 0, 400, "'mtime' is valid only for imported and embedded-image attachments");
714+
Helpers.assert400ForObject(postResponse, { message: "'mtime' is valid only for imported and embedded-image attachments" });
714715
});
715716
it('testMappedCreatorTypes', async function () {
716717
const json = {
@@ -743,8 +744,8 @@ describe('ItemsTests', function () {
743744
JSON.stringify(json)
744745
);
745746
// 'author' gets mapped automatically, others dont
746-
Helpers.assertStatusForObject(response, 'failed', 1, 400);
747-
Helpers.assertStatusForObject(response, 'success', 0);
747+
Helpers.assert400ForObject(response, { index: 1 });
748+
Helpers.assert200ForObject(response);
748749
});
749750

750751
it('testNumChildren', async function () {

tests/remote_js/test/2/noteTest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('NoteTests', function () {
3232
{ "Content-Type": "application/json" }
3333
);
3434
const expectedMessage = "Note '1234567890123456789012345678901234567890123456789012345678901234567890123456789...' too long";
35-
Helpers.assertStatusForObject(response, 'failed', 0, 413, expectedMessage);
35+
Helpers.assert413ForObject(response, { message : expectedMessage });
3636
});
3737

3838
it('testNoteTooLongBlankFirstLines', async function () {
@@ -47,7 +47,7 @@ describe('NoteTests', function () {
4747
{ "Content-Type": "application/json" }
4848
);
4949
const expectedMessage = "Note '1234567890123456789012345678901234567890123456789012345678901234567890123456789...' too long";
50-
Helpers.assertStatusForObject(response, 'failed', 0, 413, expectedMessage);
50+
Helpers.assert413ForObject(response, { message : expectedMessage });
5151
});
5252

5353
it('testNoteTooLongBlankFirstLinesHTML', async function () {
@@ -63,7 +63,7 @@ describe('NoteTests', function () {
6363
);
6464

6565
const expectedMessage = "Note '1234567890123456789012345678901234567890123456789012345678901234567890123...' too long";
66-
Helpers.assertStatusForObject(response, 'failed', 0, 413, expectedMessage);
66+
Helpers.assert413ForObject(response, { message : expectedMessage });
6767
});
6868

6969
it('testNoteTooLongTitlePlusNewlines', async function () {
@@ -79,7 +79,7 @@ describe('NoteTests', function () {
7979
);
8080

8181
const expectedMessage = "Note 'Full Text: 1234567890123456789012345678901234567890123456789012345678901234567...' too long";
82-
Helpers.assertStatusForObject(response, 'failed', 0, 413, expectedMessage);
82+
Helpers.assert413ForObject(response, { message : expectedMessage });
8383
});
8484

8585
// All content within HTML tags
@@ -96,6 +96,6 @@ describe('NoteTests', function () {
9696
);
9797

9898
const expectedMessage = "Note '<p><!-- 1234567890123456789012345678901234567890123456789012345678901234...' too long";
99-
Helpers.assertStatusForObject(response, 'failed', 0, 413, expectedMessage);
99+
Helpers.assert413ForObject(response, { message : expectedMessage });
100100
});
101101
});

tests/remote_js/test/2/objectTest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ describe('ObjectTests', function () {
192192
Helpers.assertStatusCode(response, 200);
193193
json = API.getJSONFromResponse(response);
194194

195-
Helpers.assertStatusForObject(response, 'success', 0, 200);
196-
Helpers.assertStatusForObject(response, 'success', 1, 413);
197-
Helpers.assertStatusForObject(response, 'success', 2, 200);
195+
Helpers.assert200ForObject(response, { index: 0 });
196+
Helpers.assert413ForObject(response, { index: 1 });
197+
Helpers.assert200ForObject(response, { index: 2 });
198198

199199
const responseKeys = await API.userGet(
200200
config.userID,
@@ -268,8 +268,8 @@ describe('ObjectTests', function () {
268268
let json = API.getJSONFromResponse(response);
269269

270270
Helpers.assertStatusForObject(response, 'unchanged', 0);
271-
Helpers.assertStatusForObject(response, 'failed', 1);
272-
Helpers.assertStatusForObject(response, 'success', 2);
271+
Helpers.assert413ForObject(response, { index: 1 });
272+
Helpers.assert200ForObject(response, { index: 2 });
273273

274274

275275
response = await API.userGet(config.userID,

tests/remote_js/test/2/relationsTest.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,23 @@ describe('RelationsTests', function () {
134134
}
135135
}, true, 'response');
136136

137-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "Unsupported predicate 'foo:unknown'");
137+
Helpers.assert400ForObject(response, { message: "Unsupported predicate 'foo:unknown'" });
138138

139139
response = await API.createItem('book', {
140140
relations: {
141141
'owl:sameAs': 'Not a URI'
142142
}
143143
}, this, 'response');
144144

145-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "'relations' values currently must be Zotero item URIs");
145+
Helpers.assert400ForObject(response, { message: "'relations' values currently must be Zotero item URIs" });
146146

147147
response = await API.createItem('book', {
148148
relations: {
149149
'owl:sameAs': ['Not a URI']
150150
}
151151
}, this, 'response');
152152

153-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "'relations' values currently must be Zotero item URIs");
153+
Helpers.assert400ForObject(response, { message: "'relations' values currently must be Zotero item URIs" });
154154
});
155155

156156
it('testDeleteItemRelation', async function () {
@@ -231,7 +231,8 @@ describe('RelationsTests', function () {
231231
"collections?key=" + config.apiKey,
232232
JSON.stringify({ collections: [json] })
233233
);
234-
Helpers.assertStatusForObject(response, 'failed', 0, null, "Unsupported predicate 'foo:unknown'");
234+
235+
Helpers.assert400ForObject(response, { message: "Unsupported predicate 'foo:unknown'" });
235236

236237
json.relations = {
237238
"owl:sameAs": "Not a URI"
@@ -241,15 +242,15 @@ describe('RelationsTests', function () {
241242
"collections?key=" + config.apiKey,
242243
JSON.stringify({ collections: [json] })
243244
);
244-
Helpers.assertStatusForObject(response2, 'failed', 0, null, "'relations' values currently must be Zotero collection URIs");
245+
Helpers.assert400ForObject(response2, { message: "'relations' values currently must be Zotero collection URIs" });
245246

246247
json.relations = ["http://zotero.org/groups/1/collections/AAAAAAAA"];
247248
const response3 = await API.userPost(
248249
config.userID,
249250
"collections?key=" + config.apiKey,
250251
JSON.stringify({ collections: [json] })
251252
);
252-
Helpers.assertStatusForObject(response3, 'failed', 0, null, "'relations' property must be an object");
253+
Helpers.assert400ForObject(response3, { message: "'relations' property must be an object" });
253254
});
254255

255256
it('testDeleteCollectionRelation', async function () {

tests/remote_js/test/2/searchTest.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ describe('SearchTests', function () {
102102
'Content-Type': 'application/json',
103103
};
104104
const response = await API.createSearch('', conditions, headers, 'responsejson');
105-
Helpers.assertStatusForObject(response, 'failed', 0, 400, 'Search name cannot be empty');
105+
Helpers.assert400ForObject(response, { message: 'Search name cannot be empty' });
106106
});
107107

108108
it('testNewSearchNoConditions', async function () {
109109
const json = await API.createSearch("Test", [], true, 'responsejson');
110-
Helpers.assertStatusForObject(json, 'failed', 0, 400, "'conditions' cannot be empty");
110+
Helpers.assert400ForObject(json, { message: "'conditions' cannot be empty" });
111111
});
112112

113113
it('testNewSearchConditionErrors', async function () {
@@ -122,7 +122,7 @@ describe('SearchTests', function () {
122122
true,
123123
'responsejson'
124124
);
125-
Helpers.assertStatusForObject(json, 'failed', 0, 400, "'condition' property not provided for search condition");
125+
Helpers.assert400ForObject(json, { message: "'condition' property not provided for search condition" });
126126

127127

128128
json = await API.createSearch(
@@ -137,7 +137,7 @@ describe('SearchTests', function () {
137137
true,
138138
'responsejson'
139139
);
140-
Helpers.assertStatusForObject(json, 'failed', 0, 400, 'Search condition cannot be empty');
140+
Helpers.assert400ForObject(json, { message: 'Search condition cannot be empty' });
141141

142142

143143
json = await API.createSearch(
@@ -151,7 +151,7 @@ describe('SearchTests', function () {
151151
true,
152152
'responsejson'
153153
);
154-
Helpers.assertStatusForObject(json, 'failed', 0, 400, "'operator' property not provided for search condition");
154+
Helpers.assert400ForObject(json, { message: "'operator' property not provided for search condition" });
155155

156156

157157
json = await API.createSearch(
@@ -166,6 +166,6 @@ describe('SearchTests', function () {
166166
true,
167167
'responsejson'
168168
);
169-
Helpers.assertStatusForObject(json, 'failed', 0, 400, 'Search operator cannot be empty');
169+
Helpers.assert400ForObject(json, { message: 'Search operator cannot be empty' });
170170
});
171171
});

tests/remote_js/test/2/tagTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('TagTests', function () {
3131
let headers = { "Content-Type": "application/json" };
3232
let response = await API.postItem(json, headers);
3333

34-
Helpers.assertStatusForObject(response, 'failed', 0, 400, "Tag must be an object");
34+
Helpers.assert400ForObject(response, { message: "Tag must be an object" });
3535
});
3636

3737
it('testTagSearch', async function () {

tests/remote_js/test/2/versionTest.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe('VersionsTests', function () {
266266
headers2
267267
);
268268
Helpers.assertStatusCode(response, 200);
269-
Helpers.assertStatusForObject(response, 'success', 0);
269+
Helpers.assert200ForObject(response);
270270
const version2 = parseInt(response.headers['last-modified-version'][0]);
271271
assert.isNumber(version2);
272272
// Version should be incremented on new object
@@ -314,7 +314,7 @@ describe('VersionsTests', function () {
314314
"Content-Type": "application/json"
315315
}
316316
);
317-
Helpers.assertStatusForObject(response, 'failed', 0, 428);
317+
Helpers.assert428ForObject(response);
318318

319319
json[objectVersionProp] = version - 1;
320320

@@ -330,7 +330,7 @@ describe('VersionsTests', function () {
330330
);
331331
// Outdated object version property
332332
const message = `${_capitalizeFirstLetter(objectType)} has been modified since specified version (expected ${json[objectVersionProp]}, found ${version2})`;
333-
Helpers.assertStatusForObject(response, 'failed', 0, 412, message);
333+
Helpers.assert412ForObject(response, { message: message });
334334
// Modify object, using object version property
335335
json[objectVersionProp] = version;
336336

@@ -345,7 +345,7 @@ describe('VersionsTests', function () {
345345
}
346346
);
347347
Helpers.assertStatusCode(response, 200);
348-
Helpers.assertStatusForObject(response, 'success', 0);
348+
Helpers.assert200ForObject(response);
349349
// Version should be incremented on modified object
350350
const version3 = parseInt(response.headers['last-modified-version'][0]);
351351
assert.isNumber(version3);

tests/remote_js/test/3/generalTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('GeneralTests', function () {
5050
);
5151

5252
Helpers.assertStatusCode(response, 200);
53-
Helpers.assertStatusForObject(response, 'success', 0);
53+
Helpers.assert200ForObject(response);
5454

5555
response = await API.userPost(
5656
config.userID,

0 commit comments

Comments
 (0)