Skip to content

Commit 0ca0721

Browse files
Add more flexibility to the internal sendRequest function.
This adds the ability to add a body to POST requests. Also updated the automated tests.
1 parent 7b7b03b commit 0ca0721

File tree

2 files changed

+75
-61
lines changed

2 files changed

+75
-61
lines changed

lib/internal.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,27 @@ let clientObject;
2727
return this;
2828
}
2929

30-
function sendRequest(userOptions, optionalName, requestType='single', responseType='single', validStatusCodes = [200, 201, 204, 404]){
31-
if(!userOptions.path){
30+
function sendRequest(path, requestOptionsCallback, operationCallback){
31+
if(!path){
3232
throw new Error('Path is needed to send request.');
3333
}
34-
mlutil.copyProperties(userOptions,requestOptions);
35-
let operation = new Operation(
36-
optionalName?optionalName:'', clientObject, requestOptions, requestType, responseType
37-
);
38-
operation.validStatusCodes = validStatusCodes;
39-
return requester.startRequest(operation);
40-
}
34+
requestOptions.path = mlutil.databaseParam(clientObject.getConnectionParams(), path, '?')
35+
if (requestOptionsCallback) {
36+
requestOptionsCallback(requestOptions);
37+
}
4138

42-
function newRequestOptions(path){
43-
let userOptions = {};
44-
userOptions.path = mlutil.databaseParam(clientObject.getConnectionParams(), path, '?');
45-
return userOptions;
39+
const operation = new Operation("", clientObject, requestOptions, "single", "single");
40+
if (operationCallback) {
41+
operationCallback(operation);
42+
}
43+
if (!operation.validStatusCodes) {
44+
operation.validStatusCodes = [200, 201, 204, 404];
45+
}
46+
47+
return requester.startRequest(operation);
4648
}
4749

4850
module.exports = {
4951
intialize: intialize,
50-
sendRequest: sendRequest,
51-
newRequestOptions: newRequestOptions
52+
sendRequest: sendRequest
5253
};

test-basic/test-internal.js

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,70 +26,83 @@ describe('internal tests', function() {
2626
done();
2727
}));
2828

29-
it('test internal object exists', function(done){
30-
29+
it('test internal sendRequest GET', function(done){
3130
try {
32-
should.exist(dbWriter.internal);
33-
done();
31+
dbWriter.internal.sendRequest(
32+
"/v1/internal/forestinfo",
33+
requestOptions => {
34+
requestOptions.method = "GET";
35+
requestOptions.headers = {"Accept": "application/json"}
36+
}
37+
)
38+
.result(function(response){
39+
for(let i=0; i<response.length; i++){
40+
should.exist(response[i].host);
41+
should.exist(response[i].name);
42+
should.exist(response[i].id);
43+
}
44+
}).then(() => done())
45+
.catch(error=>done(error));
3446
} catch(error){
3547
done(error);
3648
}
3749
});
3850

39-
it('test newRequestOptions creation', function(done){
40-
const requestOptions = dbWriter.internal.newRequestOptions('/v1/ping');
41-
requestOptions.method = 'POST';
42-
requestOptions.headers = {
43-
'Accept': 'application/json',
44-
'Content-Type': 'application/json'
45-
};
51+
it('test internal sendRequest POST', function(done){
4652
try {
47-
should.deepEqual(requestOptions.path, '/v1/ping');
48-
should.deepEqual(requestOptions.headers.Accept, 'application/json');
49-
should.deepEqual(requestOptions.headers['Content-Type'], 'application/json');
50-
done();
53+
dbWriter.internal.sendRequest(
54+
"/v1/search",
55+
requestOptions => {
56+
requestOptions.method = "POST";
57+
requestOptions.headers = {"Accept": "application/json", "Content-type": "application/json"};
58+
},
59+
null
60+
)
61+
.result(function(response){
62+
should.exist(response.total);
63+
should.exist(response.start);
64+
should.exist(response['page-length']);
65+
for(let i=0; i<response.length; i++){
66+
should.exist(response.results[i]);
67+
}
68+
})
69+
.then(() => done())
70+
.catch(error=>done(error));
5171
} catch(error){
5272
done(error);
5373
}
5474
});
5575

56-
it('test internal sendRequest', function(done){
57-
const requestOptions = dbWriter.internal.newRequestOptions('/v1/internal/forestinfo');
58-
requestOptions.method = 'GET';
59-
requestOptions.headers = {
60-
'Accept': 'application/json'
61-
};
76+
it('test internal sendRequest without path', function(done){
6277
try {
63-
should.deepEqual(requestOptions.path, '/v1/internal/forestinfo');
64-
should.deepEqual(requestOptions.method, 'GET');
65-
should.deepEqual(requestOptions.headers.Accept, 'application/json');
66-
dbWriter.internal.sendRequest(requestOptions, 'read forestInfo','single', 'empty')
67-
.result(function(response){
68-
for(let i=0; i<response.length; i++){
69-
should.exist(response[i].host);
70-
should.exist(response[i].name);
71-
should.exist(response[i].id);
72-
}
73-
}).then(() => done())
74-
.catch(e=>done(e));
78+
dbWriter.internal.sendRequest(
79+
null,
80+
requestOptions => {
81+
requestOptions.method = "GET";
82+
requestOptions.headers = {"Accept": "application/json"}
83+
}
84+
)
7585
} catch(error){
76-
done(error);
86+
should.equal(error.message, 'Path is needed to send request.');
87+
done();
7788
}
7889
});
7990

80-
it('test newRequestOptions creation with optional database and basePath', function(done){
81-
testconfig.restWriterConnection.basePath = '/test-basePath';
82-
testconfig.restWriterConnection.database = 'test-database';
83-
dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
84-
const requestOptions = dbWriter.internal.newRequestOptions('/v1/ping');
85-
requestOptions.method = 'POST';
86-
requestOptions.headers = {
87-
'Accept': 'application/json',
88-
'Content-Type': 'application/json'
89-
};
91+
it('test internal sendRequest without optional callbacks', function(done){
9092
try {
91-
should.deepEqual(requestOptions.path, '/test-basePath/v1/ping?database=test-database');
92-
done();
93+
dbWriter.internal.sendRequest(
94+
"/v1/search"
95+
)
96+
.result(function(response){
97+
should.exist(response.total);
98+
should.exist(response.start);
99+
should.exist(response['page-length']);
100+
for(let i=0; i<response.length; i++){
101+
should.exist(response.results[i]);
102+
}
103+
})
104+
.then(() => done())
105+
.catch(error=>done(error));
93106
} catch(error){
94107
done(error);
95108
}

0 commit comments

Comments
 (0)