angular-cn/packages/common/http/test/request_spec.ts
FDIM 1644d64398 feat(compiler-cli): introduce HttpContext request context (#25751)
A long-requested feature for HttpClient is the ability to store and retrieve
custom metadata for requests, especially in interceptors. This commit
implements this functionality via a new context object for requests.

Each outgoing HttpRequest now has an associated "context", an instance of
the HttpContext class. An HttpContext can be provided when making a request,
or if not then an empty context is created for the new request. This context
shares its lifecycle with the entire request, even across operations that
change the identity of the HttpRequest instance such as RxJS retries.

The HttpContext functions as an expando. Users can create typed tokens as instances of HttpContextToken, and
read/write a value for the key from any HttpContext object.

This commit implements the HttpContext functionality. A followup commit will
add angular.io documentation.

PR Close #25751
2021-03-15 10:33:48 -07:00

186 lines
7.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC 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 {HttpContext} from '@angular/common/http/src/context';
import {HttpHeaders} from '@angular/common/http/src/headers';
import {HttpParams} from '@angular/common/http/src/params';
import {HttpRequest} from '@angular/common/http/src/request';
import {ddescribe, describe, it} from '@angular/core/testing/src/testing_internal';
const TEST_URL = 'https://angular.io/';
const TEST_STRING = `I'm a body!`;
{
describe('HttpRequest', () => {
describe('constructor', () => {
it('initializes url', () => {
const req = new HttpRequest('', TEST_URL, null);
expect(req.url).toBe(TEST_URL);
});
it('doesn\'t require a body for body-less methods', () => {
let req = new HttpRequest('GET', TEST_URL);
expect(req.method).toBe('GET');
expect(req.body).toBeNull();
req = new HttpRequest('HEAD', TEST_URL);
expect(req.method).toBe('HEAD');
expect(req.body).toBeNull();
req = new HttpRequest('JSONP', TEST_URL);
expect(req.method).toBe('JSONP');
expect(req.body).toBeNull();
req = new HttpRequest('OPTIONS', TEST_URL);
expect(req.method).toBe('OPTIONS');
expect(req.body).toBeNull();
});
it('accepts a string request method', () => {
const req = new HttpRequest('TEST', TEST_URL, null);
expect(req.method).toBe('TEST');
});
it('accepts a string body', () => {
const req = new HttpRequest('POST', TEST_URL, TEST_STRING);
expect(req.body).toBe(TEST_STRING);
});
it('accepts an object body', () => {
const req = new HttpRequest('POST', TEST_URL, {data: TEST_STRING});
expect(req.body).toEqual({data: TEST_STRING});
});
it('creates default headers if not passed', () => {
const req = new HttpRequest('GET', TEST_URL);
expect(req.headers instanceof HttpHeaders).toBeTruthy();
});
it('uses the provided headers if passed', () => {
const headers = new HttpHeaders();
const req = new HttpRequest('GET', TEST_URL, {headers});
expect(req.headers).toBe(headers);
});
it('uses the provided context if passed', () => {
const context = new HttpContext();
const req = new HttpRequest('GET', TEST_URL, {context});
expect(req.context).toBe(context);
});
it('defaults to Json', () => {
const req = new HttpRequest('GET', TEST_URL);
expect(req.responseType).toBe('json');
});
});
describe('clone() copies the request', () => {
const headers = new HttpHeaders({
'Test': 'Test header',
});
const context = new HttpContext();
const req = new HttpRequest('POST', TEST_URL, 'test body', {
headers,
context,
reportProgress: true,
responseType: 'text',
withCredentials: true,
});
it('in the base case', () => {
const clone = req.clone();
expect(clone.method).toBe('POST');
expect(clone.responseType).toBe('text');
expect(clone.url).toBe(TEST_URL);
// Headers should be the same, as the headers are sealed.
expect(clone.headers).toBe(headers);
expect(clone.headers.get('Test')).toBe('Test header');
expect(clone.context).toBe(context);
});
it('and updates the url', () => {
expect(req.clone({url: '/changed'}).url).toBe('/changed');
});
it('and updates the method', () => {
expect(req.clone({method: 'PUT'}).method).toBe('PUT');
});
it('and updates the body', () => {
expect(req.clone({body: 'changed body'}).body).toBe('changed body');
});
it('and updates the context', () => {
const newContext = new HttpContext();
expect(req.clone({context: newContext}).context).toBe(newContext);
});
});
describe('content type detection', () => {
const baseReq = new HttpRequest('POST', '/test', null);
it('handles a null body', () => {
expect(baseReq.detectContentTypeHeader()).toBeNull();
});
it('doesn\'t associate a content type with ArrayBuffers', () => {
const req = baseReq.clone({body: new ArrayBuffer(4)});
expect(req.detectContentTypeHeader()).toBeNull();
});
it('handles strings as text', () => {
const req = baseReq.clone({body: 'hello world'});
expect(req.detectContentTypeHeader()).toBe('text/plain');
});
it('handles arrays as json', () => {
const req = baseReq.clone({body: ['a', 'b']});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
it('handles numbers as json', () => {
const req = baseReq.clone({body: 314159});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
it('handles objects as json', () => {
const req = baseReq.clone({body: {data: 'test data'}});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
});
describe('body serialization', () => {
const baseReq = new HttpRequest('POST', '/test', null);
it('handles a null body', () => {
expect(baseReq.serializeBody()).toBeNull();
});
it('passes ArrayBuffers through', () => {
const body = new ArrayBuffer(4);
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('passes strings through', () => {
const body = 'hello world';
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('serializes arrays as json', () => {
expect(baseReq.clone({body: ['a', 'b']}).serializeBody()).toBe('["a","b"]');
});
it('handles numbers as json', () => {
expect(baseReq.clone({body: 314159}).serializeBody()).toBe('314159');
});
it('handles objects as json', () => {
const req = baseReq.clone({body: {data: 'test data'}});
expect(req.serializeBody()).toBe('{"data":"test data"}');
});
it('serializes parameters as urlencoded', () => {
const params = new HttpParams().append('first', 'value').append('second', 'other');
const withParams = baseReq.clone({body: params});
expect(withParams.serializeBody()).toEqual('first=value&second=other');
expect(withParams.detectContentTypeHeader())
.toEqual('application/x-www-form-urlencoded;charset=UTF-8');
});
});
describe('parameter handling', () => {
const baseReq = new HttpRequest('GET', '/test', null);
const params = new HttpParams({fromString: 'test=true'});
it('appends parameters to a base URL', () => {
const req = baseReq.clone({params});
expect(req.urlWithParams).toEqual('/test?test=true');
});
it('appends parameters to a URL with an empty query string', () => {
const req = baseReq.clone({params, url: '/test?'});
expect(req.urlWithParams).toEqual('/test?test=true');
});
it('appends parameters to a URL with a query string', () => {
const req = baseReq.clone({params, url: '/test?other=false'});
expect(req.urlWithParams).toEqual('/test?other=false&test=true');
});
it('sets parameters via setParams', () => {
const req = baseReq.clone({setParams: {'test': 'false'}});
expect(req.urlWithParams).toEqual('/test?test=false');
});
});
});
}