-
Notifications
You must be signed in to change notification settings - Fork 70
/
test-https-exception.coffee
62 lines (48 loc) · 1.57 KB
/
test-https-exception.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
# 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."
},
(username, password, callback) =>
if username is "gevorg"
callback new Error("Error comes here")
else
callback true
# 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()
# Error should be thrown.
testError: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "Error comes here"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'gevorg', 'ggg'
# Correct encrypted details.
testSuccess: (test) ->
callback = (error, response, body) -> # Callback.
test.equals body, "Welcome to private area - valod!"
test.done()
# Test request.
(request.get {uri: 'https://127.0.0.1:1337', strictSSL: false}, callback).auth 'valod', 'sss'