mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-07-31 15:58:27 -04:00
* Move all unit tests to Engine/Tests * Fix Margin.EdgeSubtract * Fix Capsule.Contains * EnvironmentVariables.Remove if it's null * Fixed ray trace never returning startedsolid * Fix HistoryList.Navigate on empty list * Fix GameObject.WorldPosition accepting NaNs * Fix flex: initial expanding to the wrong grow/shrink * Translation.TryConvert shouldn't throw on invalid enum strings * Skip sound file tests on machines with no audio device
104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
|
|
namespace WebTests;
|
|
|
|
[TestClass]
|
|
public class HttpTest
|
|
{
|
|
// Hostname rows resolve through live DNS (Http.IsAllowed -> Uri.IsPrivate -> Dns.GetHostEntry),
|
|
// so they're excluded from PR runs - the offline address-based rows live in IsUriAllowed below.
|
|
[TestMethod]
|
|
[TestCategory( "LiveBackend" )]
|
|
[DataRow( "http://google.com", true )]
|
|
[DataRow( "https://google.com", true )]
|
|
[DataRow( "https://api.google.com", true )]
|
|
[DataRow( "https://yahoo.com", true )]
|
|
[DataRow( "https://127-0-0-1.mattstevens.co.uk/", false )]
|
|
[DataRow( "https://10-0-0-1.mattstevens.co.uk/", false )]
|
|
[DataRow( "https://192-168-1-1.mattstevens.co.uk/", false )]
|
|
public async Task IsUriAllowed_Hostnames( string uri, bool expected )
|
|
{
|
|
var u = new Uri( uri, UriKind.Absolute );
|
|
Assert.AreEqual( expected, Http.IsAllowed( u ) );
|
|
Assert.AreEqual( expected, await Http.IsAllowedAsync( u ) );
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow( "http://127.0.0.1", true )]
|
|
[DataRow( "http://127.0.0.1:80", true )]
|
|
[DataRow( "http://127.0.0.1:443", true )]
|
|
[DataRow( "http://127.0.0.1:8080", true )]
|
|
[DataRow( "http://127.0.0.1:8443", true )]
|
|
[DataRow( "http://127.0.0.1:1337", false )]
|
|
[DataRow( "https://localhost/", true )]
|
|
[DataRow( "https://localhost:80/", true )]
|
|
[DataRow( "https://localhost:443/", true )]
|
|
[DataRow( "https://localhost:8080/", true )]
|
|
[DataRow( "https://localhost:8443/", true )]
|
|
[DataRow( "https://localhost:1337/", false )]
|
|
[DataRow( "https://8.8.8.8/", false )]
|
|
[DataRow( "https://192.168.1.1/", false )]
|
|
[DataRow( "file://blah", false )]
|
|
public async Task IsUriAllowed( string uri, bool expected )
|
|
{
|
|
var u = new Uri( uri, UriKind.Absolute );
|
|
Assert.AreEqual( expected, Http.IsAllowed( u ) );
|
|
Assert.AreEqual( expected, await Http.IsAllowedAsync( u ) );
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow( "Authorization", true )]
|
|
[DataRow( "Host", false )]
|
|
[DataRow( "X-Test", true )]
|
|
[DataRow( "Proxy-Blah", false )]
|
|
[DataRow( "Sec-Blah", false )]
|
|
public void IsHeaderAllowed( string header, bool expected )
|
|
{
|
|
Assert.AreEqual( expected, Http.IsHeaderAllowed( header ) );
|
|
Assert.AreEqual( expected, Http.IsHeaderAllowed( header.ToUpperInvariant() ) );
|
|
Assert.AreEqual( expected, Http.IsHeaderAllowed( header.ToLowerInvariant() ) );
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CreateRequest_Valid_Succeeds()
|
|
{
|
|
var headers = new Dictionary<string, string> { { "X-Test", "1" } };
|
|
using var request = Http.CreateRequest( HttpMethod.Get, "https://google.com/", headers );
|
|
Assert.AreEqual( HttpMethod.Get, request.Method );
|
|
Assert.AreEqual( "https://google.com/", request.RequestUri?.ToString() );
|
|
Assert.AreEqual( 1, request.Headers.Count() );
|
|
Assert.IsTrue( request.Headers.Contains( "X-Test" ) );
|
|
CollectionAssert.AreEqual( new[] { "1" }, request.Headers.GetValues( "X-Test" ).ToArray() );
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CreateRequest_BadHeader_Throws()
|
|
{
|
|
var headers = new Dictionary<string, string> { { "Host", "blah" } };
|
|
Assert.ThrowsException<InvalidOperationException>( () => Http.CreateRequest( HttpMethod.Get, "http://google.com", headers ) );
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory( "LiveBackend" )]
|
|
public async Task GetString()
|
|
{
|
|
var str = await Http.RequestStringAsync( "https://google.com" );
|
|
|
|
Assert.IsFalse( string.IsNullOrEmpty( str ) );
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
[TestCategory( "LiveBackend" )]
|
|
public async Task GetBytes()
|
|
{
|
|
var bytes = await Http.RequestBytesAsync( "https://www.google.com/favicon.ico" );
|
|
|
|
Assert.IsNotNull( bytes );
|
|
Assert.IsTrue( bytes.Length > 0 );
|
|
}
|
|
}
|