add working tests for gzip handling

This commit is contained in:
samanpwbb
2015-03-07 18:04:09 -05:00
parent fffb0f7df5
commit a886cdd695
6 changed files with 33 additions and 80 deletions

View File

@@ -50,7 +50,7 @@ $(MBGL)/config/%.gypi: $(MBGL) $(MBGL)/configure
.PHONY: test-suite
test-suite:
-@(`npm bin`/tape test/render.test.js | `npm bin`/tap-spec)
-@(`npm bin`/tape test/*.test.js | `npm bin`/tap-spec)
.PHONY: test-js
test-js:

View File

@@ -41,27 +41,11 @@ The first argument passed to `map.render` is an options object, all keys are opt
longitude: {longitude}, // number (coordinate), defaults to 0
bearing: {bearing}, // number (in degrees, counter-clockwise from north), defaults to 0
ratio: {ratio} // number (scale factor), defaults to 1.0,
classes: {classes} // array of strings, optional *
classes: {classes} // array of strings, optional
}
```
* More about classes: https://github.com/mapbox/mapbox-gl-js/blob/master/API.md#working-with-style-classes
## Mapbox API Access tokens
To use styles that rely on Mapbox vector tiles, you must register a [API access token](https://www.mapbox.com/developers/api/#access-tokens) on the map. Use the `setAccessToken` method before load:
```js
var map = mbgl.Map(fileSource);
var style = mapboxStyle; // includes a datasource with a reference to something like `mapbox://mapbox.mapbox-streets-v6`
map.setAccessToken(process.env.MAPBOX_ACCESS_TOKEN);
map.load(style);
map.render({}, function(err, image) {
if (err) throw err;
fs.writeFileSync('image.png', image);
});
```
_More about classes: https://github.com/mapbox/mapbox-gl-js/blob/master/API.md#working-with-style-classes_
## Testing
@@ -170,3 +154,20 @@ fileSource.cancel = function(req) {
```
Note that in reality, Mapbox GL uses two types of protocols: `asset://` for files that should be loaded from some local static system, and `http://` (and `https://`), which should be loaded from the internet. However, stylesheets are free to use other protocols too, if your implementation of FileSource supports these; e.g. you could use `s3://` to indicate that files are supposed to be loaded from S3.
## Mapbox API Access tokens
To use styles that rely on Mapbox vector tiles, you must register a [API access token](https://www.mapbox.com/developers/api/#access-tokens) on the map. Use the `setAccessToken` method before load:
```js
var map = mbgl.Map(fileSource);
var style = mapboxStyle; // includes a datasource with a reference to something like `mapbox://mapbox.mapbox-streets-v6`
map.setAccessToken(process.env.MAPBOX_ACCESS_TOKEN);
map.load(style);
map.render({}, function(err, image) {
if (err) throw err;
fs.writeFileSync('image.png', image);
});
```

View File

@@ -1,26 +0,0 @@
{
"version": 7,
"name": "Empty",
"sources": {
"mapbox": {
"type": "vector",
"url": "./fixtures/tiles-gz.tilejson",
"maxzoom": 15
}
},
"layers": [{
"id": "background",
"type": "background",
"paint": {
"background-color": "aqua"
}
}, {
"id": "water",
"type": "fill",
"source": "mapbox",
"source-layer": "water",
"paint": {
"fill-color": "red"
}
}]
}

View File

@@ -1,13 +0,0 @@
{
"bounds": [ -180, -85.0511, 180, 85.0511 ],
"center": [ 0, 0, 0 ],
"format": "pbf",
"id": "mapbox.mapbox-streets-v6-dev",
"maskLevel": 8,
"maxzoom": 15,
"minzoom": 0,
"name": "Mapbox Streets V6",
"scheme": "xyz",
"tilejson": "2.0.0",
"tiles": [ "./fixtures/tiles/{z}-{x}-{y}.vector.pbf.gz" ]
}

Binary file not shown.

View File

@@ -10,15 +10,9 @@ var mkdirp = require('mkdirp');
var http = require('http');
var request = require('request');
var st = require('st');
/*
FIX: In all cases style-gz fails, and in all cases, style.json passes,
regardless of how gzip is set in either st or request.
*/
var style = require('../test/fixtures/style.json');
// var style = require('../test/fixtures/style-gz.json');
mkdirp.sync('results');
mkdirp.sync('test/results');
var server = http.createServer(st({path: __dirname}));
server.listen(2900);
@@ -34,7 +28,10 @@ function getFileSource(gzip, t) {
request({
url: 'http://localhost:2900' + path.join('/', req.url),
encoding: null,
gzip: gzip
gzip: gzip,
headers: {
'Accept-Encoding': 'gzip'
}
}, function (err, res, body) {
t.error(err);
var response = {};
@@ -52,42 +49,36 @@ test('before tests', function(t) {
server.listen(2900, t.end);
});
test('gzip passes', function(t) {
test('unpacked gzip', function(t) {
/*
FIX: I think need to turn OFF this event listener before this test
is done so next test doesn't accidentally use it.
*/
mbgl.on('message', function(msg) {
t.error(msg);
t.error(msg, 'doesn\'t emit warning');
});
setup(getFileSource(true, t), function(map) {
map.load(style);
map.render({}, function(err, image) {
mbgl.compressPNG(image, function(err, data) {
fs.writeFileSync('results/image-pgf-gz-false.png', data);
fs.writeFileSync('test/results/image-gz-success.png', data);
mbgl.removeAllListeners('message');
t.end();
});
});
});
});
test('gzip fails', function(t) {
test('unhandled gzip', function(t) {
/*
FIX: No message will be emmitted if gzip doesn't fail,
so this test is pointless
*/
mbgl.on('message', function(msg) {
t.equal(msg === null, 'gzipped pbf fails when gzip:false');
t.ok(msg, 'emits warning');
});
setup(getFileSource(false, t), function(map) {
map.load(style);
map.render({}, function(err, image) {
mbgl.compressPNG(image, function(err, data) {
fs.writeFileSync('results/image-pgf-gz-true.png', data);
fs.writeFileSync('test/results/image-gz-fail.png', data);
t.ok(fs.statSync('test/results/image-gz-success.png')['size'] > fs.statSync('test/results/image-gz-fail.png')['size'], 'render is smaller than unpacked render');
server.close(t.end);
});
});