describe('http', () => {
var url = 'http://foo.bar';
var http: Http;
var injector: Injector;
var backend: MockBackend;
var baseResponse;
beforeEach(() => {
injector = Injector.resolveAndCreate([
BaseRequestOptions,
MockBackend,
bind(Http).toFactory(
function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
[MockBackend, BaseRequestOptions])
]);
http = injector.get(Http);
backend = injector.get(MockBackend);
baseResponse = new Response(new ResponseOptions({body: 'base response'}));
});
afterEach(() => backend.verifyNoPendingRequests());
describe('Http', () => {
describe('.request()', () => {
it('should return an Observable',
() => { expect(ObservableWrapper.isObservable(http.request(url))).toBe(true); });
it('should accept a fully-qualified request as its only parameter',
inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe<MockConnection>(backend.connections, c => {
expect(c.request.url).toBe('https://google.com');
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done();
});
ObservableWrapper.subscribe(
http.request(new Request(new RequestOptions({url: 'https://google.com'}))),
(res) => {});
}));
it('should perform a get request for given url if only passed a string',
inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe<MockConnection>(backend.connections,
c => c.mockRespond(baseResponse));
ObservableWrapper.subscribe<Response>(http.request('http://basic.connection'), res => {
expect(res.text()).toBe('base response');
async.done();
});
}));
// 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();
// });
// }));
});
describe('.get()', () => {
it('should perform a get request for given url', inject([AsyncTestCompleter], async => {
ObservableWrapper.subscribe<MockConnection>(backend.connections, c => {
expect(c.request.method).toBe(RequestMethods.GET);
backend.resolveAllConnections();
async.done();
});
ObservableWrapper.subscribe(http.get(url), res => {});
}));
});
describe('.post()', () => {
it('should perform a post request for given url', inject([AsyncTestCompleter], async => {
ObservableWrapper.subscribe<MockConnection>(backend.connections, c => {
expect(c.request.method).toBe(RequestMethods.POST);
backend.resolveAllConnections();
async.done();
});
ObservableWrapper.subscribe(http.post(url, 'post me'), res => {});
}));
it('should attach the provided body to the request', inject([AsyncTestCompleter], async => {
var body = 'this is my post body';
ObservableWrapper.subscribe<MockConnection>(backend.connections, c => {
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
ObservableWrapper.subscribe(http.post(url, body), res => {});
}));
});
//.........这里部分代码省略.........
请发表评论