This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtest-api.js
145 lines (134 loc) · 4.94 KB
/
test-api.js
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
/* jshint node: true, mocha: true, esversion: 6 */
var assert = require('chai').assert,
kumascript = require('..'),
ks_api = kumascript.api,
ks_server = kumascript.server,
ks_macros = kumascript.macros,
ks_test_utils = kumascript.test_utils,
testRequestExpected = ks_test_utils.testRequestExpected;
function getURL(uri) {
return 'http://localhost:9000' + uri;
}
describe('test-api', function () {
beforeEach(function() {
this.test_server = ks_test_utils.createTestServer();
this.macro_processor = new ks_macros.MacroProcessor({
macro_timeout: 500,
autorequire: {
"test_api": "autorequire-lib1"
},
loader: {
module: __dirname + '/../lib/kumascript/loaders',
class_name: 'FileLoader',
options: {
root_dir: "tests/fixtures/templates",
}
}
});
this.server = new ks_server.Server({
port: 9000,
logging: false,
document_url_template: "http://localhost:9001/documents/{path}.txt",
macro_processor: this.macro_processor
});
this.server.listen();
});
afterEach(function () {
// Kill all the servers on teardown.
this.server.close();
this.test_server.close();
});
it('A template can include the output from another with template()', function (done) {
testRequestExpected(
getURL('/docs/template-exec'),
'documents/template-exec-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
it('A template can import functions and data from another with require_macro()', function (done) {
testRequestExpected(
getURL('/docs/library-test'),
'documents/library-test-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
it('A template can import an npm module with require()', function (done) {
testRequestExpected(
getURL('/docs/require-test'),
'documents/require-test-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
it('The server can be configured to auto-require some templates', function (done) {
testRequestExpected(
getURL('/docs/autorequire'),
'documents/autorequire-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
it('The API offers access to a cache for work done in templates', function (done) {
// This is not an integration test for memcache. Instead, it just
// ensures that the FakeMemcached stub gets used. If that works, then
// the right calls should get made to memcached.
testRequestExpected(
getURL('/docs/memcache'),
'documents/memcache-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
it('The API offers access to an RSS/Atom feed parser', function (done) {
testRequestExpected(
getURL('/docs/feeds'),
'documents/feeds-expected.txt',
done,
function(resp, result, expected) {
assert.equal(result.trim(), expected.trim());
}
);
});
describe('The API offers a function to build absolute API URLs', function () {
const test_path = 'en-US/docs/<Web>?look=fancy&font=big#note';
beforeEach(function() {
this.doc_base_url = 'https://api:8000';
this.api = new ks_api.APIContext({
doc_base_url: this.doc_base_url
});
});
it('test with falsy paths', function () {
assert.equal(this.api.build_api_url(), this.doc_base_url);
assert.equal(this.api.build_api_url(''), this.doc_base_url);
assert.equal(this.api.build_api_url(null), this.doc_base_url);
assert.equal(this.api.build_api_url(undefined), this.doc_base_url);
});
it('test with "/"', function () {
assert.equal(this.api.build_api_url('/'), this.doc_base_url + '/');
});
[
test_path,
'/' + test_path,
'http://localhost:8000/' + test_path
].forEach(function (path) {
it(`test with "${path}"`, function () {
assert.equal(
this.api.build_api_url(path),
this.doc_base_url + '/' + encodeURI(test_path)
);
});
});
});
});