How to use getBatchConfig method in ts-auto-mock

Best JavaScript code snippet using ts-auto-mock

httpBatcher.spec.js

Source:httpBatcher.spec.js Github

copy

Full Screen

1(function (angular, sinon) {2 'use strict';3 describe('httpBatcher', function () {4 var sandbox, $httpBackend, $timeout, httpBatchConfig, httpBatcher,5 defaultBatchAdapter = 'httpBatchAdapter',6 host = 'www.google.com.au',7 $window = {8 location: {9 host: host10 }11 },12 parseHeaders = function (headers) {13 var parsed = {},14 key, val, i;15 if (!headers) {16 return parsed;17 }18 headers.split('\n').forEach(function (line) {19 i = line.indexOf(':');20 key = line.substr(0, i).trim().toLowerCase();21 val = line.substr(i + 1).trim();22 if (key) {23 if (parsed[key]) {24 parsed[key] += ', ' + val;25 } else {26 parsed[key] = val;27 }28 }29 });30 return parsed;31 };32 beforeEach(module(function ($provide) {33 $provide.value('$window', $window);34 }));35 beforeEach(module(window.ahb.name));36 describe('httpBatcher', function () {37 beforeEach(inject(function ($injector) {38 sandbox = sinon.sandbox.create();39 $httpBackend = $injector.get('$httpBackend');40 $timeout = $injector.get('$timeout');41 httpBatchConfig = $injector.get('httpBatchConfig');42 httpBatcher = $injector.get('httpBatcher');43 }));44 afterEach(function () {45 sandbox.restore();46 $httpBackend.verifyNoOutstandingExpectation();47 $httpBackend.verifyNoOutstandingRequest();48 });49 it('should be defined', function () {50 expect(httpBatcher).to.exist;51 });52 describe('canBatchRequest', function () {53 it('should call canBatchCall on httpBatchConfig', function () {54 sandbox.stub(httpBatchConfig, 'canBatchCall').returns(false);55 httpBatcher.canBatchRequest('http://www.gogle.com/resource', 'GET');56 expect(httpBatchConfig.canBatchCall.calledOnce).to.equal(true);57 });58 });59 describe('batchRequest request creation', function () {60 it('should call getBatchConfig on httpBatchConfig', function () {61 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns({62 batchEndpointUrl: 'http://www.someservice.com/batch'63 });64 httpBatcher.batchRequest({65 url: 'http://www.gogle.com/resource',66 method: 'GET',67 callback: angular.noop68 });69 expect(httpBatchConfig.getBatchConfig.calledOnce).to.equal(true);70 });71 it('should call batchEndpointUrl after batchRequestCollectionDelay timeout has passed', function () {72 $httpBackend.expectPOST('http://www.someservice.com/batch').respond(404);73 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns({74 batchEndpointUrl: 'http://www.someservice.com/batch',75 batchRequestCollectionDelay: 200,76 minimumBatchSize: 1,77 adapter: defaultBatchAdapter78 });79 httpBatcher.batchRequest({80 url: 'http://www.gogle.com/resource',81 method: 'GET',82 callback: angular.noop83 });84 $timeout.flush();85 $httpBackend.flush();86 });87 it('should create the correct http post data for a single GET request', function () {88 var batchConfig = {89 batchEndpointUrl: 'http://www.someservice.com/batch',90 batchRequestCollectionDelay: 200,91 minimumBatchSize: 1,92 adapter: defaultBatchAdapter93 },94 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',95 responseData = '';96 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);97 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');98 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);99 httpBatcher.batchRequest({100 url: 'http://www.gogle.com/resource',101 method: 'GET',102 callback: angular.noop103 });104 $timeout.flush();105 $httpBackend.flush();106 });107 it('should create the correct http post data for a complex relative url GET request', function () {108 var batchConfig = {109 batchEndpointUrl: 'api/batch',110 batchRequestCollectionDelay: 200,111 minimumBatchSize: 1,112 adapter: defaultBatchAdapter113 },114 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: localhost:9876\r\n\r\n\r\n--some_boundary_mocked--',115 responseData = '';116 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);117 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');118 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);119 httpBatcher.batchRequest({120 url: './resource',121 method: 'GET',122 callback: angular.noop123 });124 $timeout.flush();125 $httpBackend.flush();126 });127 it('should add additional headers to the batch request as defined in the config object', function () {128 var batchConfig = {129 batchEndpointUrl: 'http://www.someservice.com/batch',130 batchRequestCollectionDelay: 200,131 minimumBatchSize: 1,132 adapter: defaultBatchAdapter,133 batchRequestHeaders: {134 'Content-disposition': 'form-data'135 }136 },137 responseData = '';138 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, undefined, function (headers) {139 return headers['Content-disposition'] === 'form-data';140 }).respond(404, responseData);141 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');142 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);143 httpBatcher.batchRequest({144 url: 'http://www.gogle.com/resource',145 method: 'GET',146 callback: angular.noop147 });148 $timeout.flush();149 $httpBackend.flush();150 });151 it('should create the correct http post data for a single GET request with additional headers', function () {152 var batchConfig = {153 batchEndpointUrl: 'http://www.someservice.com/batch',154 batchRequestCollectionDelay: 200,155 minimumBatchSize: 1,156 adapter: defaultBatchAdapter,157 batchRequestHeaders: {158 'Content-disposition': 'form-data'159 },160 batchPartRequestHeaders: {161 'Content-disposition': 'form-data'162 }163 },164 postData = '--some_boundary_mocked\r\nContent-disposition: form-data\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',165 responseData = '';166 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData, function (headers) {167 return headers['Content-disposition'] === 'form-data';168 }).respond(404, responseData);169 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');170 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);171 httpBatcher.batchRequest({172 url: 'http://www.gogle.com/resource',173 method: 'GET',174 callback: angular.noop175 });176 $timeout.flush();177 $httpBackend.flush();178 });179 it('should create the correct http post data for a single POST request', function () {180 var batchConfig = {181 batchEndpointUrl: 'http://www.someservice.com/batch',182 batchRequestCollectionDelay: 200,183 minimumBatchSize: 1,184 adapter: defaultBatchAdapter185 },186 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nPOST /resource HTTP/1.1\r\n' +187 'Host: www.gogle.com\r\n\r\n{"propOne":1,"propTwo":"two","propThree":3,"propFour":true}\r\n\r\n--some_boundary_mocked--',188 responseData = '';189 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);190 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');191 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);192 httpBatcher.batchRequest({193 url: 'http://www.gogle.com/resource',194 method: 'POST',195 data: angular.toJson({196 propOne: 1,197 propTwo: 'two',198 propThree: 3.00,199 propFour: true200 }),201 callback: angular.noop202 });203 $timeout.flush();204 $httpBackend.flush();205 });206 it('should create the correct http post data for a single GET request with custom headers', function () {207 var batchConfig = {208 batchEndpointUrl: 'http://www.someservice.com/batch',209 batchRequestCollectionDelay: 200,210 minimumBatchSize: 1,211 adapter: defaultBatchAdapter212 },213 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\nx-custom: data123\r\nAuthentication: 1234567890\r\n\r\n\r\n--some_boundary_mocked--',214 responseData = '';215 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);216 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');217 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);218 httpBatcher.batchRequest({219 url: 'http://www.gogle.com/resource',220 method: 'GET',221 headers: {222 'x-custom': 'data123',223 Authentication: '1234567890'224 },225 callback: angular.noop226 });227 $timeout.flush();228 $httpBackend.flush();229 });230 it('should create the correct http post data for a multiple requests', function () {231 var batchConfig = {232 batchEndpointUrl: 'http://www.someservice.com/batch',233 batchRequestCollectionDelay: 200,234 minimumBatchSize: 1,235 adapter: defaultBatchAdapter236 },237 postData = '--some_boundary_mocked' +238 '\r\nContent-Type: application/http; msgtype=request\r\n\r\nPOST /resource-two HTTP/1.1\r\nHost: www.gogle.com\r\nAuthentication: 123987\r\n\r\n{"propOne":1,"propTwo":"two","propThree":3,"propFour":true}' +239 '\r\n\r\n--some_boundary_mocked' +240 '\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\nx-custom: data123\r\nAuthentication: 1234567890' +241 '\r\n\r\n\r\n--some_boundary_mocked--',242 responseData = '';243 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);244 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');245 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);246 httpBatcher.batchRequest({247 url: 'http://www.gogle.com/resource-two',248 method: 'POST',249 headers: {250 Authentication: '123987'251 },252 data: angular.toJson({253 propOne: 1,254 propTwo: 'two',255 propThree: 3.00,256 propFour: true257 }),258 callback: angular.noop259 });260 httpBatcher.batchRequest({261 url: 'http://www.gogle.com/resource',262 method: 'GET',263 headers: {264 'x-custom': 'data123',265 Authentication: '1234567890'266 },267 callback: angular.noop268 });269 $timeout.flush();270 $httpBackend.flush();271 });272 it('should not create a batch request but let the request continue normally if the min batch size is not met', function () {273 var normalRouteCalled = 0,274 batchConfig = {275 batchEndpointUrl: 'http://www.someservice.com/batch',276 batchRequestCollectionDelay: 200,277 minimumBatchSize: 2,278 adapter: defaultBatchAdapter279 };280 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);281 httpBatcher.batchRequest({282 url: 'http://www.gogle.com/resource-two',283 method: 'GET',284 headers: {285 Authentication: '123987'286 },287 callback: angular.noop,288 continueDownNormalPipeline: function () {289 normalRouteCalled += 1;290 }291 });292 $timeout.flush();293 expect(normalRouteCalled).to.equal(1);294 });295 });296 describe('batchRequest request creation with relative url', function () {297 it('should create the correct http post data for a single GET request with a relative url', function () {298 var batchConfig = {299 batchEndpointUrl: '/api/batch',300 batchRequestCollectionDelay: 200,301 minimumBatchSize: 1,302 adapter: defaultBatchAdapter303 },304 postData = '--some_boundary_mocked\r\nContent-Type: application/http; ' +305 'msgtype=request\r\n\r\n' +306 'GET /api/resource HTTP/1.1\r\n' +307 'Host: www.google.com.au\r\n\r\n\r\n' +308 '--some_boundary_mocked--',309 responseData = '';310 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);311 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');312 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);313 httpBatcher.batchRequest({314 url: '/api/resource',315 method: 'GET',316 callback: angular.noop317 });318 $timeout.flush();319 $httpBackend.flush();320 });321 });322 describe('batchRequest response parsing', function () {323 it('should parse the response of a single batch request', function (done) {324 var batchConfig = {325 batchEndpointUrl: 'http://www.someservice.com/batch',326 batchRequestCollectionDelay: 200,327 minimumBatchSize: 1,328 adapter: defaultBatchAdapter329 },330 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',331 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +332 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +333 '[{"Name":"Product 1","Id":1,"StockQuantity":100},{"Name":"Product 2","Id":2,"StockQuantity":2},{"Name":"Product 3","Id":3,"StockQuantity":32432}]' +334 '\r\n--some_boundary_mocked--\r\n';335 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {336 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'337 }, 'OK');338 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');339 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);340 httpBatcher.batchRequest({341 url: 'http://www.gogle.com/resource',342 method: 'GET',343 callback: function (statusCode, data, headers, statusText) {344 var headerObj = parseHeaders(headers);345 expect(statusCode).to.equal(200);346 expect(statusText).to.equal('OK');347 expect(headerObj['content-type']).to.equal('json; charset=utf-8');348 expect(data).to.deep.equal([{349 Name: 'Product 1',350 Id: 1,351 StockQuantity: 100352 }, {353 Name: 'Product 2',354 Id: 2,355 StockQuantity: 2356 }, {357 Name: 'Product 3',358 Id: 3,359 StockQuantity: 32432360 }]);361 done();362 }363 });364 $timeout.flush();365 $httpBackend.flush();366 });367 it('should parse the response of a single batch request which contains --', function (done) {368 var batchConfig = {369 batchEndpointUrl: 'http://www.someservice.com/batch',370 batchRequestCollectionDelay: 200,371 minimumBatchSize: 1,372 adapter: defaultBatchAdapter373 },374 postData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--',375 responseData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +376 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8;\r\n\r\n' +377 '{"results":[{"BusinessDescription":"Some text here\r\n-------------------"}],"inlineCount":35}' +378 '\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--\r\n';379 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {380 'content-type': 'multipart/mixed; boundary="31fcc127-a593-4e1d-86f3-57e45375848f"'381 }, 'OK');382 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('31fcc127-a593-4e1d-86f3-57e45375848f');383 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);384 httpBatcher.batchRequest({385 url: 'http://www.gogle.com/resource',386 method: 'GET',387 callback: function (statusCode, data) {388 expect(data).to.deep.equal({389 results: [{390 BusinessDescription: 'Some text here-------------------'391 }],392 inlineCount: 35393 });394 done();395 }396 });397 $timeout.flush();398 $httpBackend.flush();399 });400 it('should parse the response of a single batch request where the data is on multilines', function (done) {401 var batchConfig = {402 batchEndpointUrl: 'http://www.someservice.com/batch',403 batchRequestCollectionDelay: 200,404 minimumBatchSize: 1,405 adapter: defaultBatchAdapter406 },407 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',408 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +409 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +410 '[\r\n{\r\n"Name":\r\n"Product 1",\r\n"Id":\r\n1},\r\n{\r\n"Name":\r\n"Product 2",\r\n"Id":\r\n2}\r\n]' +411 '\r\n--some_boundary_mocked--\r\n';412 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {413 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'414 }, 'OK');415 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');416 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);417 httpBatcher.batchRequest({418 url: 'http://www.gogle.com/resource',419 method: 'GET',420 callback: function (statusCode, data, headers, statusText) {421 var headerObj = parseHeaders(headers);422 expect(statusCode).to.equal(200);423 expect(statusText).to.equal('OK');424 expect(headerObj['content-type']).to.equal('json; charset=utf-8');425 expect(data).to.deep.equal([{426 Name: 'Product 1',427 Id: 1428 }, {429 Name: 'Product 2',430 Id: 2431 }]);432 done();433 }434 });435 $timeout.flush();436 $httpBackend.flush();437 });438 it('should parse the response of a single batch request with additional custom headers in the response', function (done) {439 var batchConfig = {440 batchEndpointUrl: 'http://www.someservice.com/batch',441 batchRequestCollectionDelay: 200,442 minimumBatchSize: 1,443 adapter: defaultBatchAdapter444 },445 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',446 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +447 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\nX-SomeHeader: 123AbC\r\nAuthentication: Bonza\r\n\r\n' +448 '[{"Name":"Product 1","Id":1,"StockQuantity":100},{"Name":"Product 2","Id":2,"StockQuantity":2},{"Name":"Product 3","Id":3,"StockQuantity":32432}]' +449 '\r\n--some_boundary_mocked--\r\n';450 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {451 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'452 }, 'OK');453 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');454 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);455 httpBatcher.batchRequest({456 url: 'http://www.gogle.com/resource',457 method: 'GET',458 callback: function (statusCode, data, headers) {459 var headerObj = parseHeaders(headers);460 expect(headerObj['x-someheader']).to.equal('123AbC');461 expect(headerObj.authentication).to.equal('Bonza');462 done();463 }464 });465 $timeout.flush();466 $httpBackend.flush();467 });468 it('should parse a failed response of a single batch request', function (done) {469 var batchConfig = {470 batchEndpointUrl: 'http://www.someservice.com/batch',471 batchRequestCollectionDelay: 200,472 minimumBatchSize: 1,473 adapter: defaultBatchAdapter474 },475 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',476 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +477 'HTTP/1.1 401 UnAuthorised\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +478 '{ "message": "Access Denied" }' +479 '\r\n--some_boundary_mocked--\r\n';480 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {481 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'482 }, 'OK');483 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');484 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);485 httpBatcher.batchRequest({486 url: 'http://www.gogle.com/resource',487 method: 'GET',488 callback: function (statusCode, data, headers, statusText) {489 expect(statusCode).to.equal(401);490 expect(statusText).to.equal('UnAuthorised');491 expect(data).to.deep.equal({492 message: 'Access Denied'493 });494 done();495 }496 });497 $timeout.flush();498 $httpBackend.flush();499 });500 it('should parse multiple responses of a single batch request', function (done) {501 var batchConfig = {502 batchEndpointUrl: 'http://www.someservice.com/batch',503 batchRequestCollectionDelay: 200,504 minimumBatchSize: 1,505 adapter: defaultBatchAdapter506 },507 postData = '--some_boundary_mocked' +508 '\r\nContent-Type: application/http; msgtype=request\r\n\r\nPOST /resource-two HTTP/1.1\r\nHost: www.gogle.com\r\nAuthentication: 123987\r\n\r\n{"propOne":1,"propTwo":"two","propThree":3,"propFour":true}' +509 '\r\n\r\n--some_boundary_mocked' +510 '\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\nx-custom: data123\r\nAuthentication: 1234567890' +511 '\r\n\r\n\r\n--some_boundary_mocked--',512 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +513 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +514 '[{"Name":"Product 1","Id":1,"StockQuantity":100},{"Name":"Product 2","Id":2,"StockQuantity":2},{"Name":"Product 3","Id":3,"StockQuantity":32432}]' +515 '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +516 'HTTP/1.1 200 YO!\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +517 '[{"name":"Jon","id":1,"age":30},{"name":"Laurie","id":2,"age":29}]' +518 '\r\n--some_boundary_mocked--\r\n',519 completedFnInvocationCount = 0,520 completedFn = function () {521 completedFnInvocationCount += 1;522 if (completedFnInvocationCount === 2) {523 done();524 }525 };526 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {527 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'528 }, 'OK');529 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');530 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);531 httpBatcher.batchRequest({532 url: 'http://www.gogle.com/resource-two',533 method: 'POST',534 headers: {535 Authentication: '123987'536 },537 data: angular.toJson({538 propOne: 1,539 propTwo: 'two',540 propThree: 3.00,541 propFour: true542 }),543 callback: function (statusCode, data, headers, statusText) {544 var headerObj = parseHeaders(headers);545 expect(statusCode).to.equal(200);546 expect(statusText).to.equal('OK');547 expect(headerObj['content-type']).to.equal('json; charset=utf-8');548 expect(data).to.deep.equal([{549 Name: 'Product 1',550 Id: 1,551 StockQuantity: 100552 }, {553 Name: 'Product 2',554 Id: 2,555 StockQuantity: 2556 }, {557 Name: 'Product 3',558 Id: 3,559 StockQuantity: 32432560 }]);561 completedFn();562 }563 });564 httpBatcher.batchRequest({565 url: 'http://www.gogle.com/resource',566 method: 'GET',567 headers: {568 'x-custom': 'data123',569 Authentication: '1234567890'570 },571 callback: function (statusCode, data, headers, statusText) {572 var headerObj = parseHeaders(headers);573 expect(statusCode).to.equal(200);574 expect(statusText).to.equal('YO!');575 expect(headerObj['content-type']).to.equal('json; charset=utf-8');576 expect(data).to.deep.equal([{577 name: 'Jon',578 id: 1,579 age: 30580 }, {581 name: 'Laurie',582 id: 2,583 age: 29584 }]);585 completedFn();586 }587 });588 $timeout.flush();589 $httpBackend.flush();590 });591 it('should parse the response of a single batch request which contains the Angular "JSON Vulnerability Protection" prefix', function (done) {592 var batchConfig = {593 batchEndpointUrl: 'http://www.someservice.com/batch',594 batchRequestCollectionDelay: 200,595 minimumBatchSize: 1,596 adapter: defaultBatchAdapter597 },598 postData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--',599 responseData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +600 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8;\r\n\r\n' +601 ')]}\',\n' + // JSON Vulnerability Protection prefix (see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection )602 '{"results":[{"BusinessDescription":"Some text here"}],"inlineCount":35}' +603 '\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--\r\n';604 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {605 'content-type': 'multipart/mixed; boundary="31fcc127-a593-4e1d-86f3-57e45375848f"'606 }, 'OK');607 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('31fcc127-a593-4e1d-86f3-57e45375848f');608 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);609 httpBatcher.batchRequest({610 url: 'http://www.gogle.com/resource',611 method: 'GET',612 callback: function (statusCode, data) {613 expect(data).to.deep.equal({614 results: [{615 BusinessDescription: 'Some text here'616 }],617 inlineCount: 35618 });619 done();620 }621 });622 $timeout.flush();623 $httpBackend.flush();624 });625 it('should parse the response of a single batch request which contains the Angular "JSON Vulnerability Protection" prefix and multiple responses', function (done) {626 var batchConfig = {627 batchEndpointUrl: 'http://www.someservice.com/batch',628 batchRequestCollectionDelay: 200,629 minimumBatchSize: 1,630 adapter: defaultBatchAdapter631 },632 postData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--31fcc127-a593-4e1d-86f3-57e45375848f' +633 '\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource-two HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--',634 responseData = '--31fcc127-a593-4e1d-86f3-57e45375848f\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +635 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8;\r\n\r\n' +636 ')]}\',\n' + // JSON Vulnerability Protection prefix (see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection )637 '{"results":[{"BusinessDescription":"Some text here"}],"inlineCount":35}' +638 '\r\n--31fcc127-a593-4e1d-86f3-57e45375848f\r\n' +639 '\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +640 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8;\r\n\r\n' +641 ')]}\',\n' + // JSON Vulnerability Protection prefix (see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection )642 '[{"name":"Jon","id":1,"age":30},{"name":"Laurie","id":2,"age":29}]' +643 '\r\n--31fcc127-a593-4e1d-86f3-57e45375848f--\r\n',644 completedFnInvocationCount = 0,645 completedFn = function () {646 completedFnInvocationCount += 1;647 if (completedFnInvocationCount === 2) {648 done();649 }650 };651 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {652 'content-type': 'multipart/mixed; boundary="31fcc127-a593-4e1d-86f3-57e45375848f"'653 }, 'OK');654 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('31fcc127-a593-4e1d-86f3-57e45375848f');655 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);656 httpBatcher.batchRequest({657 url: 'http://www.gogle.com/resource',658 method: 'GET',659 callback: function (statusCode, data) {660 expect(data).to.deep.equal({661 results: [{662 BusinessDescription: 'Some text here'663 }],664 inlineCount: 35665 });666 completedFn();667 }668 });669 httpBatcher.batchRequest({670 url: 'http://www.gogle.com/resource-two',671 method: 'GET',672 callback: function (statusCode, data) {673 expect(data).to.deep.equal([{674 name: 'Jon',675 id: 1,676 age: 30677 }, {678 name: 'Laurie',679 id: 2,680 age: 29681 }]);682 completedFn();683 }684 });685 $timeout.flush();686 $httpBackend.flush();687 });688 it('should return original data for non strings when trim Angular "JSON Vulnerability Protection" prefix', function (done) {689 var responseData = [{690 "headers": {691 "Content-Type": "text/html; charset=utf-8"692 },693 "status_code": 200,694 "body": "Success!",695 "reason_phrase": "OK"696 }, {697 "headers": {698 "Content-Type": "text/html; charset=utf-8"699 },700 "status_code": 201,701 "body": "{\"text\": \"some text\"}",702 "reason_phrase": "CREATED"703 }],704 batchEndpointUrl = 'http://www.someservice.com/batch',705 batchConfig = {706 batchEndpointUrl: batchEndpointUrl,707 batchRequestCollectionDelay: 200,708 minimumBatchSize: 1,709 adapter: {710 buildRequest: function () {711 return {712 method: 'POST',713 url: batchEndpointUrl,714 cache: false,715 headers: {},716 data: ''717 };718 },719 parseResponse: function (requests, rawResponse) {720 expect(rawResponse.data).to.deep.equal(responseData);721 done();722 return [];723 }724 }725 };726 $httpBackend.expectPOST(batchConfig.batchEndpointUrl).respond(200, responseData, {}, 'OK');727 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('31fcc127-a593-4e1d-86f3-57e45375848f');728 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);729 httpBatcher.batchRequest({730 url: 'http://www.gogle.com/resource',731 method: 'GET',732 callback: angular.noop733 });734 $timeout.flush();735 $httpBackend.flush();736 });737 describe('error handling', function () {738 it('should handle a 500 response', function (done) {739 var batchConfig = {740 batchEndpointUrl: 'http://www.someservice.com/batch',741 batchRequestCollectionDelay: 200,742 minimumBatchSize: 1,743 adapter: defaultBatchAdapter744 },745 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',746 responseData = 'Internal Server Error';747 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(500, responseData, {}, 'Internal Server Error');748 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');749 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);750 httpBatcher.batchRequest({751 url: 'http://www.gogle.com/resource',752 method: 'GET',753 callback: function (statusCode, data, headers, statusText) {754 done();755 }756 });757 $timeout.flush();758 $httpBackend.flush();759 });760 });761 });762 describe('flush', function () {763 it('should send the batched request before the timeout to send the batch has been reached', function (done) {764 var batchConfig = {765 batchEndpointUrl: 'http://www.someservice.com/batch',766 batchRequestCollectionDelay: 10000,767 minimumBatchSize: 1,768 adapter: defaultBatchAdapter769 },770 postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: www.gogle.com\r\n\r\n\r\n--some_boundary_mocked--',771 responseData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=response\r\n\r\n' +772 'HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n' +773 '[{"Name":"Product 1","Id":1,"StockQuantity":100},{"Name":"Product 2","Id":2,"StockQuantity":2},{"Name":"Product 3","Id":3,"StockQuantity":32432}]' +774 '\r\n--some_boundary_mocked--\r\n';775 $httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(200, responseData, {776 'content-type': 'multipart/mixed; boundary="some_boundary_mocked"'777 }, 'OK');778 sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');779 sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);780 httpBatcher.batchRequest({781 url: 'http://www.gogle.com/resource',782 method: 'GET',783 callback: function () {784 done();785 }786 });787 httpBatcher.flush();788 $httpBackend.flush();789 });790 });791 });792 });...

Full Screen

Full Screen

config.service.spec.js

Source:config.service.spec.js Github

copy

Full Screen

...21 accessArrangementCodes: '',22 fontSizeCode: null,23 colourContrastCode: null24 }])25 const c = await configService.getBatchConfig([5], 18601)26 const config = c[pupilId]27 expect(config.audibleSounds).toBeFalsy()28 expect(config.checkTime).toBe(32)29 expect(config.colourContrast).toBeFalsy()30 expect(config.fontSize).toBeFalsy()31 expect(config.inputAssistance).toBeFalsy()32 expect(config.loadingTime).toBe(1)33 expect(config.nextBetweenQuestions).toBeFalsy()34 expect(config.numpadRemoval).toBeFalsy()35 expect(config.questionReader).toBeFalsy()36 expect(config.questionTime).toBe(2)37 })38 test('set audible sounds correctly', async () => {39 const pupilId = 540 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(41 [{42 pupilId,43 schoolId: 18601,44 loadingTime: 1,45 questionTime: 2,46 checkTime: 32,47 accessArrangementCodes: 'ATA',48 fontSizeCode: null,49 colourContrastCode: null50 }])51 const c = await configService.getBatchConfig([5], 18601)52 const config = c[pupilId]53 expect(config.audibleSounds).toBe(true)54 })55 test('set input assistance correctly', async () => {56 const pupilId = 557 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(58 [{59 pupilId,60 schoolId: 18601,61 loadingTime: 1,62 questionTime: 2,63 checkTime: 32,64 accessArrangementCodes: 'ITA',65 fontSizeCode: null,66 colourContrastCode: null67 }])68 const c = await configService.getBatchConfig([5], 18601)69 const config = c[pupilId]70 expect(config.inputAssistance).toBe(true)71 })72 test('set colour contrast correctly without a colour contrast code already set', async () => {73 const pupilId = 574 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(75 [{76 pupilId,77 schoolId: 18601,78 loadingTime: 1,79 questionTime: 2,80 checkTime: 32,81 accessArrangementCodes: 'CCT',82 fontSizeCode: null,83 colourContrastCode: null84 }])85 const c = await configService.getBatchConfig([5], 18601)86 const config = c[pupilId]87 expect(config.colourContrast).toBe(true)88 expect(config.colourContrastCode).toBeFalsy()89 })90 test('sets the font size correctly without a font size code already set', async () => {91 const pupilId = 592 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(93 [{94 pupilId,95 schoolId: 18601,96 loadingTime: 1,97 questionTime: 2,98 checkTime: 32,99 accessArrangementCodes: 'FTS',100 fontSizeCode: null,101 colourContrastCode: null102 }])103 const c = await configService.getBatchConfig([5], 18601)104 const config = c[pupilId]105 expect(config.fontSize).toBe(true)106 expect(config.fontSizeCode).toBeFalsy()107 })108 test('sets the font size correctly with a font size code already set', async () => {109 const pupilId = 5110 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(111 [{112 pupilId,113 schoolId: 18601,114 loadingTime: 1,115 questionTime: 2,116 checkTime: 32,117 accessArrangementCodes: 'FTS',118 fontSizeCode: 'XLG',119 colourContrastCode: null120 }])121 const c = await configService.getBatchConfig([5], 18601)122 const config = c[pupilId]123 expect(config.fontSize).toBe(true)124 expect(config.fontSizeCode).toBe('XLG')125 })126 test('sets the next-between-questions flag correctly', async () => {127 const pupilId = 5128 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(129 [{130 pupilId,131 schoolId: 18601,132 loadingTime: 1,133 questionTime: 2,134 checkTime: 32,135 accessArrangementCodes: 'NBQ',136 fontSizeCode: null,137 colourContrastCode: null138 }])139 const c = await configService.getBatchConfig([5], 18601)140 const config = c[pupilId]141 expect(config.nextBetweenQuestions).toBe(true)142 })143 test('sets the question reader flag correctly', async () => {144 const pupilId = 5145 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(146 [{147 pupilId,148 schoolId: 18601,149 loadingTime: 1,150 questionTime: 2,151 checkTime: 32,152 accessArrangementCodes: 'QNR',153 fontSizeCode: null,154 colourContrastCode: null155 }])156 const c = await configService.getBatchConfig([5], 18601)157 const config = c[pupilId]158 expect(config.questionReader).toBe(true)159 })160 test('sets the numpad removal flag correctly', async () => {161 const pupilId = 5162 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(163 [{164 pupilId,165 schoolId: 18601,166 loadingTime: 1,167 questionTime: 2,168 checkTime: 32,169 accessArrangementCodes: 'RON',170 fontSizeCode: null,171 colourContrastCode: null172 }])173 const c = await configService.getBatchConfig([5], 18601)174 const config = c[pupilId]175 expect(config.numpadRemoval).toBe(true)176 })177 test('logs any unknown access arrangement code', async () => {178 const pupilId = 5179 jest.spyOn(logger, 'error').mockImplementation()180 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(181 [{182 pupilId,183 schoolId: 18601,184 loadingTime: 1,185 questionTime: 2,186 checkTime: 32,187 accessArrangementCodes: 'INVALID_CODE',188 fontSizeCode: null,189 colourContrastCode: null190 }])191 await configService.getBatchConfig([5], 18601)192 expect(logger.error).toHaveBeenCalledTimes(1)193 })194 test('can set multiple access codes', async () => {195 const pupilId = 5196 jest.spyOn(configDataService, 'getBatchConfig').mockResolvedValue(197 [{198 pupilId,199 schoolId: 18601,200 loadingTime: 5,201 questionTime: 7,202 checkTime: 64,203 accessArrangementCodes: 'ATA,CCT,FTS,ITA,NBQ,QNR,RON',204 fontSizeCode: 'VSM',205 colourContrastCode: 'YOB'206 }])207 const c = await configService.getBatchConfig([5], 18601)208 const config = c[pupilId]209 expect(config.audibleSounds).toBe(true)210 expect(config.checkTime).toBe(64)211 expect(config.colourContrast).toBe(true)212 expect(config.colourContrastCode).toBe('YOB')213 expect(config.fontSize).toBe(true)214 expect(config.fontSizeCode).toBe('VSM')215 expect(config.inputAssistance).toBe(true)216 expect(config.loadingTime).toBe(5)217 expect(config.nextBetweenQuestions).toBe(true)218 expect(config.numpadRemoval).toBe(true)219 expect(config.questionReader).toBe(true)220 expect(config.questionTime).toBe(7)221 })...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getBatchConfig } from 'ts-auto-mock';2import { getBatchConfig } from 'ts-auto-mock';3import { getBatchConfig } from 'ts-auto-mock';4import { getBatchConfig } from 'ts-auto-mock';5import { getBatchConfig } from 'ts-auto-mock';6import { getBatchConfig } from 'ts-auto-mock';7import { getBatchConfig } from 'ts-auto-mock';8import { getBatchConfig } from 'ts-auto-mock';9import { getBatchConfig } from 'ts-auto-mock';10import { getBatchConfig } from 'ts-auto-mock';11import { getBatchConfig } from 'ts-auto-mock';12import { getBatchConfig } from 'ts-auto-mock';13import { getBatchConfig } from 'ts-auto-mock';14import { getBatchConfig } from 'ts-auto-mock';15import { getBatchConfig } from 'ts-auto-mock';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getBatchConfig } from "ts-auto-mock";2const batchConfig = getBatchConfig();3import { getBatchConfig } from "ts-auto-mock";4const batchConfig = getBatchConfig();5import { getBatchConfig } from "ts-auto-mock";6const batchConfig = getBatchConfig();7import { getBatchConfig } from "ts-auto-mock";8const batchConfig = getBatchConfig();9import { getBatchConfig } from "ts-auto-mock";10const batchConfig = getBatchConfig();11import { getBatchConfig } from "ts-auto-mock";12const batchConfig = getBatchConfig();13import { getBatchConfig } from "ts-auto-mock";14const batchConfig = getBatchConfig();15import { getBatchConfig } from "

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getBatchConfig } from 'ts-auto-mock';2const batchConfig = getBatchConfig({3});4import { mock } from 'ts-auto-mock';5const result = mock({6});7import { getBatchConfig } from 'ts-auto-mock';8const batchConfig = getBatchConfig({9});10import { mock } from 'ts-auto-mock';11const result = mock({12});13import { combineReducers } from 'redux';14import { persistReducer } from 'redux-persist';15import storage from 'redux-persist/lib/storage';16import filter from '

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getBatchConfig } from 'ts-auto-mock';2const config = getBatchConfig({3});4import { getBatchConfig } from 'ts-auto-mock';5const config = getBatchConfig({6});7import { getBatchConfig } from 'ts-auto-mock';8const config = getBatchConfig({9});10import { getBatchConfig } from 'ts-auto-mock';11const config = getBatchConfig({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getBatchConfig } from "ts-auto-mock";2const batchConfig = getBatchConfig({3});4import { TestInterface } from "test1";5const test: TestInterface = {6};7import { TestInterface } from "test1";8const test2: TestInterface = {9};10import { getBatchConfig } from "ts-auto-mock";11const batchConfig = getBatchConfig({12});13import { TestInterface } from "test1";14const test3: TestInterface = {15};16import { TestInterface } from "test1";17const test4: TestInterface = {18};19import { getBatchConfig } from "ts-auto-mock";20const batchConfig = getBatchConfig({21});22import { TestInterface } from "test1";23const test5: TestInterface = {24};25import { TestInterface } from "test1";26const test6: TestInterface = {27};28import { getBatchConfig } from "ts-auto-mock";29const batchConfig = getBatchConfig({30});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { getBatchConfig } = require('ts-auto-mock');2const config = getBatchConfig({3});4const fs = require('fs');5fs.writeFileSync('./ts-auto-mock.config.js', `module.exports = ${JSON.stringify(config, null, 2)}`);6const { getBatchConfig } = require('ts-auto-mock');7const config = require('./ts-auto-mock.config');8getBatchConfig(config);9I’m trying to use ts-auto-mock to mock a class that uses the “import type” syntax. I’m using the latest version of ts-auto-mock (3.8.0) and I’m getting the following error:10{11 "compilerOptions": {12 "paths": {13 }14 },15}16I’m trying to use ts-auto-mock to mock a class that uses the “import

Full Screen

Using AI Code Generation

copy

Full Screen

1const getBatchConfig = require('ts-auto-mock/batch');2const batchConfig = getBatchConfig({3});4const batchConfig = getBatchConfig({5});6const batchConfig = getBatchConfig({7});8const batchConfig = getBatchConfig({9});10const batchConfig = getBatchConfig({11});12const batchConfig = getBatchConfig({13});14const batchConfig = getBatchConfig({15});16const batchConfig = getBatchConfig({17});18const batchConfig = getBatchConfig({19});20const batchConfig = getBatchConfig({21});22const batchConfig = getBatchConfig({23});24const batchConfig = getBatchConfig({25});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ts-auto-mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful