diff --git a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/Http.ts b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/Http.ts index 170761646f..d914b6b22b 100644 --- a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/Http.ts +++ b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/Http.ts @@ -7,19 +7,22 @@ const httpClientTypeName = 'BrowserHttpMessageHandler'; const httpClientFullTypeName = `${httpClientNamespace}.${httpClientTypeName}`; let receiveResponseMethod: MethodHandle; -registerFunction(`${httpClientFullTypeName}.Send`, (id: number, method: string, requestUri: string, body: string | null, headersJson: string | null) => { - sendAsync(id, method, requestUri, body, headersJson); +registerFunction(`${httpClientFullTypeName}.Send`, (id: number, method: string, requestUri: string, body: string | null, headersJson: string | null, fetchArgs: RequestInit | null) => { + sendAsync(id, method, requestUri, body, headersJson, fetchArgs); }); -async function sendAsync(id: number, method: string, requestUri: string, body: string | null, headersJson: string | null) { +async function sendAsync(id: number, method: string, requestUri: string, body: string | null, headersJson: string | null, fetchArgs: RequestInit | null) { let response: Response; let responseText: string; + + const requestInit = fetchArgs || {}; + requestInit.method = method; + requestInit.body = body || undefined; + try { - response = await fetch(requestUri, { - method: method, - body: body || undefined, - headers: headersJson ? (JSON.parse(headersJson) as string[][]) : undefined - }); + requestInit.headers = headersJson ? (JSON.parse(headersJson) as string[][]) : undefined; + + response = await fetch(requestUri, requestInit); responseText = await response.text(); } catch (ex) { dispatchErrorResponse(id, ex.toString()); diff --git a/src/Microsoft.AspNetCore.Blazor.Browser/Http/BrowserHttpMessageHandler.cs b/src/Microsoft.AspNetCore.Blazor.Browser/Http/BrowserHttpMessageHandler.cs index de00e0a3a5..4bf5dc6c4c 100644 --- a/src/Microsoft.AspNetCore.Blazor.Browser/Http/BrowserHttpMessageHandler.cs +++ b/src/Microsoft.AspNetCore.Blazor.Browser/Http/BrowserHttpMessageHandler.cs @@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Http static IDictionary> _pendingRequests = new Dictionary>(); + public const string FetchArgs = "BrowserHttpMessageHandler.FetchArgs"; + /// protected override async Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) @@ -36,13 +38,16 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Http _pendingRequests.Add(id, tcs); } + request.Properties.TryGetValue(FetchArgs, out var fetchArgs); + RegisteredFunction.Invoke( $"{typeof(BrowserHttpMessageHandler).FullName}.Send", id, request.Method.Method, request.RequestUri, request.Content == null ? null : await GetContentAsString(request.Content), - SerializeHeadersAsJson(request)); + SerializeHeadersAsJson(request), + fetchArgs); return await tcs.Task; } diff --git a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/HttpClientTest.cs b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/HttpClientTest.cs index 0a053681b3..2b6ab8d33d 100644 --- a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/HttpClientTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/HttpClientTest.cs @@ -97,6 +97,15 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests Assert.Equal("{\"id\":123,\"name\":\"Bert\"}", _responseBody.Text); } + [Fact] + public void CanSetRequestReferer() + { + SetValue("request-referrer", "/test-referrer"); + IssueRequest("GET", "/api/person/referrer"); + Assert.Equal("OK", _responseStatus.Text); + Assert.EndsWith("/test-referrer", _responseBody.Text); + } + private void IssueRequest(string requestMethod, string relativeUri, string requestBody = null) { var targetUri = new Uri(_apiServerFixture.RootUri, relativeUri); diff --git a/test/testapps/BasicTestApp/HttpClientTest/HttpRequestsComponent.cshtml b/test/testapps/BasicTestApp/HttpClientTest/HttpRequestsComponent.cshtml index 5a632491f5..5f9fd817b9 100644 --- a/test/testapps/BasicTestApp/HttpClientTest/HttpRequestsComponent.cshtml +++ b/test/testapps/BasicTestApp/HttpClientTest/HttpRequestsComponent.cshtml @@ -1,5 +1,6 @@ @using System.Net @using System.Net.Http +@using Microsoft.AspNetCore.Blazor.Browser.Http @inject HttpClient Http

HTTP request tester

@@ -37,6 +38,11 @@

+

+

Request referrer:
+ { requestReferrer = (string)value; }) /> +

+ @if (responseStatusCode.HasValue) @@ -59,6 +65,7 @@ string method = "GET"; string requestBody = ""; List requestHeaders = new List(); + string requestReferrer = ""; HttpStatusCode? responseStatusCode; string responseBody; @@ -87,6 +94,14 @@ } } + if (!string.IsNullOrEmpty(requestReferrer)) + { + requestMessage.Properties[BrowserHttpMessageHandler.FetchArgs] = new + { + referrer = requestReferrer + }; + } + var response = await Http.SendAsync(requestMessage); responseStatusCode = response.StatusCode; responseBody = await response.Content.ReadAsStringAsync(); diff --git a/test/testapps/TestServer/Controllers/PersonController.cs b/test/testapps/TestServer/Controllers/PersonController.cs index f765566e52..9b5bd6f230 100644 --- a/test/testapps/TestServer/Controllers/PersonController.cs +++ b/test/testapps/TestServer/Controllers/PersonController.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; -using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Cors; @@ -32,6 +31,12 @@ namespace TestServer.Controllers } } + [HttpGet("referrer")] + public string GetReferer() + { + return $"The referrer is: {Request.Headers["Referer"].ToString()}"; + } + // PUT api/person [HttpPut] public Person Put([FromBody, Required] Person person)