it('should allow saving a failed response', function() { var finalGoodSpy = jasmine.createSpy('finalGoodSpy'); var finalBadSpy = jasmine.createSpy('finalBadSpy'); var error = {foo: 'bar'}; var interceptorCount = 0; http.globalInterceptors.response.push({ resolve ({err, req, res}) { interceptorCount++; return Promise.reject({err, res}); } }); http.globalInterceptors.response.push({ reject ({err, req, res}) { interceptorCount++; return Promise.resolve({err, res}); } }); http.intercept({ req: sampleRequest, res: sampleResponse, interceptType: 'response' }).then(finalGoodSpy, finalBadSpy); PromiseBackend.flush(true); expect(interceptorCount).toBe(2); expect(finalBadSpy).not.toHaveBeenCalled(); expect(finalGoodSpy).toHaveBeenCalled(); });
it('should send the request through the request interceptor', function() { var spy = jasmine.createSpy('interceptor'); spy.and.returnValue({headers:{}}); http.globalInterceptors.request.push({resolve:spy}); http.request(defaultConfig); PromiseBackend.flush(true); expect(spy).toHaveBeenCalled(); });
it('should call intercept with the Request object', function() { var spy = spyOn(http, 'intercept').and.callThrough(); http.request({method: 'GET', url: '/something'}); PromiseBackend.flush(true); expect(spy).toHaveBeenCalled(); expect(spy.calls.argsFor(0)[0].req.method).toBe('GET'); expect(spy.calls.argsFor(0)[0].req.url).toBe('/something'); });
it('should call intercept with an error failure', function() { var spy = spyOn(http, 'intercept').and.callThrough(); ConnectionMockBackend.whenRequest('GET', '/users').respond(404, 'error: not found'); http.request({ method: 'GET', url: '/users', ConnectionClass: ConnectionMock }); PromiseBackend.flush(true); ConnectionMockBackend.flush(true); expect(spy.calls.count()).toBe(2); });
it('should call intercept with the raw response upon successful request', function() { var spy = spyOn(http, 'intercept').and.callThrough(); ConnectionMockBackend.whenRequest('GET', '/users').respond(200, 'rawbody'); http.request({ method: 'GET', url: '/users', ConnectionClass: ConnectionMock }).then(spy); PromiseBackend.flush(true); ConnectionMockBackend.flush(); expect(spy).toHaveBeenCalled(); expect(spy.calls.count()).toBe(2); });
it('should apply request headers set in the interceptor', function() { var headerSpy = spyOn(XHRConnection.prototype, 'setRequestHeader'); var interceptor = { resolve ({err, req}) { req.headers.set('Client', 'Browser'); return {err, req}; } } http.globalInterceptors.request.push(interceptor); http.request(defaultConfig); PromiseBackend.flush(true); expect(headerSpy).toHaveBeenCalledWith('Client', 'Browser'); });
it('should process a request through globalInterceptors', function() { var goodSpy = jasmine.createSpy('goodSpy'); var badSpy = jasmine.createSpy('badSpy'); http.globalInterceptors.request.push({ resolve ({err, req}) { req.headers.set('sender', 'Jeff'); return {req, err}; } }); http.intercept({ req: sampleRequest, interceptType: 'request' }).then(goodSpy); PromiseBackend.flush(true); expect(goodSpy.calls.argsFor(0)[0].req.headers.get('sender')).toBe('Jeff'); expect(badSpy).not.toHaveBeenCalled(); });
it('should not throw when applying request headers', function() { openSpy.and.callThrough(); var rejectedSpy = jasmine.createSpy('rejected'); var interceptor = { resolve({err, req}) { req.headers.set('Client', 'Browser'); return {err, req}; } }; http.globalInterceptors.request.push(interceptor); http.request(defaultConfig).then(null, rejectedSpy); PromiseBackend.flush(true); // Current promise mock will catch the exception and reject the promise --- we need to verify // that this did not happen expect(rejectedSpy).not.toHaveBeenCalled(); });
it('should process the response through globalInterceptors', function() { var goodSpy = jasmine.createSpy('goodSpy'); var badSpy = jasmine.createSpy('badSpy'); http.globalInterceptors.response.push({ resolve ({err, req, res}) { res.body = res.body.replace('fo','intercepted'); return {err, req, res}; } }); http.intercept({ req: sampleRequest, res: sampleResponse, interceptType: 'response' }).then(goodSpy, badSpy); PromiseBackend.flush(true); expect(goodSpy).toHaveBeenCalled(); expect(goodSpy.calls.argsFor(0)[0].res.body).toBe('interceptedo'); expect(badSpy).not.toHaveBeenCalled(); });
it('should call reject if an interceptor rejects', function() { var goodSpy = jasmine.createSpy('goodSpy'); var badSpy = jasmine.createSpy('badSpy'); var error = {foo: 'bar'}; var interceptorCalled = false; http.globalInterceptors.response.push({ resolve ({err, req, res}) { interceptorCalled = true; res.body = res.body.replace('raw','intercepted'); return Promise.reject({err: error, req, res}); } }); http.intercept({ req: sampleRequest, res: sampleResponse, interceptType: 'response' }).then(goodSpy, badSpy); PromiseBackend.flush(true); expect(interceptorCalled).toBe(true); expect(badSpy).toHaveBeenCalled(); expect(goodSpy).not.toHaveBeenCalled(); });
it('should complain if url is not a string', function() { var spy = jasmine.createSpy('rejectSpy'); http.request({method: 'GET', url: undefined}).then(null, spy); PromiseBackend.flush(true); expect(spy).toHaveBeenCalled(); });
it('should call send on the connection', function() { http.request(defaultConfig); PromiseBackend.flush(true); expect(sendSpy).toHaveBeenCalled(); });
it('should pass data to send', function() { var data = '{"user" : "jeffbcross"}'; http.request({method: 'GET', url: '/users', data: data}); PromiseBackend.flush(true); expect(sendSpy).toHaveBeenCalledWith(data); });
afterEach(function() { PromiseBackend.restoreNativePromise(); })
beforeEach(function() { PromiseBackend.patchWithMock(); });
it('should serialize data before calling open', function() { http.request({method: 'GET', url: '/users', data: {interests: 'JavaScript'}}); PromiseBackend.flush(true); expect(sendSpy.calls.all()[0].args[0]).toBe('{"interests":"JavaScript"}'); });
beforeEach(inject(Http, function(_http_) { defaultConfig = {method: 'GET', url: '/users'}; http = _http_; PromiseBackend.patchWithMock(); }));
it('should call open on the connection', function() { http.request(defaultConfig); PromiseBackend.flush(true); expect(openSpy).toHaveBeenCalledWith('GET', '/users'); });
afterEach(function() { delete ConnectionMockBackend.connections; PromiseBackend.restoreNativePromise(); });