X Tutup
Skip to content

Commit e1d7bdc

Browse files
committed
fix(http): Fix all requests defaulting to Get
Honor method parameter passed to http.request(). Closes angular#5309 Closes angular#5397
1 parent 46fc153 commit e1d7bdc

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

modules/angular2/src/http/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
1616
var newOptions = defaultOpts;
1717
if (isPresent(providedOpts)) {
1818
// Hack so Dart can used named parameters
19-
newOptions = newOptions.merge(new RequestOptions({
20-
method: providedOpts.method,
21-
url: providedOpts.url,
19+
return newOptions.merge(new RequestOptions({
20+
method: providedOpts.method || method,
21+
url: providedOpts.url || url,
2222
search: providedOpts.search,
2323
headers: providedOpts.headers,
2424
body: providedOpts.body

modules/angular2/test/http/http_spec.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ export function main() {
151151
.subscribe((res) => {});
152152
}));
153153

154+
it('should accept a fully-qualified request as its only parameter',
155+
inject([AsyncTestCompleter], (async) => {
156+
backend.connections.subscribe(c => {
157+
expect(c.request.url).toBe('https://google.com');
158+
expect(c.request.method).toBe(RequestMethods.Post);
159+
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
160+
async.done();
161+
});
162+
http.request(new Request(new RequestOptions(
163+
{url: 'https://google.com', method: RequestMethods.Post})))
164+
.subscribe((res) => {});
165+
}));
166+
154167

155168
it('should perform a get request for given url if only passed a string',
156169
inject([AsyncTestCompleter], (async) => {
@@ -162,6 +175,34 @@ export function main() {
162175
});
163176
}));
164177

178+
it('should perform a post request for given url if options include a method',
179+
inject([AsyncTestCompleter], (async) => {
180+
backend.connections.subscribe(c => {
181+
expect(c.request.method).toEqual(RequestMethods.Post);
182+
c.mockRespond(baseResponse);
183+
});
184+
let requestOptions = new RequestOptions({method: RequestMethods.Post});
185+
http.request('http://basic.connection', requestOptions)
186+
.subscribe(res => {
187+
expect(res.text()).toBe('base response');
188+
async.done();
189+
});
190+
}));
191+
192+
it('should perform a post request for given url if options include a method',
193+
inject([AsyncTestCompleter], (async) => {
194+
backend.connections.subscribe(c => {
195+
expect(c.request.method).toEqual(RequestMethods.Post);
196+
c.mockRespond(baseResponse);
197+
});
198+
let requestOptions = {method: RequestMethods.Post};
199+
http.request('http://basic.connection', requestOptions)
200+
.subscribe(res => {
201+
expect(res.text()).toBe('base response');
202+
async.done();
203+
});
204+
}));
205+
165206
it('should perform a get request and complete the response',
166207
inject([AsyncTestCompleter], (async) => {
167208
backend.connections.subscribe(c => c.mockRespond(baseResponse));
@@ -180,18 +221,6 @@ export function main() {
180221
.subscribe(res => { expect(res.text()).toBe('base response'); }, null,
181222
() => { async.done(); });
182223
}));
183-
// TODO: make dart not complain about "argument type 'Map' cannot be assigned to the
184-
// parameter type 'IRequestOptions'"
185-
// xit('should perform a get request for given url if passed a dictionary',
186-
// inject([AsyncTestCompleter], async => {
187-
// ObservableWrapper.subscribe(backend.connections, c => c.mockRespond(baseResponse));
188-
// ObservableWrapper.subscribe(http.request(url, {method: RequestMethods.GET}), res =>
189-
// {
190-
// expect(res.text()).toBe('base response');
191-
// async.done();
192-
// });
193-
// }));
194-
195224

196225
it('should throw if url is not a string or Request', () => {
197226
var req = <Request>{};

0 commit comments

Comments
 (0)
X Tutup