/*
 * webhook-test.js: Tests for instances of the Webhook transport
 *
 * (C) 2011 Marak Squires
 * MIT LICENSE
 *
 */

var path = require('path'),
    vows = require('vows'),
    fs = require('fs'),
    http = require('http'),
    https = require('https'),
    assert = require('assert'),
    winston = require('../../lib/winston'),
    helpers = require('../helpers');

var webhookTransport = new (winston.transports.Webhook)({ 
  "host": "localhost",
  "port": 8080,
  "path": "/winston-test"
});

var httpsWebhookTransport = new (winston.transports.Webhook)({
  "host": "localhost",
  "port": 8081,
  "path": "/winston-test",
  "ssl": true
});

var server = http.createServer(function (req, res) {
  res.end();
});

server.listen(8080);


var httpsServer = https.createServer({
  cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-cert.pem')),
  key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-key.pem'))
}, function (req, res) {
  res.end();
});

httpsServer.listen(8081);

vows.describe('winston/transports/webhook').addBatch({
  "An instance of the Webhook Transport": {
    "when passed valid options": {
      "should have the proper methods defined": function () {
        helpers.assertWebhook(webhookTransport);
      },
      "the log() method": helpers.testNpmLevels(webhookTransport, "should respond with true", function (ign, err, logged) {
        assert.isNull(err);
        assert.isTrue(logged);
      })
    }
  },
  "An https instance of the Webhook Transport": {
    "when passed valid options": {
      "should have the proper methods defined": function () {
        helpers.assertWebhook(httpsWebhookTransport);
      },
      "the log() method": helpers.testNpmLevels(httpsWebhookTransport, "should respond with true", function (ign, err, logged) {
        assert.isNull(err);
        assert.isTrue(logged);
      })
    }
  }
}).addBatch({
  "When the tests are over": {
    topic: function () {
      //
      // Delay destruction of the server since the 
      // WebHook transport responds before the request
      // has actually be completed.
      //
      setTimeout(this.callback, 1000);
    },
    "the server should cleanup": function () {
      server.close();
    }
  }
}).export(module);

@@ -650,56 +650,654 @@
 var 
-server = http.createServer(function (req, res) %7B
+authWebhookTransport = new (winston.transports.Webhook)(%7B%0A  %22host%22: %22localhost%22,%0A  %22port%22: 8080,%0A  %22path%22: %22/winston-auth-test%22,%0A  %22auth%22: %7B%0A    %22username%22: %22winston%22,%0A    %22password%22: %22churchill%22%0A  %7D%0A%7D);%0A%0Avar requestsAuthenticated = true;%0A%0Avar server = http.createServer(function (req, res) %7B%0A  if (req.url == '/winston-auth-test') %7B%0A    //%0A    // Test if request has been correctly authenticated%0A    //%0A    // Strip 'Basic' from Authorization header%0A    var signature = req.headers%5B'authorization'%5D.substr(6);%0A    requestsAuthenticated = requestsAuthenticated &&%0A      new Buffer(signature, 'base64').toString('utf8') == 'winston:churchill';%0A  %7D
 %0A  r
@@ -2490,24 +2490,450 @@
    %7D)%0A    %7D%0A
+  %7D,%0A  %22An http Basic Auth instance of the Webhook Transport%22: %7B%0A    %22when passed valid options%22: %7B%0A      %22should have the proper methods defined%22: function () %7B%0A        helpers.assertWebhook(authWebhookTransport);%0A      %7D,%0A      %22the log() method%22: helpers.testNpmLevels(authWebhookTransport, %22should respond with true%22, function (ign, err, logged) %7B%0A        assert.isNull(err);%0A        assert.isTrue(logged);%0A      %7D)%0A    %7D%0A
   %7D%0A%7D).addBa
@@ -3244,24 +3244,24 @@
 nction () %7B%0A
-
       server
@@ -3270,16 +3270,127 @@
 lose();%0A
+    %7D,%0A    %22requests have been correctly authenticated%22: function () %7B%0A      assert.ok(requestsAuthenticated);%0A
     %7D%0A  

Test HTTP Basic Auth in webhook transport