summaryrefslogtreecommitdiff
path: root/platform/node/test/js/request_notfound.test.js
blob: d2d2812784f105c52c6782fadfeda24ea0a90377 (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
'use strict';

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

function isTile(data) {
    return data == mockfs.tile_vector || data == mockfs.tile_raster;
}

function asyncReply(callback, data) {
    setTimeout(function() { callback(null, { data: data }); }, 0);
};

function asyncFail(callback, data) {
    // Do not set an error for tile when not found. A not found
    // tile is a valid tile.
    if (isTile(data)) {
        setTimeout(function() { callback(); }, 0);
    } else {
        setTimeout(function() { callback(new Error('not found')); }, 0);
    }
};

function notfoundRequest(t, style, notfoundResource) {
    var options = {
        request: function(req, callback) {
            var data = mockfs.dataForRequest(req);

            if (notfoundResource != data) {
                asyncReply(callback, data);
            } else {
                asyncFail(callback, data);
            }
        },
        ratio: 2,
    };

    var map = new mbgl.Map(options);
    map.load(style);

    map.render({ zoom: 16 }, function(err, pixels) {
        if (err && !isTile(notfoundResource)) {
            t.pass("pass");
            return;
        }

        if (!err && isTile(notfoundResource)) {
            t.pass("pass");
            return;
        }

        t.fail("fail");
    });
};

test('Vector', function(t) {
    t.plan(5);

    notfoundRequest(t, mockfs.style_vector, mockfs.sprite_png);
    notfoundRequest(t, mockfs.style_vector, mockfs.sprite_json);
    notfoundRequest(t, mockfs.style_vector, mockfs.source_vector);
    notfoundRequest(t, mockfs.style_vector, mockfs.tile_vector);
    notfoundRequest(t, mockfs.style_vector, mockfs.glyph);
});

test('Raster', function(t) {
    t.plan(4);

    notfoundRequest(t, mockfs.style_raster, mockfs.sprite_png);
    notfoundRequest(t, mockfs.style_raster, mockfs.sprite_json);
    notfoundRequest(t, mockfs.style_raster, mockfs.source_raster);
    notfoundRequest(t, mockfs.style_raster, mockfs.tile_raster);
});