<commit_before>var test = require('tinytap'),
    http = require('http'),
    net = require('net');

test('client-basic', function (t) {
  // test based loosely on http://nodejs.org/api/http.html#http_http_request_options_callback
  var req = http.request({
    hostname: 'httpbin.org',
    path: '/post',
    method: 'POST'
  }, function(res) {
    t.ok(res.statusCode, "has status");
    t.ok(res.headers, "has headers");
    t.equal(res.statusCode, 200, "got expected status");
    
    //res.setEncoding('utf8');
    res.on('data', function (chunk) {
      // WORKAROUND: https://github.com/tessel/runtime/issues/363
      if (typeof chunk !== 'string') chunk = chunk.toString();
      
      // NOTE: assumes single packet…
      var data = JSON.parse(chunk);
      t.equal(data.data, "HELLO WORLD");
      t.end();
    });
  });
  
  req.on('response', function (res) {
    t.ok(res, "got response event");
  });
  req.on('error', function(e) {
    t.fail("unexpected request error");
  });
  
  req.setHeader('Content-Type', "text/plain");
  req.setHeader('Content-Length', 11);    // httpbin doesn't handle chunked…
  req.end("HELLO WORLD");
});

test('server-basic', function (t) {
  // based on http://nodejs.org/ homepage example
  
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(0, function () {
    test(this.address().port);
  });
  function test(port) {
    http.get({port:port}, function(res) {
      t.equal(res.statusCode, 200, "got expected status");
      t.equal(res.headers['content-type'], 'text/plain', "got expected type");
      res.on('data', function (chunk) {
        // NOTE: assumes single packet…
        t.equal(chunk.length, 12);
        t.equal(chunk[0], "Hello World\n".charCodeAt(0));
        t.equal(chunk[11], "Hello World\n".charCodeAt(11));
      });
      res.on('end', function () {
        t.pass("request ended");
        t.end();
      });
    });
  }
});

test('client-errors', function (t) {
  var expect = 2;
  http.get("http://example.invalid").on('error', function (e) {
    t.ok(e, "expected error");
    if (!--expect) t.end();
  });
  
  net.createServer(function (socket) {
    socket.end([
      "HTTP/1.1 200 OK",
      "Transfer-Encoding: chunked",
      '',
      "garbage"
    ].join('\r\n'));
    this.close();
  }).listen(0, function () {
    http.get({port:this.address().port}).on('error', function (e) {
      t.ok(e, "expected error");
      if (!--expect) t.end();
    });
  });
});

test('server-errors', function (t) {
  var expect = 2;
  http.createServer(function (req, res) {
    res.end();
    req.socket.close();     // WORKAROUND: https://github.com/tessel/runtime/issues/336
  }).on('clientError', function (e,s) {
    t.ok(e && s, "expected params");
    if (!--expect) t.end();
  }).listen(0, function () {
    test(this.address().port);
  });
  function test(port) {
    net.connect(port, function () {
      this.end("garbage\n\n\n");      // TODO: why does it take server so long to receive this?!
    });
    http.request({port:port, method:'post', headers:{'Content-Length': 42}}).end();
  }
});

