|
1 | 1 | const chai = require("chai");
|
2 | 2 | const sinon = require("sinon");
|
| 3 | +const requestUtils = require('../utils/request.js'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
3 | 6 |
|
4 |
| -const expect = chai.expect; |
| 7 | +global.FormData = require('formdata-node'); |
5 | 8 |
|
| 9 | +const expect = chai.expect; |
| 10 | +const test_file_path = path.resolve(__dirname, "test_image.png"); |
6 | 11 | opts = {
|
7 | 12 | publicKey: "test_public_key",
|
8 | 13 | urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
|
@@ -42,66 +47,257 @@ describe("Testing SDK", function () {
|
42 | 47 | "height" : "300",
|
43 | 48 | "width" : "400"
|
44 | 49 | }]
|
| 50 | + }); |
| 51 | + |
| 52 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/); |
| 53 | + expect(url).to.match(new RegExp("[^\/]\/tr:")); |
| 54 | + expect(url).to.match(new RegExp(":h-300,w-400")); |
| 55 | + expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/); |
| 56 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should generate the correct url with path param with multiple leading slash', function() { |
| 60 | + const url = imagekit.url({ |
| 61 | + path: "///test_path.jpg", |
| 62 | + transformation : [{ |
| 63 | + "height" : "300", |
| 64 | + "width" : "400" |
| 65 | + }] |
45 | 66 | })
|
46 |
| - expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint/); |
47 |
| - expect(url).to.match(new RegExp("\/tr:")); |
48 |
| - expect(url).to.match(new RegExp("w-400")); |
49 |
| - expect(url).to.match(new RegExp("h-300")); |
50 |
| - expect(url).to.match(/\/test_path\.jpg[^\/][\?]?/); |
| 67 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/); |
| 68 | + expect(url).to.match(new RegExp("[^\/]\/tr:")); |
| 69 | + expect(url).to.match(new RegExp(":h-300,w-400")); |
| 70 | + expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/); |
| 71 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
| 72 | + |
| 73 | + }); |
| 74 | + |
| 75 | + it('should generate the correct url with path param with overidden urlEndpoint', function() { |
| 76 | + const url = imagekit.url({ |
| 77 | + urlEndpoint: "https://ik.imagekit.io/test_url_endpoint_alt", |
| 78 | + path: "/test_path.jpg", |
| 79 | + transformation : [{ |
| 80 | + "height" : "300", |
| 81 | + "width" : "400" |
| 82 | + }] |
| 83 | + }) |
| 84 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\_alt\/[^\/]/); |
| 85 | + expect(url).to.match(new RegExp("[^\/]\/tr:")); |
| 86 | + expect(url).to.match(new RegExp(":h-300,w-400")); |
| 87 | + expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/); |
| 88 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
| 89 | + |
| 90 | + }); |
| 91 | + |
| 92 | + it('should generate the correct url with path param with transformationPostion as query', function() { |
| 93 | + const url = imagekit.url({ |
| 94 | + path: "/test_path.jpg", |
| 95 | + transformationPosition: "query", |
| 96 | + transformation : [{ |
| 97 | + "height" : "300", |
| 98 | + "width" : "400" |
| 99 | + }] |
| 100 | + }); |
| 101 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\.jpg\?/); |
| 102 | + expect(url).to.match(new RegExp('&tr=h-300%2Cw-400')); |
| 103 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
51 | 104 | });
|
52 | 105 |
|
53 | 106 | it('should generate the correct url with src param', function() {
|
54 | 107 | const url = imagekit.url({
|
55 |
| - src : "https://ik.imagekit.io/test_url_endpoint/endpoint/test_path.jpg", |
| 108 | + src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg", |
56 | 109 | transformation : [{
|
57 | 110 | "height" : "300",
|
58 | 111 | "width" : "400"
|
59 | 112 | }]
|
60 | 113 | });
|
61 |
| - expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test_url_endpoint\/endpoint\/test\_path\.jpg/); |
62 |
| - expect(url).to.match(/\&tr=/); |
63 |
| - expect(url).to.match(new RegExp("w-400")); |
64 |
| - expect(url).to.match(new RegExp("h-300")); |
| 114 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg[^\/]/); |
| 115 | + expect(url).to.match(new RegExp('&tr=h-300%2Cw-400')); |
| 116 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
65 | 117 | });
|
66 |
| - }); |
67 | 118 |
|
68 |
| - describe(".upload method", function() { |
69 |
| - const requestUtils = require('../utils/request.js'); |
70 |
| - sinon.stub(requestUtils, 'generateSignatureToken').callsFake(function (options, callback) { |
71 |
| - callback(null, { |
72 |
| - signature: "test_signature", |
73 |
| - expire: 123, |
74 |
| - token: "test_token" |
| 119 | + it('should generate the correct url with transformationPostion as query', function() { |
| 120 | + const url = imagekit.url({ |
| 121 | + src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg", |
| 122 | + transformationPosition: "query", |
| 123 | + transformation : [{ |
| 124 | + "height" : "300", |
| 125 | + "width" : "400" |
| 126 | + }] |
| 127 | + }); |
| 128 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg\?/); |
| 129 | + expect(url).to.match(new RegExp('&tr=h-300%2Cw-400')); |
| 130 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should generate the correct url with query params properly merged', function() { |
| 134 | + const url = imagekit.url({ |
| 135 | + src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?t1=v1", |
| 136 | + queryParameters: {t2: "v2", t3: "v3"}, |
| 137 | + transformation : [{ |
| 138 | + "height" : "300", |
| 139 | + "width" : "400" |
| 140 | + }] |
| 141 | + }); |
| 142 | + |
| 143 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg\?t1=v1\&/); |
| 144 | + expect(url).to.match(new RegExp('&t2=v2&')); |
| 145 | + expect(url).to.match(new RegExp('&t3=v3&')); |
| 146 | + expect(url).to.match(new RegExp('&tr=h-300%2Cw-400')); |
| 147 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
| 148 | + }); |
| 149 | + |
| 150 | + |
| 151 | + it('should generate the correct chained transformation', function() { |
| 152 | + const url = imagekit.url({ |
| 153 | + path: "/test_path.jpg", |
| 154 | + transformation : [{ |
| 155 | + "height" : "300", |
| 156 | + "width" : "400" |
| 157 | + }, { |
| 158 | + "rt": "90" |
| 159 | + }] |
75 | 160 | })
|
| 161 | + |
| 162 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/); |
| 163 | + expect(url).to.match(new RegExp("[^\/]\/tr:")); |
| 164 | + expect(url).to.match(new RegExp(":h-300,w-400:rt-90")); |
| 165 | + expect(url).to.not.match(new RegExp(":rt-90:h-300,w-400")); |
| 166 | + expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/); |
| 167 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
76 | 168 | });
|
77 | 169 |
|
78 |
| - sinon.stub(requestUtils, 'uploadFile').callsFake(function (options, callback) { |
79 |
| - callback(null, "success") |
| 170 | + |
| 171 | + it('should generate the correct chained transformation url with new undocumented tranforamtion parameter', function() { |
| 172 | + const url = imagekit.url({ |
| 173 | + path: "/test_path.jpg", |
| 174 | + transformation : [{ |
| 175 | + "height" : "300", |
| 176 | + "width" : "400" |
| 177 | + }, { |
| 178 | + "rndm_trnsf": "abcd" |
| 179 | + }] |
| 180 | + }) |
| 181 | + |
| 182 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/); |
| 183 | + expect(url).to.match(new RegExp("[^\/]\/tr:")); |
| 184 | + expect(url).to.match(new RegExp(":h-300,w-400:rndm_trnsf-abcd")); |
| 185 | + expect(url).to.not.match(new RegExp(":rndm_trnsf-abcd:h-300,w-400")); |
| 186 | + expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/); |
| 187 | + expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/); |
80 | 188 | });
|
| 189 | + |
| 190 | + }); |
| 191 | + |
| 192 | + describe(".upload method", function() { |
| 193 | + let orderCop; |
81 | 194 |
|
82 |
| - global.FormData = require('formdata-node'); |
| 195 | + before(function() { |
| 196 | + orderCop = sinon.spy(); |
| 197 | + sinon.stub(requestUtils, 'generateSignatureToken').callsFake(function (options, callback) { |
| 198 | + orderCop('signatureCall'); |
| 199 | + callback(null, { |
| 200 | + signature: "test_signature", |
| 201 | + expire: 123, |
| 202 | + token: "test_token" |
| 203 | + }); |
| 204 | + }); |
| 205 | + sinon.stub(requestUtils, 'uploadFile').callsFake(function (options, callback) { |
| 206 | + orderCop('uploadCall'); |
| 207 | + callback(null, "success"); |
| 208 | + }); |
83 | 209 |
|
| 210 | + }); |
84 | 211 |
|
85 |
| - const fileOptions = { |
86 |
| - fileName: "test_file_name", |
87 |
| - file: "test_file", |
88 |
| - }; |
| 212 | + beforeEach(function() { |
| 213 | + orderCop.resetHistory(); |
| 214 | + requestUtils.generateSignatureToken.resetHistory(); |
| 215 | + requestUtils.uploadFile.resetHistory(); |
| 216 | + }) |
89 | 217 |
|
90 | 218 | it('calls relavant functions in order', function() {
|
| 219 | + const fileOptions = { |
| 220 | + fileName: "test_file_name", |
| 221 | + file: "test_file", |
| 222 | + }; |
91 | 223 | imagekit.upload(fileOptions, function(){});
|
| 224 | + |
92 | 225 | expect(requestUtils.generateSignatureToken.calledOnce).to.be.true;
|
93 | 226 | expect(requestUtils.uploadFile.calledOnce).to.be.true;
|
| 227 | + expect(orderCop.firstCall.args[0]).to.be.equal('signatureCall'); |
| 228 | + expect(orderCop.secondCall.args[0]).to.be.equal('uploadCall'); |
94 | 229 | });
|
95 | 230 |
|
96 | 231 | it('calls functions with correct arguments', function() {
|
| 232 | + |
| 233 | + const fileOptions = { |
| 234 | + fileName: "test_file_name", |
| 235 | + file: "test_file", |
| 236 | + tags : ["test_tag1", "test_tag2"], |
| 237 | + customCoordinates: "test_customCoordinates", |
| 238 | + responseFields: "tags, customCoordinates, isPrivateFile, metadata", |
| 239 | + useUniqueFileName: true |
| 240 | + }; |
| 241 | + imagekit.upload(fileOptions, function(){}); |
| 242 | + |
97 | 243 | const arg = requestUtils.uploadFile.getCall(0).args[0];
|
98 | 244 | expect(arg.get('file')).to.be.equal("test_file");
|
99 | 245 | expect(arg.get('fileName')).to.be.equal("test_file_name");
|
100 | 246 | expect(arg.get('token')).to.be.equal("test_token");
|
101 | 247 | expect(arg.get('expire')).to.be.equal("123");
|
102 | 248 | expect(arg.get('signature')).to.be.equal("test_signature");
|
103 |
| - }); |
| 249 | + expect(arg.get('tags')).to.match(new RegExp("test_tag1")); |
| 250 | + expect(arg.get('tags')).to.match(new RegExp("test_tag2")); |
| 251 | + expect(arg.get('customCoordinates')).to.match(new RegExp("test_customCoordinates")); |
| 252 | + expect(arg.get('responseFields')).to.match(new RegExp("tags, customCoordinates, isPrivateFile, metadata")); |
| 253 | + expect(arg.get('useUniqueFileName')).to.be.equal('true'); |
| 254 | + }); |
104 | 255 |
|
| 256 | + it('calls functions with correct arguments for missing parameters', function() { |
| 257 | + const fileOptions = { |
| 258 | + fileName: "test_file_name", |
| 259 | + file: "test_file", |
| 260 | + }; |
| 261 | + imagekit.upload(fileOptions, function(){}); |
| 262 | + |
| 263 | + const arg = requestUtils.uploadFile.getCall(0).args[0]; |
| 264 | + expect(arg.get('file')).to.be.equal("test_file"); |
| 265 | + expect(arg.get('fileName')).to.be.equal("test_file_name"); |
| 266 | + expect(arg.get('tags')).to.not.equal("undefined"); |
| 267 | + expect(arg.get('customCoordinates')).to.be.equal(undefined); |
| 268 | + expect(arg.get('customCoordinates')).to.not.equal("undefined"); |
| 269 | + expect(arg.get('responseFields')).to.be.equal(undefined); |
| 270 | + expect(arg.get('responseFields')).to.not.equal("undefined"); |
| 271 | + expect(arg.get('useUniqueFileName')).to.be.equal(undefined); |
| 272 | + expect(arg.get('useUniqueFileName')).to.not.equal("undefined"); |
| 273 | + }); |
| 274 | + |
| 275 | + it('calls functions with base64 file argument', function() { |
| 276 | + |
| 277 | + const fileOptions = { |
| 278 | + fileName: "test_file_name", |
| 279 | + file: fs.readFileSync(test_file_path, 'base64'), |
| 280 | + }; |
| 281 | + imagekit.upload(fileOptions, function(){}); |
| 282 | + |
| 283 | + const arg = requestUtils.uploadFile.getCall(0).args[0]; |
| 284 | + expect(arg.get('file')).to.match(/^[a-zA-Z0-9\+/]*={0,2}$/); |
| 285 | + expect(arg.get('fileName')).to.be.equal("test_file_name"); |
| 286 | + }); |
| 287 | + |
| 288 | + it('calls functions with url file argument', function() { |
| 289 | + |
| 290 | + |
| 291 | + const fileOptions = { |
| 292 | + fileName: "test_file_name", |
| 293 | + file: "http://test.url", |
| 294 | + }; |
| 295 | + imagekit.upload(fileOptions, function(){}); |
| 296 | + |
| 297 | + const arg = requestUtils.uploadFile.getCall(0).args[0]; |
| 298 | + expect(arg.get('file')).to.be.equal("http://test.url"); |
| 299 | + expect(arg.get('fileName')).to.be.equal("test_file_name"); |
| 300 | + }); |
105 | 301 | });
|
106 | 302 | });
|
107 | 303 | });
|
|
0 commit comments