|
| 1 | +/* globals describe it before */ |
| 2 | +var assert = require('assert'); |
| 3 | +var FilePreviews = require('../lib'); |
| 4 | + |
| 5 | +describe('Suite', function() { |
| 6 | + describe('initialization', function() { |
| 7 | + it('should require apiKey', function() { |
| 8 | + assert.throws(function() { |
| 9 | + var fp = new FilePreviews({ apiSecret: 'secret' }); |
| 10 | + }); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should require apiSecret', function() { |
| 14 | + assert.throws(function() { |
| 15 | + var fp = new FilePreviews({ apiKey: 'key' }); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not throw if given apiKey and apiSecret', function() { |
| 20 | + assert.doesNotThrow(function() { |
| 21 | + var fp = new FilePreviews({ apiKey: 'key', apiSecret: 'secret' }); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should set apiKey and apiSecret', function() { |
| 26 | + var fp = new FilePreviews({ apiKey: 'key', apiSecret: 'secret' }); |
| 27 | + assert.equal(fp.apiKey, 'key'); |
| 28 | + assert.equal(fp.apiSecret, 'secret'); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('#getAPIRequestData()', function() { |
| 33 | + var fp; |
| 34 | + before(function() { |
| 35 | + fp = new FilePreviews({ apiKey: 'key', apiSecret: 'secret' }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should work without options', function() { |
| 39 | + var url = 'http://example.com'; |
| 40 | + var result = fp.getAPIRequestData(url); |
| 41 | + assert.equal(result.url, url); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should work with options', function() { |
| 45 | + var url = 'http://example.com'; |
| 46 | + var options = { |
| 47 | + format: 'jpg' |
| 48 | + }; |
| 49 | + |
| 50 | + var result = fp.getAPIRequestData(url, options); |
| 51 | + assert.equal(result.url, url); |
| 52 | + assert.equal(result.format, options.format); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should concantenate height and width', function() { |
| 56 | + var url = 'http://example.com'; |
| 57 | + var options = { |
| 58 | + size: { |
| 59 | + width: 1, |
| 60 | + height: 2 |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + var result = fp.getAPIRequestData(url, options); |
| 65 | + assert.equal(result.sizes[0], '1x2'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should handle width only', function() { |
| 69 | + var url = 'http://example.com'; |
| 70 | + var options = { |
| 71 | + size: { |
| 72 | + width: 1 |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + var result = fp.getAPIRequestData(url, options); |
| 77 | + assert.equal(result.sizes[0], '1'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle height only', function() { |
| 81 | + var url = 'http://example.com'; |
| 82 | + var options = { |
| 83 | + size: { |
| 84 | + height: 2 |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + var result = fp.getAPIRequestData(url, options); |
| 89 | + assert.equal(result.sizes[0], 'x2'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('#generate()', function() { |
| 94 | + var fp; |
| 95 | + before(function() { |
| 96 | + fp = new FilePreviews({ apiKey: 'key', apiSecret: 'secret' }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should format request correctly', function() { |
| 100 | + fp._request = function(request, callback) { |
| 101 | + var url = request.url.split('/'); |
| 102 | + |
| 103 | + assert.equal(request.auth.username, 'key'); |
| 104 | + assert.equal(request.auth.password, 'secret'); |
| 105 | + |
| 106 | + assert.equal(request.method, 'POST'); |
| 107 | + assert.equal(request.json, true); |
| 108 | + |
| 109 | + assert.equal(url[3], 'v2'); |
| 110 | + assert.equal(url[4], 'previews'); |
| 111 | + }; |
| 112 | + fp.generate('my-preview-id'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle success', function() { |
| 116 | + fp._request = function(request, callback) { |
| 117 | + callback(null, { statusCode: 200 }, 'Yei!'); |
| 118 | + }; |
| 119 | + fp.retrieve('my-preview-id', function(err, result) { |
| 120 | + assert.equal(err, null); |
| 121 | + assert.equal(result, 'Yei!'); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle error', function() { |
| 126 | + fp._request = function(request, callback) { |
| 127 | + callback('error', { statusCode: 400 }, 'Some Error'); |
| 128 | + }; |
| 129 | + fp.retrieve('my-preview-id', function(err, result) { |
| 130 | + assert.equal(err, 'Some Error'); |
| 131 | + assert.equal(result, null); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('#retrieve()', function() { |
| 137 | + var fp; |
| 138 | + before(function() { |
| 139 | + fp = new FilePreviews({ apiKey: 'key', apiSecret: 'secret' }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should format request correctly', function() { |
| 143 | + fp._request = function(request, callback) { |
| 144 | + var url = request.url.split('/'); |
| 145 | + |
| 146 | + assert.equal(request.auth.username, 'key'); |
| 147 | + assert.equal(request.auth.password, 'secret'); |
| 148 | + |
| 149 | + assert.equal(request.method, 'GET'); |
| 150 | + assert.equal(request.json, true); |
| 151 | + |
| 152 | + assert.equal(url[3], 'v2'); |
| 153 | + assert.equal(url[4], 'previews'); |
| 154 | + assert.equal(url[5], 'my-preview-id'); |
| 155 | + }; |
| 156 | + fp.retrieve('my-preview-id'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should handle success', function() { |
| 160 | + fp._request = function(request, callback) { |
| 161 | + callback(null, { statusCode: 200 }, 'Yei!'); |
| 162 | + }; |
| 163 | + fp.retrieve('my-preview-id', function(err, result) { |
| 164 | + assert.equal(err, null); |
| 165 | + assert.equal(result, 'Yei!'); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should handle error', function() { |
| 170 | + fp._request = function(request, callback) { |
| 171 | + callback('error', { statusCode: 400 }, 'Some Error'); |
| 172 | + }; |
| 173 | + fp.retrieve('my-preview-id', function(err, result) { |
| 174 | + assert.equal(err, 'Some Error'); |
| 175 | + assert.equal(result, null); |
| 176 | + }); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments