Skip to content

Commit

Permalink
feat(http): added withCredentials support
Browse files Browse the repository at this point in the history
Taken into account the withCredentials property within the request options:
- added corresponding property in the RequestOptions class
- added corresponding property in the Request class
- handle this property when merging options
- set the withCredentials property on the XHR object when specified

Added a test in the xhr_backend_spec.ts to check that the property is actually
set on the XHR object

Closes https://github.com/angular/http/issues/65

Closes #7281

Closes #7281
  • Loading branch information
Thierry Templier authored and mhevery committed May 24, 2016
1 parent 0f0a8ad commit 95af14b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion modules/@angular/http/src/backends/xhr_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class XHRConnection implements Connection {
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
let _xhr: XMLHttpRequest = browserXHR.build();
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);

if (isPresent(req.withCredentials)) {
_xhr.withCredentials = req.withCredentials;
}
// load event handler
let onLoad = () => {
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
Expand Down
12 changes: 10 additions & 2 deletions modules/@angular/http/src/base_request_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ export class RequestOptions {
* Search parameters to be included in a {@link Request}.
*/
search: URLSearchParams;
constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) {
/**
* Enable use credentials for a {@link Request}.
*/
withCredentials: boolean;
constructor({method, headers, body, url, search, withCredentials}: RequestOptionsArgs = {}) {
this.method = isPresent(method) ? normalizeMethodName(method) : null;
this.headers = isPresent(headers) ? headers : null;
this.body = isPresent(body) ? body : null;
this.url = isPresent(url) ? url : null;
this.search = isPresent(search) ? (isString(search) ? new URLSearchParams(<string>(search)) :
<URLSearchParams>(search)) :
null;
this.withCredentials = isPresent(withCredentials) ? withCredentials : null;
}

/**
Expand Down Expand Up @@ -96,7 +101,10 @@ export class RequestOptions {
search: isPresent(options) && isPresent(options.search) ?
(isString(options.search) ? new URLSearchParams(<string>(options.search)) :
(<URLSearchParams>(options.search)).clone()) :
this.search
this.search,
withCredentials: isPresent(options) && isPresent(options.withCredentials) ?
options.withCredentials :
this.withCredentials
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion modules/@angular/http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function mergeOptions(defaultOpts: BaseRequestOptions, providedOpts: RequestOpti
url: providedOpts.url || url,
search: providedOpts.search,
headers: providedOpts.headers,
body: providedOpts.body
body: providedOpts.body,
withCredentials: providedOpts.withCredentials
}));
}
if (isPresent(method)) {
Expand Down
1 change: 1 addition & 0 deletions modules/@angular/http/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface RequestOptionsArgs {
search?: string | URLSearchParams;
headers?: Headers;
body?: any;
withCredentials?: boolean;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions modules/@angular/http/src/static_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class Request {
private _body: any;
/** Type of the request body **/
private contentType: ContentType;
/** Enable use credentials */
withCredentials: boolean;
constructor(requestOptions: RequestArgs) {
// TODO: assert that url is present
let url = requestOptions.url;
Expand All @@ -81,6 +83,7 @@ export class Request {
// Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.headers = new Headers(requestOptions.headers);
this.withCredentials = requestOptions.withCredentials;
}


Expand Down
21 changes: 21 additions & 0 deletions modules/@angular/http/test/backends/xhr_backend_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MockBrowserXHR extends BrowserXhr {
responseHeaders: string;
responseURL: string;

This comment has been minimized.

Copy link
@mahadbt2020

mahadbt2020 Jan 5, 2021

fbfbfb

statusText: string;
withCredentials: boolean;

constructor() {
super();
Expand Down Expand Up @@ -511,6 +512,26 @@ export function main() {
existingXHRs[0].dispatchEvent('load');
}));

it('should set withCredentials to true when defined in request options for CORS situations',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var statusCode = 200;
sampleRequest.withCredentials = true;
var mockXhr = new MockBrowserXHR();
var connection =
new XHRConnection(sampleRequest, mockXhr, new ResponseOptions({status: statusCode}));
var responseHeaders = `X-Request-URL: http://somedomain.com
Foo: Bar`

connection.response.subscribe((res: Response) => {
expect(res.url).toEqual('http://somedomain.com');
expect(existingXHRs[0].withCredentials).toBeTruthy();
async.done();
});

existingXHRs[0].setResponseHeaders(responseHeaders);
existingXHRs[0].setStatusCode(statusCode);
existingXHRs[0].dispatchEvent('load');
}));
});
});
}

4 comments on commit 95af14b

@AhsanAyaz
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 thanks

@aminebizid
Copy link

Choose a reason for hiding this comment

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

👍🏼

@templth
Copy link

Choose a reason for hiding this comment

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

Thanks very much, Miško!

@KaiBirkenstock
Copy link

Choose a reason for hiding this comment

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

Awesome! Thanks!

Please sign in to comment.