๐Ÿ“ฆ hapticdata / connect-favicon-canvas

๐Ÿ“„ spec-favicon-canvas.js ยท 85 lines
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
85var assert = require('assert'),
    Buffer = require('buffer').Buffer,
    faviconCanvas = require('../favicon-canvas'),
    sketch,
    rand;

//get a random element out of an Array
rand = function( arr ){
    return arr[ ~~(Math.random()*arr.length) ];
};
//draw a basic design with the canvas2d api
sketch = function( canvas, next ){
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = rand(['aquamarine','teal','red','yellow','lime']);
    var border = 8,
        sqSize = canvas.width - (border*2);
    ctx.fillRect( border, border, sqSize, sqSize );
    next();
};

describe('faviconCanvas(sketch)', function(){
    it('should respond with the generated image with default settings', function(done){
        var req = {
            url: '/favicon.ico'
        };

        var res = {
            writeHead: function( status, headers ){
                assert.equal( status, 200 );
                assert.equal( headers['Content-Type'], 'image/x-icon' );
                assert.ok( headers['Content-Length'] > 0 );
            },
            end: function( body ){
                assert.ok( Buffer.isBuffer(body) );
                done();
            }
        };

        var next = function(err){
            done('next should not have been called');
        };
        faviconCanvas( sketch )( req, res, next );
    });

    it('should let the request pass through to next without error', function(done){
        var req = { url: '/about' },
            res = {
                writeHead: function( status, headers ){
                    done( Error('res.writeHead called when not the correct path') );
                },
                end: function(){
                    done( Error('res.end called when not correct path') );
                }
            },
            next = function(err){
                assert.equal( err, null );
                done();
            };
        faviconCanvas( sketch )( req, res, next );
    });

    it('should create a custom png at the given route', function( done ){
        var req = { url: '/apple-touch-icon-precomposed.png' },
            res = {
                writeHead: function( status, headers ){
                    assert.equal( status, 200 );
                    assert.equal( headers['Content-Type'], 'image/png' );
                    assert.ok( headers['Content-Length'] > 0 );
                },
                end: function( body ){
                    assert.ok( Buffer.isBuffer( body ) );
                    done();
                }
            },
            next = function( err ){
                done( Error('Next should not have been called') );
            };

        faviconCanvas( sketch, {
            route: '/apple-touch-icon-precomposed.png',
            size: 152
        })( req, res, next );
    });
});