http-auth
Version:
Node.js package for HTTP basic and digest access authentication.
69 lines (54 loc) • 2.06 kB
text/coffeescript
request = require 'request'
http = require 'http'
auth = require '../gensrc/http-auth'
module.exports =
setUp: (callback) ->
basic = auth.basic {
realm: "Private Area."
},
(username, password, callback) =>
success = (username is "mia" and password is "supergirl") or
(username is "ColonUser" and password is "apasswordwith:acolon")
callback.apply this, [success]
@server = http.createServer basic, (req, res) ->
res.end "Welcome to private area - #{req.user}!"
@server.listen 1337
callback()
tearDown: (callback) ->
@server.close()
callback()
testSuccess: (test) ->
callback = (error, response, body) ->
test.equals body, "Welcome to private area - mia!"
test.done()
(request.get 'http://127.0.0.1:1337', callback).auth 'mia', 'supergirl'
testWrongPassword: (test) ->
callback = (error, response, body) ->
test.equals body, "401 Unauthorized"
test.done()
(request.get 'http://127.0.0.1:1337', callback).auth 'mia', 'cute'
testWrongUser: (test) ->
callback = (error, response, body) ->
test.equals body, "401 Unauthorized"
test.done()
(request.get 'http://127.0.0.1:1337', callback).auth 'Tina', 'supergirl'
testColonPassword: (test) ->
callback = (error, response, body) ->
test.equals body, "Welcome to private area - ColonUser!"
test.done()
(request.get 'http://127.0.0.1:1337', callback).auth 'ColonUser', 'apasswordwith:acolon'