UNPKG

9.64 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('assert'),
4 when = require('when'),
5 rewire = require('rewire'),
6 lodash = require('lodash'),
7 constants = require('../lib/constants'),
8 mocks = require('./mocks'),
9 sinon = require('sinon'),
10 should = require('should');
11
12describe('AkamaiPurge', function () {
13
14 describe('basic purge request', function () {
15 beforeEach(function () {
16
17 this.requestObjects = [
18 'http://www.example.com/somewhere'
19 ];
20
21 this.requestBody = {
22 objects: this.requestObjects
23 };
24
25 this.object = rewire('./../lib/purge.js');
26 this.object.__set__('AkamaiRequest', mocks.AkamaiRequest.standardGood({
27 uri: constants.AKAMAI_API_QUEUE,
28 method: 'POST',
29 json: this.requestBody,
30 auth: {
31 username: 'someusername',
32 password: 'supersecret'
33 }
34 }, {
35 httpStatus: 201,
36 detail: 'Request accepted.',
37 progressUri: '/someProgressUri'
38 }));
39
40 this.object.__set__('AkamaiStatus', mocks.AkamaiStatus.standardGood({
41 username: 'someusername',
42 password: 'supersecret',
43 progressUri: '/someProgressUri'
44 }, {
45 httpStatus: 201,
46 detail: 'Request accepted.',
47 progressUri: '/someProgressUri'
48 }));
49 });
50
51
52 it('should make request', function (done) {
53 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects).then(function (response) {
54 assert.equal(response.httpStatus, 201);
55 assert.equal(typeof response.status, 'function');
56 assert.deepEqual(response.requestBody, this.requestBody);
57 }.bind(this)).then(done).catch(done);
58
59 assert.equal(when.isPromiseLike(purgeRequest), true);
60 });
61
62
63 it('should make request with bad options primitive type', function (done) {
64 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, 'badoptions').then(function (response) {
65 assert.equal(response.httpStatus, 201);
66 assert.equal(typeof response.status, 'function');
67 assert.deepEqual(response.requestBody, this.requestBody);
68 }.bind(this)).then(done).catch(done);
69
70 assert.equal(when.isPromiseLike(purgeRequest), true);
71 });
72
73
74 it('should make request then make status request call', function (done) {
75 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects).then(function (response) {
76 assert.equal(response.httpStatus, 201);
77 assert.equal(typeof response.status, 'function');
78 assert.deepEqual(response.requestBody, this.requestBody);
79
80 return response.status();
81 }.bind(this)).then(function (statusResponse) {
82 assert.equal(statusResponse.httpStatus, 201);
83 }).then(done).catch(done);
84
85 assert.equal(when.isPromiseLike(purgeRequest), true);
86 });
87
88
89 it('should make request without progress uri then make status request call', function (done) {
90 this.object.__set__('AkamaiRequest', mocks.AkamaiRequest.standardGood({
91 uri: constants.AKAMAI_API_QUEUE,
92 method: 'POST',
93 json: this.requestBody,
94 auth: {
95 username: 'someusername',
96 password: 'supersecret'
97 }
98 }, {
99 httpStatus: 201,
100 detail: 'Request accepted.'
101 }));
102
103 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects).then(function (response) {
104 assert.equal(response.httpStatus, 201);
105 assert.equal(typeof response.status, 'function');
106 assert.deepEqual(response.requestBody, this.requestBody);
107
108 return response.status();
109 }.bind(this)).then(function () {
110 throw new Error('Should have failed in the status call');
111 }).catch(function (err) {
112 try {
113 assert.equal(err instanceof Error, true);
114 if (/progressUri/.test(err.toString())) {
115 assert.equal(err.toString(), 'Error: Missing progressUri from response');
116 done();
117 } else {
118 done(err);
119 }
120 } catch (e) {
121 done(e);
122 }
123 });
124
125 assert.equal(when.isPromiseLike(purgeRequest), true);
126 });
127 });
128
129 describe('purge request options', function () {
130 beforeEach(function () {
131 this.object = rewire('./../lib/purge.js');
132
133 this.requestObjects = [
134 'http://www.example.com/somewhere'
135 ];
136
137 this.requestBody = {
138 objects: this.requestObjects
139 };
140
141 this.requestOptions = {
142 uri: constants.AKAMAI_API_QUEUE,
143 method: 'POST',
144 json: this.requestBody,
145 auth: {
146 username: 'someusername',
147 password: 'supersecret'
148 }
149 };
150
151 this.applyMock = function (additionalOptions) {
152 this.requestBody = lodash.assign(this.requestBody, additionalOptions);
153 this.requestOptions.json = this.requestBody;
154
155 this.object.__set__('AkamaiRequest', mocks.AkamaiRequest.standardGood(this.requestOptions, {
156 httpStatus: 201,
157 detail: 'Request accepted.',
158 progressUri: '/someProgressUri'
159 }));
160 }.bind(this);
161 });
162
163
164 it('should make request with type option given', function (done) {
165 this.applyMock({type: 'cpcode'});
166
167 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {type: 'cpcode'}).then(function (response) {
168 assert.equal(response.httpStatus, 201);
169 assert.equal(typeof response.status, 'function');
170 assert.deepEqual(response.requestBody, this.requestBody);
171 }.bind(this)).then(done).catch(done);
172
173 assert.equal(when.isPromiseLike(purgeRequest), true);
174 });
175
176
177 it('should make request with bad type option given', function (done) {
178 this.applyMock({});
179 this.object.__set__('console', {
180 warn: function (msg) {
181 assert.equal(/akamai: WARNING: Invalid purge request type\. Valid types:/.test(msg), true);
182 }
183 });
184
185 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {type: 'badtype'}).then(function (response) {
186 assert.equal(response.httpStatus, 201);
187 assert.equal(typeof response.status, 'function');
188 assert.deepEqual(response.requestBody, this.requestBody);
189 }.bind(this)).then(done).catch(done);
190
191 assert.equal(when.isPromiseLike(purgeRequest), true);
192 });
193
194
195 it('should make request with domain option given', function (done) {
196 this.applyMock({domain: 'production'});
197
198 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {domain: 'production'}).then(function (response) {
199 assert.equal(response.httpStatus, 201);
200 assert.equal(typeof response.status, 'function');
201 assert.deepEqual(response.requestBody, this.requestBody);
202 }.bind(this)).then(done).catch(done);
203
204 assert.equal(when.isPromiseLike(purgeRequest), true);
205 });
206
207
208 it('should make request with bad domain option given', function (done) {
209 this.applyMock({});
210 this.object.__set__('console', {
211 warn: function (msg) {
212 assert.equal(/akamai: WARNING: Invalid purge request domain\. Valid domains:/.test(msg), true);
213 }
214 });
215
216 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {domain: 'baddomain'}).then(function (response) {
217 assert.equal(response.httpStatus, 201);
218 assert.equal(typeof response.status, 'function');
219 assert.deepEqual(response.requestBody, this.requestBody);
220 }.bind(this)).then(done).catch(done);
221
222 assert.equal(when.isPromiseLike(purgeRequest), true);
223 });
224
225
226 it('should make request with action option given', function (done) {
227 this.applyMock({action: 'invalidate'});
228
229 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {action: 'invalidate'}).then(function (response) {
230 assert.equal(response.httpStatus, 201);
231 assert.equal(typeof response.status, 'function');
232 assert.deepEqual(response.requestBody, this.requestBody);
233 }.bind(this)).then(done).catch(done);
234
235 assert.equal(when.isPromiseLike(purgeRequest), true);
236 });
237
238
239 it('should make request with bad action option given', function (done) {
240 this.applyMock({});
241 this.object.__set__('console', {
242 warn: function (msg) {
243 assert.equal(/akamai: WARNING: Invalid purge request action\. Valid actions:/.test(msg), true);
244 }
245 });
246
247 var purgeRequest = this.object.AkamaiPurge('someusername', 'supersecret', this.requestObjects, {action: 'badaction'}).then(function (response) {
248 assert.equal(response.httpStatus, 201);
249 assert.equal(typeof response.status, 'function');
250 assert.deepEqual(response.requestBody, this.requestBody);
251 }.bind(this)).then(done).catch(done);
252
253 assert.equal(when.isPromiseLike(purgeRequest), true);
254 });
255
256
257 it('should make request using object modifiers', function (done) {
258 this.applyMock({type: 'cpcode', domain: 'staging'}, true);
259
260 var purgeRequest = this.object.AkamaiPurge.staging.cpcode('someusername', 'supersecret', this.requestObjects).then(function (response) {
261 assert.equal(response.httpStatus, 201);
262 assert.equal(typeof response.status, 'function');
263 assert.deepEqual(response.requestBody, this.requestBody);
264 }.bind(this)).then(done).catch(done);
265
266 assert.equal(when.isPromiseLike(purgeRequest), true);
267 });
268 });
269});
\No newline at end of file