-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathclient.js
More file actions
178 lines (166 loc) · 5.59 KB
/
client.js
File metadata and controls
178 lines (166 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var assert = require('assert');
var should = require('should');
var http = require('http');
var testconfig = require('../etc/test-config.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
// 'rest-evaluator' user can evaluate against an alternate db:
// http://docs.marklogic.com/guide/rest-dev/intro#id_72318
var connection = {
user: testconfig.restEvaluatorConnection.user,
password: testconfig.restEvaluatorConnection.password
};
Object.keys(testconfig.restWriterConnection).forEach(function (key) {
if (connection[key] === undefined) {
connection[key] = testconfig.restWriterConnection[key];
}
});
var db = marklogic.createDatabaseClient(connection);
var otherConnection = {
database: 'unittest-nodeapi',
port: '8000'
};
Object.keys(connection).forEach(function (key) {
if (otherConnection[key] === undefined) {
otherConnection[key] = connection[key];
}
});
var otherDb = marklogic.createDatabaseClient(otherConnection);
var agentConnection = {
agent: new http.Agent({ keepAlive: true, keepAliveTimeoutMsecs: 1000 })
};
Object.keys(otherConnection).forEach(function (key) {
if (agentConnection[key] === undefined) {
agentConnection[key] = otherConnection[key];
}
});
var agentDb = marklogic.createDatabaseClient(agentConnection);
describe('database clients', function () {
it('should write in a default db and read in a specified db', function (done) {
db.documents.write({
uri: '/test/database/doc1.json',
contentType: 'application/json',
content: {
id: 'database1',
value: 'Database One'
}
})
.result(function (document) {
return otherDb.documents.probe('/test/database/doc1.json').result();
})
.then(function (document) {
document.exists.should.equal(true);
done();
})
.catch(done);
});
it('should write in a specified db and read in a default db', function (done) {
otherDb.documents.write({
uri: '/test/database/doc2.json',
contentType: 'application/json',
content: {
id: 'database2',
value: 'Database Two'
}
})
.result(function (document) {
return db.documents.probe('/test/database/doc2.json').result();
})
.then(function (document) {
document.exists.should.equal(true);
done();
})
.catch(done);
});
it('should use a default agent', function (done) {
otherDb.connectionParams.agent.options.keepAlive.should.equal(true);
done();
});
it('should use a custom agent', function (done) {
try {
agentDb.connectionParams.agent.options.keepAliveTimeoutMsecs.should.equal(1000);
done();
} catch(e){
done(e);
}
});
it('should create a timestamp', function (done) {
let timestamp = db.createTimestamp('123');
try {
timestamp.value.should.equal('123');
done();
} catch(e){
done(e);
}
});
it('should throw Error when server expects DIGEST and authType is CERTIFICATE', function (done) {
const db = marklogic.createDatabaseClient({
host: "localhost",
port: 8015,
authType: 'certificate'
});
const query = marklogic.queryBuilder
.where(marklogic.ctsQueryBuilder.cts.directoryQuery('/optic/test/'));
db.documents.query(query)
.result(function (documents) {
documents.forEach(function (document) {
});
done(new Error('Expecting an error to be thrown due to invalid authentication configuration'));
})
.catch(error => {
assert(error.toString().includes('response with invalid 401 status with path: /v1/search'));
marklogic.releaseClient(db);
done();
});
});
it('rejectUnauthorized param should be populated correctly', function (done) {
const connectionOptions = {
host: testconfig.restWriterConnection.host,
port: testconfig.restWriterConnection.port,
user: testconfig.restWriterConnection.user,
password: testconfig.restWriterConnection.password,
rejectUnauthorized: false,
ssl: true
};
let databaseClient = marklogic.createDatabaseClient(connectionOptions);
try {
assert(databaseClient);
assert(databaseClient.connectionParams.rejectUnauthorized === false);
done();
} catch (error) {
done(error);
}
});
it('should throw Error when SSL is used and server does not use SSL', function(done) {
const db = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithSsl);
db.documents.write({
uri: '/test/write/string1.json',
contentType: 'application/json',
content: '{"key1":"value 1"}'
})
.result(function (document) {
done(new Error('Expecting an error to be thrown due to invalid SSL configuration'));
})
.catch(error => {
try{
assert(error.message.toString().includes('You have attempted to access an HTTP server using HTTPS. Please check your configuration.') ||
error.message.toString().includes('write EPROTO'));
done();
} catch(err){
done(err);
}
});
});
it('should throw error when authType is OAuth and oauthToken is missing', function(done){
try {
marklogic.createDatabaseClient(testconfig.restConnectionForOauth);
done(new Error('Expecting an error to be thrown due to missing oauthToken'));
} catch(error){
assert(error.message.toString().includes('oauthToken required for OAuth authentication. '));
done();
}
});
});