Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions modules/@angular/http/src/static_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,41 @@ export class Request extends Body {
}
}
this._body = requestOptions.body;
this.contentType = this.detectContentType();
this.method = normalizeMethodName(requestOptions.method);
// TODO(jeffbcross): implement behavior
// Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.headers = new Headers(requestOptions.headers);
this.contentType = this.detectContentType();
this.withCredentials = requestOptions.withCredentials;
this.responseType = requestOptions.responseType;
}

/**
* Returns the content type enum based on header options.
*/
detectContentType(): ContentType {
switch (this.headers.get('content-type')) {
case 'application/json':
return ContentType.JSON;
case 'application/x-www-form-urlencoded':
return ContentType.FORM;
case 'multipart/form-data':
return ContentType.FORM_DATA;
case 'text/plain':
case 'text/html':
return ContentType.TEXT;
case 'application/octet-stream':
return ContentType.BLOB;
default:
return this.detectContentTypeFromBody();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user has specified a content-type that isn't in a case statement, won't default override that? Should detecting from the body only occur if the content type is not specified?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if not found it would default to whatever type it determined from the body. I was assuming that the limited content types specified as enums were the 'supported' types. And in the future the switch statement could be expanded as more enums are added/supported.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this PR does not change the behavior vs the previous impl.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I still want to send an empty content-type header. Now it always detects it from the body, can I somehow override that?

}
}

/**
* Returns the content type of request's body based on its type.
*/
detectContentType() {
detectContentTypeFromBody(): ContentType {
if (this._body == null) {
return ContentType.NONE;
} else if (this._body instanceof URLSearchParams) {
Expand Down
81 changes: 81 additions & 0 deletions modules/@angular/http/test/static_request_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {describe, expect, it} from '@angular/core/testing/testing_internal';

import {RequestOptions} from '../src/base_request_options';
import {ContentType} from '../src/enums';
import {Headers} from '../src/headers';
import {Request} from '../src/static_request';

export function main() {
describe('Request', () => {
describe('detectContentType', () => {
it('should return ContentType.NONE', () => {
const req = new Request(new RequestOptions({url: 'test', method: 'GET', body: null}));

expect(req.detectContentType()).toEqual(ContentType.NONE);
});

it('should return ContentType.JSON', () => {
const req = new Request(new RequestOptions({
url: 'test',
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/json'})
}));

expect(req.detectContentType()).toEqual(ContentType.JSON);
});

it('should return ContentType.FORM', () => {
const req = new Request(new RequestOptions({
url: 'test',
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'})
}));

expect(req.detectContentType()).toEqual(ContentType.FORM);
});

it('should return ContentType.FORM_DATA', () => {
const req = new Request(new RequestOptions({
url: 'test',
method: 'GET',
body: null,
headers: new Headers({'content-type': 'multipart/form-data'})
}));

expect(req.detectContentType()).toEqual(ContentType.FORM_DATA);
});

it('should return ContentType.TEXT', () => {
const req = new Request(new RequestOptions({
url: 'test',
method: 'GET',
body: null,
headers: new Headers({'content-type': 'text/plain'})
}));

expect(req.detectContentType()).toEqual(ContentType.TEXT);
});

it('should return ContentType.BLOB', () => {
const req = new Request(new RequestOptions({
url: 'test',
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/octet-stream'})
}));

expect(req.detectContentType()).toEqual(ContentType.BLOB);
});
});
});
}
1 change: 1 addition & 0 deletions tools/public_api_guard/http/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export declare class Request extends Body {
withCredentials: boolean;
constructor(requestOptions: RequestArgs);
detectContentType(): ContentType;
detectContentTypeFromBody(): ContentType;
getBody(): any;
}

Expand Down