2015-04-29 02:07:55 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
2015-06-19 15:14:12 -04:00
|
|
|
afterEach,
|
2015-04-29 02:07:55 -04:00
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
SpyObject
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {Injector, provide} from 'angular2/core';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {MockBackend, MockConnection} from 'angular2/src/http/backends/mock_backend';
|
2015-07-28 16:10:25 -04:00
|
|
|
import {
|
|
|
|
BaseRequestOptions,
|
|
|
|
ConnectionBackend,
|
|
|
|
Request,
|
|
|
|
RequestMethods,
|
|
|
|
RequestOptions,
|
|
|
|
Response,
|
|
|
|
ResponseOptions,
|
2015-07-30 10:29:16 -04:00
|
|
|
URLSearchParams,
|
2015-10-11 01:11:13 -04:00
|
|
|
JSONP_PROVIDERS,
|
|
|
|
HTTP_PROVIDERS,
|
2015-07-30 10:29:16 -04:00
|
|
|
XHRBackend,
|
|
|
|
JSONPBackend,
|
|
|
|
Http,
|
|
|
|
Jsonp
|
2015-08-20 17:28:25 -04:00
|
|
|
} from 'angular2/http';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
|
|
|
|
let{Observable, Subject} = Rx;
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
class SpyObserver extends SpyObject {
|
|
|
|
onNext: Function;
|
|
|
|
onError: Function;
|
|
|
|
onCompleted: Function;
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onNext = this.spy('onNext');
|
|
|
|
this.onError = this.spy('onError');
|
|
|
|
this.onCompleted = this.spy('onCompleted');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function main() {
|
2015-07-30 10:29:16 -04:00
|
|
|
describe('injectables', () => {
|
|
|
|
var url = 'http://foo.bar';
|
|
|
|
var http: Http;
|
|
|
|
var parentInjector: Injector;
|
|
|
|
var childInjector: Injector;
|
|
|
|
var jsonpBackend: MockBackend;
|
|
|
|
var xhrBackend: MockBackend;
|
|
|
|
var jsonp: Jsonp;
|
|
|
|
|
|
|
|
it('should allow using jsonpInjectables and httpInjectables in same injector',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
parentInjector = Injector.resolveAndCreate([
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(XHRBackend, {useClass: MockBackend}),
|
|
|
|
provide(JSONPBackend, {useClass: MockBackend})
|
2015-10-11 01:11:13 -04:00
|
|
|
]);
|
2015-07-30 10:29:16 -04:00
|
|
|
|
|
|
|
childInjector = parentInjector.resolveAndCreateChild([
|
2015-10-11 01:11:13 -04:00
|
|
|
HTTP_PROVIDERS,
|
|
|
|
JSONP_PROVIDERS,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(XHRBackend, {useClass: MockBackend}),
|
|
|
|
provide(JSONPBackend, {useClass: MockBackend})
|
2015-07-30 10:29:16 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
http = childInjector.get(Http);
|
|
|
|
jsonp = childInjector.get(Jsonp);
|
|
|
|
jsonpBackend = childInjector.get(JSONPBackend);
|
|
|
|
xhrBackend = childInjector.get(XHRBackend);
|
|
|
|
|
|
|
|
var xhrCreatedConnections = 0;
|
|
|
|
var jsonpCreatedConnections = 0;
|
|
|
|
|
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
xhrBackend.connections.subscribe(() => {
|
2015-07-30 10:29:16 -04:00
|
|
|
xhrCreatedConnections++;
|
|
|
|
expect(xhrCreatedConnections).toEqual(1);
|
|
|
|
if (jsonpCreatedConnections) {
|
|
|
|
async.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
http.get(url).subscribe(() => {});
|
2015-07-30 10:29:16 -04:00
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
jsonpBackend.connections.subscribe(() => {
|
2015-07-30 10:29:16 -04:00
|
|
|
jsonpCreatedConnections++;
|
|
|
|
expect(jsonpCreatedConnections).toEqual(1);
|
|
|
|
if (xhrCreatedConnections) {
|
|
|
|
async.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
jsonp.request(url).subscribe(() => {});
|
2015-07-30 10:29:16 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
describe('http', () => {
|
|
|
|
var url = 'http://foo.bar';
|
2015-06-19 15:14:12 -04:00
|
|
|
var http: Http;
|
|
|
|
var injector: Injector;
|
2015-04-29 02:07:55 -04:00
|
|
|
var backend: MockBackend;
|
|
|
|
var baseResponse;
|
2015-09-18 16:30:05 -04:00
|
|
|
var jsonp: Jsonp;
|
2015-04-29 02:07:55 -04:00
|
|
|
beforeEach(() => {
|
2015-06-13 18:49:05 -04:00
|
|
|
injector = Injector.resolveAndCreate([
|
|
|
|
BaseRequestOptions,
|
|
|
|
MockBackend,
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(
|
|
|
|
Http,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
2015-10-11 01:11:13 -04:00
|
|
|
return new Http(backend, defaultOptions);
|
|
|
|
},
|
|
|
|
deps: [MockBackend, BaseRequestOptions]
|
|
|
|
}),
|
|
|
|
provide(
|
|
|
|
Jsonp,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
2015-10-11 01:11:13 -04:00
|
|
|
return new Jsonp(backend, defaultOptions);
|
|
|
|
},
|
|
|
|
deps: [MockBackend, BaseRequestOptions]
|
|
|
|
})
|
2015-06-13 18:49:05 -04:00
|
|
|
]);
|
2015-04-29 02:07:55 -04:00
|
|
|
http = injector.get(Http);
|
2015-09-18 16:30:05 -04:00
|
|
|
jsonp = injector.get(Jsonp);
|
2015-04-29 02:07:55 -04:00
|
|
|
backend = injector.get(MockBackend);
|
2015-06-24 03:27:07 -04:00
|
|
|
baseResponse = new Response(new ResponseOptions({body: 'base response'}));
|
2015-04-29 02:07:55 -04:00
|
|
|
});
|
|
|
|
|
2015-06-13 18:49:05 -04:00
|
|
|
afterEach(() => backend.verifyNoPendingRequests());
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-13 18:49:05 -04:00
|
|
|
describe('Http', () => {
|
2015-06-13 19:44:32 -04:00
|
|
|
describe('.request()', () => {
|
2015-06-09 18:18:57 -04:00
|
|
|
it('should return an Observable',
|
2015-10-01 19:04:20 -04:00
|
|
|
() => { expect(http.request(url)).toBeAnInstanceOf(Observable); });
|
2015-06-13 19:44:32 -04:00
|
|
|
|
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
it('should accept a fully-qualified request as its only parameter',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-06-19 15:14:12 -04:00
|
|
|
expect(c.request.url).toBe('https://google.com');
|
2015-06-24 03:27:07 -04:00
|
|
|
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
|
2015-06-19 15:14:12 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.request(new Request(new RequestOptions({url: 'https://google.com'})))
|
|
|
|
.subscribe((res) => {});
|
2015-06-19 15:14:12 -04:00
|
|
|
}));
|
2015-06-13 18:49:05 -04:00
|
|
|
|
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
it('should perform a get request for given url if only passed a string',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => c.mockRespond(baseResponse));
|
|
|
|
http.request('http://basic.connection')
|
|
|
|
.subscribe(res => {
|
|
|
|
expect(res.text()).toBe('base response');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should perform a get request and complete the response',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
backend.connections.subscribe(c => c.mockRespond(baseResponse));
|
|
|
|
http.request('http://basic.connection')
|
|
|
|
.subscribe(res => { expect(res.text()).toBe('base response'); }, null,
|
|
|
|
() => { async.done(); });
|
2015-06-19 15:14:12 -04:00
|
|
|
}));
|
2015-06-13 18:49:05 -04:00
|
|
|
|
2015-10-01 19:04:20 -04:00
|
|
|
it('should perform multiple get requests and complete the responses',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
backend.connections.subscribe(c => c.mockRespond(baseResponse));
|
|
|
|
|
|
|
|
http.request('http://basic.connection')
|
|
|
|
.subscribe(res => { expect(res.text()).toBe('base response'); });
|
|
|
|
http.request('http://basic.connection')
|
|
|
|
.subscribe(res => { expect(res.text()).toBe('base response'); }, null,
|
|
|
|
() => { async.done(); });
|
|
|
|
}));
|
2015-06-19 15:14:12 -04:00
|
|
|
// TODO: make dart not complain about "argument type 'Map' cannot be assigned to the
|
|
|
|
// parameter type 'IRequestOptions'"
|
|
|
|
// xit('should perform a get request for given url if passed a dictionary',
|
|
|
|
// inject([AsyncTestCompleter], async => {
|
|
|
|
// ObservableWrapper.subscribe(backend.connections, c => c.mockRespond(baseResponse));
|
|
|
|
// ObservableWrapper.subscribe(http.request(url, {method: RequestMethods.GET}), res =>
|
|
|
|
// {
|
|
|
|
// expect(res.text()).toBe('base response');
|
|
|
|
// async.done();
|
|
|
|
// });
|
|
|
|
// }));
|
2015-09-18 16:30:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should throw if url is not a string or Request', () => {
|
|
|
|
var req = <Request>{};
|
|
|
|
expect(() => http.request(req))
|
|
|
|
.toThrowError('First argument must be a url string or Request instance.');
|
|
|
|
});
|
2015-06-19 15:14:12 -04:00
|
|
|
});
|
2015-06-13 18:49:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe('.get()', () => {
|
|
|
|
it('should perform a get request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Get);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.get(url).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('.post()', () => {
|
|
|
|
it('should perform a post request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Post);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.post(url, 'post me').subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should attach the provided body to the request', inject([AsyncTestCompleter], async => {
|
2015-06-19 15:14:12 -04:00
|
|
|
var body = 'this is my post body';
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-06-13 18:49:05 -04:00
|
|
|
expect(c.request.text()).toBe(body);
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.post(url, body).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('.put()', () => {
|
|
|
|
it('should perform a put request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Put);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.put(url, 'put me').subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should attach the provided body to the request', inject([AsyncTestCompleter], async => {
|
|
|
|
var body = 'this is my put body';
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-06-13 18:49:05 -04:00
|
|
|
expect(c.request.text()).toBe(body);
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.put(url, body).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('.delete()', () => {
|
|
|
|
it('should perform a delete request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Delete);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.delete(url).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('.patch()', () => {
|
|
|
|
it('should perform a patch request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Patch);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
2015-04-29 02:07:55 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.patch(url, 'this is my patch body').subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-13 18:49:05 -04:00
|
|
|
it('should attach the provided body to the request', inject([AsyncTestCompleter], async => {
|
2015-06-19 15:14:12 -04:00
|
|
|
var body = 'this is my patch body';
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-06-13 18:49:05 -04:00
|
|
|
expect(c.request.text()).toBe(body);
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.patch(url, body).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-13 18:49:05 -04:00
|
|
|
|
|
|
|
describe('.head()', () => {
|
|
|
|
it('should perform a head request for given url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-08-26 16:40:12 -04:00
|
|
|
expect(c.request.method).toBe(RequestMethods.Head);
|
2015-06-13 18:49:05 -04:00
|
|
|
backend.resolveAllConnections();
|
2015-04-29 02:07:55 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.head(url).subscribe(res => {});
|
2015-06-13 18:49:05 -04:00
|
|
|
}));
|
|
|
|
});
|
2015-07-13 14:47:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe('searchParams', () => {
|
|
|
|
it('should append search params to url', inject([AsyncTestCompleter], async => {
|
|
|
|
var params = new URLSearchParams();
|
|
|
|
params.append('q', 'puppies');
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-07-13 14:47:10 -04:00
|
|
|
expect(c.request.url).toEqual('https://www.google.com?q=puppies');
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.get('https://www.google.com', new RequestOptions({search: params}))
|
|
|
|
.subscribe(res => {});
|
2015-07-13 14:47:10 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should append string search params to url', inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-07-13 14:47:10 -04:00
|
|
|
expect(c.request.url).toEqual('https://www.google.com?q=piggies');
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.get('https://www.google.com', new RequestOptions({search: 'q=piggies'}))
|
|
|
|
.subscribe(res => {});
|
2015-07-13 14:47:10 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should produce valid url when url already contains a query',
|
|
|
|
inject([AsyncTestCompleter], async => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-07-13 14:47:10 -04:00
|
|
|
expect(c.request.url).toEqual('https://www.google.com?q=angular&as_eq=1.x');
|
|
|
|
backend.resolveAllConnections();
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.get('https://www.google.com?q=angular', new RequestOptions({search: 'as_eq=1.x'}))
|
|
|
|
.subscribe(res => {});
|
2015-07-13 14:47:10 -04:00
|
|
|
}));
|
|
|
|
});
|
2015-09-21 03:46:16 -04:00
|
|
|
|
|
|
|
describe('string method names', () => {
|
|
|
|
it('should allow case insensitive strings for method names', () => {
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-01 19:04:20 -04:00
|
|
|
backend.connections.subscribe(c => {
|
2015-09-21 03:46:16 -04:00
|
|
|
expect(c.request.method)
|
|
|
|
.toBe(RequestMethods.Post)
|
|
|
|
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-01 19:04:20 -04:00
|
|
|
http.request(
|
|
|
|
new Request(new RequestOptions({url: 'https://google.com', method: 'PosT'})))
|
|
|
|
.subscribe((res) => {});
|
2015-09-21 03:46:16 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when invalid string parameter is passed for method name', () => {
|
|
|
|
expect(() => {
|
|
|
|
http.request(
|
|
|
|
new Request(new RequestOptions({url: 'https://google.com', method: 'Invalid'})));
|
|
|
|
}).toThrowError('Invalid request method. The method "Invalid" is not supported.');
|
|
|
|
});
|
|
|
|
});
|
2015-06-13 18:49:05 -04:00
|
|
|
});
|
2015-09-18 16:30:05 -04:00
|
|
|
|
|
|
|
describe('Jsonp', () => {
|
|
|
|
describe('.request()', () => {
|
|
|
|
it('should throw if url is not a string or Request', () => {
|
|
|
|
var req = <Request>{};
|
|
|
|
expect(() => jsonp.request(req))
|
|
|
|
.toThrowError('First argument must be a url string or Request instance.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
});
|
|
|
|
}
|