<commit_before>var EventSource = require('eventsource')
  , http = require('http')
  , https = require('https')
  , fs = require('fs');

var port = 20000;
function createServer(chunks, callback, onreq, secure) {
    var options = {};
    var isSecure = onreq === true || secure === true;
    if (isSecure) {
        options = {
            key: fs.readFileSync(__dirname + '/key.pem'),
            cert: fs.readFileSync(__dirname + '/certificate.pem')
        };
    }
    var responses = [];
    function open(req, res) {
        if (typeof onreq == 'function' && onreq(req, res) === true) return;
        res.writeHead(200, {'Content-Type': 'text/event-stream'});
        chunks.forEach(function(chunk) { res.write(chunk); });
        res.write(':'); // send a dummy comment to ensure that the head is flushed
        responses.push(res);
    }
    function close(closed) {
        responses.forEach(function(res) {
            res.end();
        });
        server.on('close', closed);
        server.close();
    }
    var server;
    if (isSecure) server = https.createServer(options, open);
    else server = http.createServer(open);
    server.listen(port, function() {
        callback(close);
    });
};

exports['Messages'] = {
    setUp: function(done) {
        port++;
        done();
    },

    'one one-line message in one chunk': function(test) {
        createServer(["data: Hello\n\n"], function(close) {
            var es = new EventSource('http://localhost:' + port);
            es.onmessage = function(m) {<commit_msg>Added passing test for #18<commit_after>var EventSource = require('eventsource')
  , http = require('http')
  , https = require('https')
  , fs = require('fs');

var port = 20000;
function createServer(chunks, callback, onreq, secure) {
    var options = {};
    var isSecure = onreq === true || secure === true;
    if (isSecure) {
        options = {
            key: fs.readFileSync(__dirname + '/key.pem'),
            cert: fs.readFileSync(__dirname + '/certificate.pem')
        };
    }
    var responses = [];
    function open(req, res) {
        if (typeof onreq == 'function' && onreq(req, res) === true) return;
        res.writeHead(200, {'Content-Type': 'text/event-stream'});
        chunks.forEach(function(chunk) { res.write(chunk); });
        res.write(':'); // send a dummy comment to ensure that the head is flushed
        responses.push(res);
    }
    function close(closed) {
        responses.forEach(function(res) {
            res.end();
        });
        server.on('close', closed);
        server.close();
    }
    var server;
    if (isSecure) server = https.createServer(options, open);
    else server = http.createServer(open);
    server.listen(port, function() {
        callback(close);
    });
};

exports['Messages'] = {
    setUp: function(done) {
        port++;
        done();
    },

    'issue-18': function(test) {
        createServer(["id: 1\ndata: hello world\n\n"], function(close) {
            var es = new EventSource('http://localhost:' + port);
            es.onmessage = function(m) {
                test.equal("hello world", m.data);
                es.close();
                close(test.done);
            };
        });
    },

    'one one-line message in one chunk': function(test) {
        createServer(["data: Hello\n\n"], function(close) {
            var es = new EventSource('http://localhost:' + port);
            es.onmessage = function(m) {
{'fraction_non_alphanumeric': 0.16107576633892423, 'fraction_numerical': 0.006072874493927126, 'mean_word_length': 2.329162656400385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2940007', 'n_tokens_mistral': 1043, 'n_tokens_neox': 1024, 'n_words': 237}