test('continue', function (t) {
return t.end();  // TODO: troubleshoot hang
  var expect = 3;
  http.createServer(function (req, res) {
    res.setHeader('X-Things', [1,2]);
    res.end("DATA");
  }).on('checkContinue', function (req,res) {
    t.ok(req && res, "expected params");
    if (!--expect) t.end();
    res.writeContinue();
  }).listen(0, function () {
    http.request({port:this.address().port, headers: {expect:'100-continue'}}, function (res) {
      t.ok(res, "got response");
      if (!--expect) t.end();
    }).on('continue', function () {
      t.pass("got continue");
      if (!--expect) t.end();
      this.end();<commit_msg>fix up continue test, if checkContinue handled there is _not supposed to be_ any request event<commit_after>var test = require('tinytap'),
    http = require('http'),
    net = require('net');

test('client-basic', function (t) {
  // test based loosely on http://nodejs.org/api/http.html#http_http_request_options_callback
  var req = http.request({
    hostname: 'httpbin.org',
    path: '/post',
    method: 'POST'
  }, function(res) {
    t.ok(res.statusCode, "has status");
    t.ok(res.headers, "has headers");
    t.equal(res.statusCode, 200, "got expected status");
    
    //res.setEncoding('utf8');
    res.on('data', function (chunk) {
      // WORKAROUND: https://github.com/tessel/runtime/issues/363
      if (typeof chunk !== 'string') chunk = chunk.toString();
      
      // NOTE: assumes single packet…
      var data = JSON.parse(chunk);
      t.equal(data.data, "HELLO WORLD");
      t.end();
    });
  });
  
  req.on('response', function (res) {
    t.ok(res, "got response event");
  });
  req.on('error', function(e) {
    t.fail("unexpected request error");
  });
  
  req.setHeader('Content-Type', "text/plain");
  req.setHeader('Content-Length', 11);    // httpbin doesn't handle chunked…
  req.end("HELLO WORLD");
});

test('server-basic', function (t) {
  // based on http://nodejs.org/ homepage example
  
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(0, function () {
    test(this.address().port);
  });
  function test(port) {
    http.get({port:port}, function(res) {
      t.equal(res.statusCode, 200, "got expected status");
      t.equal(res.headers['content-type'], 'text/plain', "got expected type");
      res.on('data', function (chunk) {
        // NOTE: assumes single packet…
        t.equal(chunk.length, 12);
        t.equal(chunk[0], "Hello World\n".charCodeAt(0));
        t.equal(chunk[11], "Hello World\n".charCodeAt(11));
      });
      res.on('end', function () {
        t.pass("request ended");
        t.end();
      });
    });
  }
});

test('client-errors', function (t) {
  var expect = 2;
  http.get("http://example.invalid").on('error', function (e) {
    t.ok(e, "expected error");
    if (!--expect) t.end();
  });
  
  net.createServer(function (socket) {
    socket.end([
      "HTTP/1.1 200 OK",
      "Transfer-Encoding: chunked",
      '',
      "garbage"
    ].join('\r\n'));
    this.close();
  }).listen(0, function () {
    http.get({port:this.address().port}).on('error', function (e) {
      t.ok(e, "expected error");
      if (!--expect) t.end();
    });
  });
});

test('server-errors', function (t) {
  var expect = 2;
  http.createServer(function (req, res) {
    res.end();
    req.socket.close();     // WORKAROUND: https://github.com/tessel/runtime/issues/336
  }).on('clientError', function (e,s) {
    t.ok(e && s, "expected params");
    if (!--expect) t.end();
  }).listen(0, function () {
    test(this.address().port);
  });
  function test(port) {
    net.connect(port, function () {
      this.end("garbage\n\n\n");      // TODO: why does it take server so long to receive this?!
    });
    http.request({port:port, method:'post', headers:{'Content-Length': 42}}).end();
  }
});

test('continue', function (t) {
  http.createServer().on('checkContinue', function (req,res) {
    t.ok(req && res, "expected params");
    res.writeContinue();
    res.setHeader('X-Things', [1,2]);
    res.end();
  }).listen(0, function () {
    http.request({port:this.address().port, headers: {expect:'100-continue'}}, function (res) {
      t.ok(res, "got response");
      t.equal(res.statusCode, 200, "correct status");
      t.equal(res.headers['x-things'], "1, 2", "proper headers");
      t.end();
    }).on('continue', function () {
      t.pass("got continue");
      this.end();
{'fraction_non_alphanumeric': 0.20867526377491208, 'fraction_numerical': 0.01224436628891494, 'mean_word_length': 2.9293756397134083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '2180458', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2595, 'n_words': 536}