-
Notifications
You must be signed in to change notification settings - Fork 70
/
test-https.coffee
74 lines (58 loc) · 2.09 KB
/
test-https.coffee
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
# Request library.
request = require 'request'
# HTTPS library.
https = require 'https'
# File system library.
fs = require 'fs'
# Authentication library.
auth = require '../gensrc/http-auth'
module.exports =
# Before each test.
setUp: (callback) ->
basic = auth.basic { # Configure authentication.
realm: "Private Area.",
file: __dirname + "/../data/users.htpasswd"
}
# HTTPS server options.
options = {
key: fs.readFileSync(__dirname + "/../data/server.key"),
cert: fs.readFileSync(__dirname + "/../data/server.crt")
}
# Creating new HTTPS server.
@server = https.createServer basic, options, (req, res) ->
res.end "Welcome to private area - #{req.user}!"
# Start server.
@server.listen 1337
callback()
# After each test.
tearDown: (callback) ->
@server.close() # Stop server.
callback()
# Correct encrypted details.
testSuccess: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "Welcome to private area - gevorg!"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'gevorg', 'gpass'
# Correct plain details.
testSuccessPlain: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "Welcome to private area - Sarah!"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'Sarah', 'testpass'
# Wrong password.
testWrongPassword: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "401 Unauthorized"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'gevorg', 'duck'
# Wrong user.
testWrongUser: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "401 Unauthorized"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'solomon', 'gpass'