<commit_before>var helper = require('../spec_helper');
var mqtt = require('mqtt');
var request = require('request');
var coap = require('coap');

describe('Application', function () {
	var app, server, coapServer, mqttServer;
	before(function () {
		app = helper.globalSetup();
		server = app.listen(8899, function () {});
		coapServer = coap.createServer(app.coap).listen(5683, function () {});
		mqttServer = mqtt.createServer(app.mqtt).listen(1883, function () {});
	});

	after(function () {
		server.close();
		coapServer.close();
		mqttServer.close();
	});

	describe("MQTT Server", function () {
		it('should able connect to mqtt server', function (done) {
      var client = mqtt.createClient(1883, '127.0.0.1', {
        username: 'root',
        password: '<PASSWORD>'
      });

			client.on('connect', function () {
				client.publish('hello', 'coap');
				client.end();
				done();
			});
		});

	});

	describe("HTTP Server", function () {
		it('should able connect to http server', function (done) {
			request('http://localhost:8899', function (error, response, body) {
				if (response.statusCode === 200) {
					done();
				}
			})
		});

		it('should able put response', function (done) {
			request({
        uri: 'http://phodal:phodal@localhost:8899/topics/test',
        method: 'PUT',
        multipart: [
          {
            'content-type': 'application/json',
            body: JSON.stringify({
              foo: 'bar',
              _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain'}}
            })
          },
          { body: 'I am an attachment' }]
      }, function (error, response, body) {
        console.log(body);
				if (response.statusCode) {
					done();
				}
			})
		});

		it('should able put response', function (done) {
			request.put('http://localhost:8899/topics/test', {
        'auth': {
          'username': 'phodal',
          'password': '<PASSWORD>',
          'sendImmediately': true
        }
      }, function (error, response, body) {
				if (response.statusCode === 204) {
					done();
				}
			})
		});

		it('should able post response', function (done) {
			request({
				uri: 'http://localhost:8899/topics/test',
				method: 'POST',
        'auth': {
          'username': 'phodal',
          'password': '<PASSWORD>',
          'sendImmediately': true
        }
			}, function (error, response, body) {
				if (response.statusCode === 204) {
					done();
				}
			})
		});
	});

	describe("CoAP Server", function () {
		it('should able connect to coap server', function (done) {
			var req = coap.request('coap://localhost/hello');
			var result = {"method": "get"};

			req.on('response', function (res) {
				var response_result = JSON.parse(res.payload.toString());
				if (response_result.method === result.method) {
					done();
				}
			});

			req.end();
		});
	});
});<commit_msg>add test for coap put with auth<commit_after>var helper = require('../spec_helper');
var mqtt = require('mqtt');
var request = require('request');
var coap = require('coap');

describe('Application', function () {
	var app, server, coapServer, mqttServer;
	before(function () {
		app = helper.globalSetup();
		server = app.listen(8899, function () {});
		coapServer = coap.createServer(app.coap).listen(5683, function () {});
		mqttServer = mqtt.createServer(app.mqtt).listen(1883, function () {});
	});

	after(function () {
		server.close();
		coapServer.close();
		mqttServer.close();
	});

	describe("MQTT Server", function () {
		it('should able connect to mqtt server', function (done) {
      var client = mqtt.createClient(1883, '127.0.0.1', {
        username: 'root',
        password: '<PASSWORD>'
      });

			client.on('connect', function () {
				client.publish('hello', 'coap');
				client.end();
				done();
			});
		});

	});

	describe("HTTP Server", function () {
		it('should able connect to http server', function (done) {
			request('http://localhost:8899', function (error, response, body) {
				if (response.statusCode === 200) {
					done();
				}
			})
		});

		it('should able put response', function (done) {
			request({
        uri: 'http://phodal:phodal@localhost:8899/topics/test',
        method: 'PUT',
        multipart: [
          {
            'content-type': 'application/json',
            body: JSON.stringify({
              foo: 'bar',
              _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain'}}
            })
          },
          { body: 'I am an attachment' }]
      }, function (error, response, body) {
        console.log(body);
				if (response.statusCode) {
					done();
				}
			})
		});

		it('should able put response', function (done) {
			request.put('http://localhost:8899/topics/test', {
        'auth': {
          'username': 'phodal',
          'password': '<PASSWORD>',
          'sendImmediately': true
        }
      }, function (error, response, body) {
				if (response.statusCode === 204) {
					done();
				}
			})
		});

		it('should able post response', function (done) {
			request({
				uri: 'http://localhost:8899/topics/test',
				method: 'POST',
        'auth': {
          'username': 'phodal',
          'password': '<PASSWORD>',
          'sendImmediately': true
        }
			}, function (error, response, body) {
				if (response.statusCode === 204) {
					done();
				}
			})
		});
	});

	describe("CoAP Server", function () {
		it('should able connect to coap server', function (done) {
			var req = coap.request('coap://localhost/hello');
			var result = {"method": "get"};

			req.on('response', function (res) {
				var response_result = JSON.parse(res.payload.toString());
				if (response_result.method === result.method) {
					done();
				}
			});

			req.end();
		});

    it('should abe to put data with auth', function (done) {
      var request  = coap.request;
      var bl       = require('bl');
      var req = request({hostname: 'localhost',port:5683,pathname: '',method: 'POST'});

      var payload = {
        title: 'this is a test payload',
        body: 'containing nothing useful'
      };

      req.setHeader("Accept", "application/json");
      req.setOption('Block2',  [new Buffer('phodal'), new Buffer('phodal')]);
      req.write(JSON.stringify(payload));
      req.on('response', function(res) {
        if(res.code === '2.06') {
          done();
        }
      });

      req.end();

    });
	});
});
{'fraction_non_alphanumeric': 0.18298729470096065, 'fraction_numerical': 0.01642392314843508, 'mean_word_length': 2.578159645232816, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7624617', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2006, 'n_words': 445}