This reverts commit 9edea0bb75
.
PR Close #37547
This commit is contained in:
@ -10,13 +10,10 @@
|
|||||||
const xhr2: any = require('xhr2');
|
const xhr2: any = require('xhr2');
|
||||||
|
|
||||||
import {Injectable, Injector, Provider} from '@angular/core';
|
import {Injectable, Injector, Provider} from '@angular/core';
|
||||||
import {DOCUMENT} from '@angular/common';
|
|
||||||
import {HttpEvent, HttpRequest, HttpHandler, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
|
|
||||||
import {Observable, Observer, Subscription} from 'rxjs';
|
|
||||||
|
|
||||||
// @see https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01#URI-syntax
|
import {HttpEvent, HttpRequest, HttpHandler, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
|
||||||
const isAbsoluteUrl = /^[a-zA-Z\-\+.]+:\/\//;
|
|
||||||
const FORWARD_SLASH = '/';
|
import {Observable, Observer, Subscription} from 'rxjs';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ServerXhr implements XhrFactory {
|
export class ServerXhr implements XhrFactory {
|
||||||
@ -105,21 +102,11 @@ export abstract class ZoneMacroTaskWrapper<S, R> {
|
|||||||
|
|
||||||
export class ZoneClientBackend extends
|
export class ZoneClientBackend extends
|
||||||
ZoneMacroTaskWrapper<HttpRequest<any>, HttpEvent<any>> implements HttpBackend {
|
ZoneMacroTaskWrapper<HttpRequest<any>, HttpEvent<any>> implements HttpBackend {
|
||||||
constructor(private backend: HttpBackend, private doc: Document) {
|
constructor(private backend: HttpBackend) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
|
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
|
||||||
const href = this.doc.location.href;
|
|
||||||
if (!isAbsoluteUrl.test(request.url) && href) {
|
|
||||||
const urlParts = Array.from(request.url);
|
|
||||||
if (request.url[0] === FORWARD_SLASH && href[href.length - 1] === FORWARD_SLASH) {
|
|
||||||
urlParts.shift();
|
|
||||||
} else if (request.url[0] !== FORWARD_SLASH && href[href.length - 1] !== FORWARD_SLASH) {
|
|
||||||
urlParts.splice(0, 0, FORWARD_SLASH);
|
|
||||||
}
|
|
||||||
return this.wrap(request.clone({url: href + urlParts.join('')}));
|
|
||||||
}
|
|
||||||
return this.wrap(request);
|
return this.wrap(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,16 +115,12 @@ export class ZoneClientBackend extends
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function zoneWrappedInterceptingHandler(
|
export function zoneWrappedInterceptingHandler(backend: HttpBackend, injector: Injector) {
|
||||||
backend: HttpBackend, injector: Injector, doc: Document) {
|
|
||||||
const realBackend: HttpBackend = new HttpInterceptingHandler(backend, injector);
|
const realBackend: HttpBackend = new HttpInterceptingHandler(backend, injector);
|
||||||
return new ZoneClientBackend(realBackend, doc);
|
return new ZoneClientBackend(realBackend);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SERVER_HTTP_PROVIDERS: Provider[] = [
|
export const SERVER_HTTP_PROVIDERS: Provider[] = [
|
||||||
{provide: XhrFactory, useClass: ServerXhr}, {
|
{provide: XhrFactory, useClass: ServerXhr},
|
||||||
provide: HttpHandler,
|
{provide: HttpHandler, useFactory: zoneWrappedInterceptingHandler, deps: [HttpBackend, Injector]}
|
||||||
useFactory: zoneWrappedInterceptingHandler,
|
|
||||||
deps: [HttpBackend, Injector, DOCUMENT]
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
@ -793,54 +793,6 @@ describe('platform-server integration', () => {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('can make relative HttpClient requests', async () => {
|
|
||||||
const platform = platformDynamicServer([
|
|
||||||
{provide: INITIAL_CONFIG, useValue: {document: '<app></app>', url: 'http://localhost'}}
|
|
||||||
]);
|
|
||||||
const ref = await platform.bootstrapModule(HttpClientExampleModule);
|
|
||||||
const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
|
|
||||||
const http = ref.injector.get(HttpClient);
|
|
||||||
ref.injector.get(NgZone).run(() => {
|
|
||||||
http.get<string>('/testing').subscribe((body: string) => {
|
|
||||||
NgZone.assertInAngularZone();
|
|
||||||
expect(body).toEqual('success!');
|
|
||||||
});
|
|
||||||
mock.expectOne('http://localhost/testing').flush('success!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can make relative HttpClient requests two slashes', async () => {
|
|
||||||
const platform = platformDynamicServer([
|
|
||||||
{provide: INITIAL_CONFIG, useValue: {document: '<app></app>', url: 'http://localhost/'}}
|
|
||||||
]);
|
|
||||||
const ref = await platform.bootstrapModule(HttpClientExampleModule);
|
|
||||||
const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
|
|
||||||
const http = ref.injector.get(HttpClient);
|
|
||||||
ref.injector.get(NgZone).run(() => {
|
|
||||||
http.get<string>('/testing').subscribe((body: string) => {
|
|
||||||
NgZone.assertInAngularZone();
|
|
||||||
expect(body).toEqual('success!');
|
|
||||||
});
|
|
||||||
mock.expectOne('http://localhost/testing').flush('success!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can make relative HttpClient requests no slashes', async () => {
|
|
||||||
const platform = platformDynamicServer([
|
|
||||||
{provide: INITIAL_CONFIG, useValue: {document: '<app></app>', url: 'http://localhost'}}
|
|
||||||
]);
|
|
||||||
const ref = await platform.bootstrapModule(HttpClientExampleModule);
|
|
||||||
const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
|
|
||||||
const http = ref.injector.get(HttpClient);
|
|
||||||
ref.injector.get(NgZone).run(() => {
|
|
||||||
http.get<string>('testing').subscribe((body: string) => {
|
|
||||||
NgZone.assertInAngularZone();
|
|
||||||
expect(body).toEqual('success!');
|
|
||||||
});
|
|
||||||
mock.expectOne('http://localhost/testing').flush('success!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('requests are macrotasks', async(() => {
|
it('requests are macrotasks', async(() => {
|
||||||
const platform = platformDynamicServer(
|
const platform = platformDynamicServer(
|
||||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
|
Reference in New Issue
Block a user