Support arbitrary 'fetch' arguments
This commit is contained in:
parent
0c942ccc76
commit
1626b3b8c8
|
|
@ -7,19 +7,22 @@ const httpClientTypeName = 'BrowserHttpMessageHandler';
|
||||||
const httpClientFullTypeName = `${httpClientNamespace}.${httpClientTypeName}`;
|
const httpClientFullTypeName = `${httpClientNamespace}.${httpClientTypeName}`;
|
||||||
let receiveResponseMethod: MethodHandle;
|
let receiveResponseMethod: MethodHandle;
|
||||||
|
|
||||||
registerFunction(`${httpClientFullTypeName}.Send`, (id: number, method: string, requestUri: string, body: string | null, headersJson: string | null) => {
|
registerFunction(`${httpClientFullTypeName}.Send`, (id: number, method: string, requestUri: string, body: string | null, headersJson: string | null, fetchArgs: RequestInit | null) => {
|
||||||
sendAsync(id, method, requestUri, body, headersJson);
|
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 response: Response;
|
||||||
let responseText: string;
|
let responseText: string;
|
||||||
|
|
||||||
|
const requestInit = fetchArgs || {};
|
||||||
|
requestInit.method = method;
|
||||||
|
requestInit.body = body || undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await fetch(requestUri, {
|
requestInit.headers = headersJson ? (JSON.parse(headersJson) as string[][]) : undefined;
|
||||||
method: method,
|
|
||||||
body: body || undefined,
|
response = await fetch(requestUri, requestInit);
|
||||||
headers: headersJson ? (JSON.parse(headersJson) as string[][]) : undefined
|
|
||||||
});
|
|
||||||
responseText = await response.text();
|
responseText = await response.text();
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
dispatchErrorResponse(id, ex.toString());
|
dispatchErrorResponse(id, ex.toString());
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Http
|
||||||
static IDictionary<int, TaskCompletionSource<HttpResponseMessage>> _pendingRequests
|
static IDictionary<int, TaskCompletionSource<HttpResponseMessage>> _pendingRequests
|
||||||
= new Dictionary<int, TaskCompletionSource<HttpResponseMessage>>();
|
= new Dictionary<int, TaskCompletionSource<HttpResponseMessage>>();
|
||||||
|
|
||||||
|
public const string FetchArgs = "BrowserHttpMessageHandler.FetchArgs";
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override async Task<HttpResponseMessage> SendAsync(
|
protected override async Task<HttpResponseMessage> SendAsync(
|
||||||
HttpRequestMessage request, CancellationToken cancellationToken)
|
HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
|
@ -36,13 +38,16 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Http
|
||||||
_pendingRequests.Add(id, tcs);
|
_pendingRequests.Add(id, tcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request.Properties.TryGetValue(FetchArgs, out var fetchArgs);
|
||||||
|
|
||||||
RegisteredFunction.Invoke<object>(
|
RegisteredFunction.Invoke<object>(
|
||||||
$"{typeof(BrowserHttpMessageHandler).FullName}.Send",
|
$"{typeof(BrowserHttpMessageHandler).FullName}.Send",
|
||||||
id,
|
id,
|
||||||
request.Method.Method,
|
request.Method.Method,
|
||||||
request.RequestUri,
|
request.RequestUri,
|
||||||
request.Content == null ? null : await GetContentAsString(request.Content),
|
request.Content == null ? null : await GetContentAsString(request.Content),
|
||||||
SerializeHeadersAsJson(request));
|
SerializeHeadersAsJson(request),
|
||||||
|
fetchArgs);
|
||||||
|
|
||||||
return await tcs.Task;
|
return await tcs.Task;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,15 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
|
||||||
Assert.Equal("{\"id\":123,\"name\":\"Bert\"}", _responseBody.Text);
|
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)
|
private void IssueRequest(string requestMethod, string relativeUri, string requestBody = null)
|
||||||
{
|
{
|
||||||
var targetUri = new Uri(_apiServerFixture.RootUri, relativeUri);
|
var targetUri = new Uri(_apiServerFixture.RootUri, relativeUri);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
@using System.Net
|
@using System.Net
|
||||||
@using System.Net.Http
|
@using System.Net.Http
|
||||||
|
@using Microsoft.AspNetCore.Blazor.Browser.Http
|
||||||
@inject HttpClient Http
|
@inject HttpClient Http
|
||||||
|
|
||||||
<h1>HTTP request tester</h1>
|
<h1>HTTP request tester</h1>
|
||||||
|
|
@ -37,6 +38,11 @@
|
||||||
<button id="add-header" @onclick(AddHeader)>Add</button>
|
<button id="add-header" @onclick(AddHeader)>Add</button>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<div>Request referrer:</div>
|
||||||
|
<input id="request-referrer" type="text" value=@requestReferrer @onchange(value => { requestReferrer = (string)value; }) />
|
||||||
|
</p>
|
||||||
|
|
||||||
<button id="send-request" @onclick(DoRequest)>Request</button>
|
<button id="send-request" @onclick(DoRequest)>Request</button>
|
||||||
|
|
||||||
@if (responseStatusCode.HasValue)
|
@if (responseStatusCode.HasValue)
|
||||||
|
|
@ -59,6 +65,7 @@
|
||||||
string method = "GET";
|
string method = "GET";
|
||||||
string requestBody = "";
|
string requestBody = "";
|
||||||
List<RequestHeader> requestHeaders = new List<RequestHeader>();
|
List<RequestHeader> requestHeaders = new List<RequestHeader>();
|
||||||
|
string requestReferrer = "";
|
||||||
|
|
||||||
HttpStatusCode? responseStatusCode;
|
HttpStatusCode? responseStatusCode;
|
||||||
string responseBody;
|
string responseBody;
|
||||||
|
|
@ -87,6 +94,14 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(requestReferrer))
|
||||||
|
{
|
||||||
|
requestMessage.Properties[BrowserHttpMessageHandler.FetchArgs] = new
|
||||||
|
{
|
||||||
|
referrer = requestReferrer
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var response = await Http.SendAsync(requestMessage);
|
var response = await Http.SendAsync(requestMessage);
|
||||||
responseStatusCode = response.StatusCode;
|
responseStatusCode = response.StatusCode;
|
||||||
responseBody = await response.Content.ReadAsStringAsync();
|
responseBody = await response.Content.ReadAsStringAsync();
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Cors;
|
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
|
// PUT api/person
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
public Person Put([FromBody, Required] Person person)
|
public Person Put([FromBody, Required] Person person)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue