summaryrefslogtreecommitdiff
path: root/platform/node/test/js/request.test.js
blob: ec2b084f4e8b23f69482bd7c431eb34725ba7dc0 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';

var mockfs = require('../mockfs');
var mbgl = require('../../index');
var test = require('tape');

[ 'sprite_png', 'sprite_json', 'source_vector', 'glyph' ].forEach(function (resource) {
    test(`render reports an error when the request function responds with an error (${resource})`, function(t) {
        var map = new mbgl.Map({
            request: function(req, callback) {
                var data = mockfs.dataForRequest(req);
                if (mockfs[resource] === data) {
                    callback(new Error('message'));
                } else {
                    callback(null, { data: data });
                }
            }
        });
        map.load(mockfs.style_vector);
        map.render({ zoom: 16 }, function(err, pixels) {
            t.assert(err);
            t.assert(/message/.test(err.message));
            t.assert(!pixels);
            t.end();
        });
    });
});

[ 'vector', 'raster' ].forEach(function (type) {
    test(`render does not report an error when the request function responds with no data for a tile (${type})`, function(t) {
        var map = new mbgl.Map({
            request: function(req, callback) {
                var data = mockfs.dataForRequest(req);
                if (mockfs[`tile_${type}`] === data) {
                    callback();
                } else {
                    callback(null, { data: data });
                }
            }
        });
        map.load(mockfs[`style_${type}`]);
        map.render({ zoom: 16 }, function(err, pixels) {
            t.error(err);
            t.assert(pixels);
            t.end();
        });
    });

    test(`render reports an error when the request function responds with an error for a tile (${type})`, function(t) {
        var map = new mbgl.Map({
            request: function(req, callback) {
                var data = mockfs.dataForRequest(req);
                if (mockfs[`tile_${type}`] === data) {
                    callback(new Error('message'));
                } else {
                    callback(null, { data: data });
                }
            }
        });
        map.load(mockfs[`style_${type}`]);
        map.render({ zoom: 16 }, function(err, pixels) {
            t.assert(err);
            t.assert(/message/.test(err.message));
            t.assert(!pixels);
            t.end();
        });
    });
});

test(`render reports an error if the request function throws an exception`, function(t) {
    var map = new mbgl.Map({
        request: function() {
            throw new Error('message');
        }
    });
    map.load(mockfs.style_vector);
    map.render({ zoom: 16 }, function(err, pixels) {
        t.assert(err);
        t.assert(/message/.test(err.message));
        t.assert(!pixels);
        t.end();
    });
});

test(`render ignores request functions throwing an exception after calling the callback`, function(t) {
    var map = new mbgl.Map({
        request: function(req, callback) {
            var data = mockfs.dataForRequest(req);
            callback(null, { data: data });
            throw new Error('message');
        }
    });
    map.load(mockfs.style_vector);
    map.render({ zoom: 16 }, function(err, pixels) {
        t.error(err);
        t.assert(pixels);
        t.end();
    });
});

test(`render ignores request functions calling the callback a second time`, function(t) {
    var map = new mbgl.Map({
        request: function(req, callback) {
            var data = mockfs.dataForRequest(req);
            callback(null, { data: data });
            callback(null, { data: data });
        }
    });
    map.load(mockfs.style_vector);
    map.render({ zoom: 16 }, function(err, pixels) {
        t.error(err);
        t.assert(pixels);
        t.end();
    });
});