-
Notifications
You must be signed in to change notification settings - Fork 70
/
https.js
103 lines (81 loc) · 2.89 KB
/
https.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
"use strict";
// Expect module.
import {expect} from 'chai'
// Request module.
import request from 'request'
// HTTPS library.
import https from 'https'
// FS.
import fs from 'fs'
// Source.
import auth from '../gensrc/http-auth'
// HTTPS.
describe('https', function () {
let server = undefined;
before(function() {
// Configure authentication.
let basic = auth.basic({
realm: "Private Area."
}, function (username, password, done) {
if (username === 'gevorg') {
done(new Error("Error comes here"));
} else if (username === "mia" && password === "supergirl") {
done(true);
} else if (username === "ColonUser" && password === "apasswordwith:acolon") {
done(true);
} else {
done(false);
}
});
// HTTPS server options.
let options = {
key: fs.readFileSync(__dirname + "/../data/server.key"),
cert: fs.readFileSync(__dirname + "/../data/server.crt")
};
// Creating new HTTPS server.
server = https.createServer(basic, options, function (req, res) {
res.end(`Welcome to private area - ${req.user}!`);
});
// Start server.
server.listen(1337);
});
after(function() {
server.close();
});
it('error', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Error comes here");
};
// Test request.
request.get({uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth('gevorg', 'gpass');
});
it('success', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Welcome to private area - mia!");
};
// Test request.
request.get({uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth('mia', 'supergirl');
});
it('wrong password', function () {
let callback = function (error, response, body) {
expect(body).to.equal("401 Unauthorized");
};
// Test request.
request.get({uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth('mia', 'cute');
});
it('wrong user', function () {
let callback = function (error, response, body) {
expect(body).to.equal("401 Unauthorized");
};
// Test request.
request.get({uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth('Tina', 'supergirl');
});
it('password with colon', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Welcome to private area - ColonUser!");
};
// Test request.
request.get({uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth(
'ColonUser', 'apasswordwith:acolon');
});
});