Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http-meta.spec.js « test « mocha-3.1.2 « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c92dbd230755cea711487dc9e1bad18fa5609224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';

var http = require('http');

var PORT = 8889;

var server = http.createServer(function (req, res) {
  var accept = req.headers.accept || '';
  var json = ~accept.indexOf('json');

  switch (req.url) {
    case '/':
      res.end('hello');
      break;
    case '/users':
      if (json) {
        res.end('["tobi","loki","jane"]');
      } else {
        res.end('tobi, loki, jane');
      }
      break;
  }
});

function get (url, body, header) {
  return function (done) {
    http.get({
      path: url,
      port: PORT,
      headers: header || {}
    }, function (res) {
      var buf = '';
      res.should.have.property('statusCode', 200);
      res.setEncoding('utf8');
      res.on('data', function (chunk) { buf += chunk; });
      res.on('end', function () {
        buf.should.equal(body);
        done();
      });
    });
  };
}

describe('http requests', function () {
  before(function (done) {
    server.listen(PORT, done);
  });

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

  describe('GET /', function () {
    it('should respond with hello',
      get('/', 'hello'));
  });

  describe('GET /users', function () {
    it('should respond with users',
      get('/users', 'tobi, loki, jane'));

    it('should respond with users',
      get('/users', '["tobi","loki","jane"]', { Accept: 'application/json' }));
  });
});