mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
Commit migrated from https://github.com/dotnet/corefx/commit/ec6a7215bb28a061d2357d8c3874447bc575132d
102 lines
4.0 KiB
C#
102 lines
4.0 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
public static class TestHelper
|
|
{
|
|
public static bool JsonMessageContainsKeyValue(string message, string key, string value)
|
|
{
|
|
// TODO (#5525): Align with the rest of tests w.r.t response parsing once the test server is finalized.
|
|
// Currently not adding any new dependencies
|
|
string pattern = string.Format(@"""{0}"": ""{1}""", key, value);
|
|
return message.Contains(pattern);
|
|
}
|
|
|
|
public static bool JsonMessageContainsKey(string message, string key)
|
|
{
|
|
// TODO (#5525): Align with the rest of tests w.r.t response parsing once the test server is finalized.
|
|
// Currently not adding any new dependencies
|
|
string pattern = string.Format(@"""{0}"": """, key);
|
|
return message.Contains(pattern);
|
|
}
|
|
|
|
public static void VerifyResponseBody(
|
|
string responseContent,
|
|
byte[] expectedMD5Hash,
|
|
bool chunkedUpload,
|
|
string requestBody)
|
|
{
|
|
// Verify that response body from the server was corrected received by comparing MD5 hash.
|
|
byte[] actualMD5Hash = ComputeMD5Hash(responseContent);
|
|
Assert.Equal(expectedMD5Hash, actualMD5Hash);
|
|
|
|
// Verify upload semantics: 'Content-Length' vs. 'Transfer-Encoding: chunked'.
|
|
if (requestBody != null)
|
|
{
|
|
bool requestUsedContentLengthUpload =
|
|
JsonMessageContainsKeyValue(responseContent, "Content-Length", requestBody.Length.ToString());
|
|
bool requestUsedChunkedUpload =
|
|
JsonMessageContainsKeyValue(responseContent, "Transfer-Encoding", "chunked");
|
|
if (requestBody.Length > 0)
|
|
{
|
|
Assert.NotEqual(requestUsedContentLengthUpload, requestUsedChunkedUpload);
|
|
Assert.Equal(chunkedUpload, requestUsedChunkedUpload);
|
|
Assert.Equal(!chunkedUpload, requestUsedContentLengthUpload);
|
|
}
|
|
|
|
// Verify that request body content was correctly sent to server.
|
|
Assert.True(JsonMessageContainsKeyValue(responseContent, "BodyContent", requestBody), "Valid request body");
|
|
}
|
|
}
|
|
|
|
public static void VerifyRequestMethod(HttpResponseMessage response, string expectedMethod)
|
|
{
|
|
IEnumerable<string> values = response.Headers.GetValues("X-HttpRequest-Method");
|
|
foreach (string value in values)
|
|
{
|
|
Assert.Equal(expectedMethod, value);
|
|
}
|
|
}
|
|
|
|
public static byte[] ComputeMD5Hash(string data)
|
|
{
|
|
return ComputeMD5Hash(Encoding.UTF8.GetBytes(data));
|
|
}
|
|
|
|
public static byte[] ComputeMD5Hash(byte[] data)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
return md5.ComputeHash(data);
|
|
}
|
|
}
|
|
|
|
public static Task WhenAllCompletedOrAnyFailed(params Task[] tasks)
|
|
{
|
|
return TaskTimeoutExtensions.WhenAllOrAnyFailed(tasks);
|
|
}
|
|
|
|
public static Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> AllowAllCertificates = (_, __, ___, ____) => true;
|
|
|
|
public static IPAddress GetIPv6LinkLocalAddress() =>
|
|
NetworkInterface
|
|
.GetAllNetworkInterfaces()
|
|
.SelectMany(i => i.GetIPProperties().UnicastAddresses)
|
|
.Select(a => a.Address)
|
|
.Where(a => a.IsIPv6LinkLocal)
|
|
.FirstOrDefault();
|
|
}
|
|
}
|