Skip to content

Commit

Permalink
Upgrade to C# 8 (#388)
Browse files Browse the repository at this point in the history
* Update netcore.yml

* Update appveyor.yml

* Update Targets

* Update appveyor.yml

* Change to VS 2019 VM

* Add Nullable Context

* Add Nullable context

* Revert to NetStandard2.0 and add nullable context

* Add nullable context

* Revert to Net standard 2.0

* Revert file TestRequest

* [UPD] to using var

* [ADD] new null checks feature to Embedio project

* [ADD] new null checks feature to Embedio Test project

* [ADD] new null checks feature to Embedio Unit Test project

* [UPD] Remove Samples from CI Build

* [FIX] Revert EndpointListener

* [FIX] Restore with a fix

* [UPD] Missing null checks

* [UPD] Missing null checks v2

* [UPD] Missing null checks v3

* Update netcore.yml

Include windows to matrix

* Update netcore.yml

Revert adding windows to the matrix

* [UPD] Swan including new null checks
  • Loading branch information
geoperez authored Oct 6, 2019
1 parent d1bb86b commit 0093d4e
Show file tree
Hide file tree
Showing 103 changed files with 795 additions and 1,066 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/netcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.2.108
- name: Build with dotnet
dotnet-version: 3.0.100
- name: Test with dotnet
run: dotnet test ./test/EmbedIO.Tests/EmbedIO.Tests.csproj -c Release
14 changes: 7 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '3.0.{build}'
image:
- Visual Studio 2017
- Visual Studio 2019
- Ubuntu
notifications:
- provider: Slack
Expand Down Expand Up @@ -36,12 +36,12 @@ before_build:
$command | netsh
build_script:
- cmd: msbuild /verbosity:quiet /p:Configuration=Release EmbedIO.sln
- cmd: |
cd src/EmbedIO.Forms.Sample/
msbuild /t:restore EmbedIO.Forms.Sample.sln
msbuild /verbosity:quiet /p:Configuration=Release EmbedIO.Forms.Sample.sln
cd ..
cd ..
# - cmd: |
# cd src/EmbedIO.Forms.Sample/
# msbuild /t:restore EmbedIO.Forms.Sample.sln
# msbuild /verbosity:quiet /p:Configuration=Release EmbedIO.Forms.Sample.sln
# cd ..
# cd ..
- cmd: InspectCode --swea -o=inspectcode.xml -s=Error --verbosity=ERROR EmbedIO.sln
- ps: |
[xml]$xml = Get-Content inspectcode.xml
Expand Down
5 changes: 3 additions & 2 deletions src/EmbedIO.Samples/EmbedIO.Samples.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>EmbedIO.Samples</AssemblyName>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<LangVersion>7.3</LangVersion>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Expand Down
5 changes: 3 additions & 2 deletions src/EmbedIO.Testing/EmbedIO.Testing.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.8</Version>
<Version>3.1.0</Version>
<AssemblyTitle>EmbedIO Web Server Testing</AssemblyTitle>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<LangVersion>8.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 4 additions & 6 deletions src/EmbedIO.Testing/HttpResponseMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ public static class HttpResponseMessageExtensions
/// </summary>
/// <param name="this">The <see cref="Task{TResult}"/> that will return the response.</param>
/// <returns>A <see cref="Task{TResult}"/> whose result will be the response body as a string.</returns>
public static async Task<string> ReceiveStringAsync(this Task<HttpResponseMessage> @this)
public static async Task<string?> ReceiveStringAsync(this Task<HttpResponseMessage> @this)
{
using (var response = await @this.ConfigureAwait(false))
{
if (response == null) return null;
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
using var response = await @this.ConfigureAwait(false);
if (response == null) return null;
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
4 changes: 2 additions & 2 deletions src/EmbedIO.Testing/Internal/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ internal TestContext(IHttpRequest request)

public RouteMatch Route { get; set; }

public string RequestedPath => Route.SubPath;
public string? RequestedPath => Route?.SubPath;

public IHttpResponse Response => TestResponse;

internal TestResponse TestResponse { get; }

public IPrincipal User { get; }
public IPrincipal? User { get; }

public ISessionProxy Session { get; set; }

Expand Down
71 changes: 33 additions & 38 deletions src/EmbedIO.Testing/Internal/TestMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (!string.IsNullOrEmpty(cookiesFromContainer))
serverRequest.Headers.Add(HttpHeaderNames.Cookie, cookiesFromContainer);

var context = new TestContext(serverRequest);
context.CancellationToken = cancellationToken;
context.Route = RouteMatch.UnsafeFromRoot(UrlPath.Normalize(serverRequest.Url.AbsolutePath, false));
var context = new TestContext(serverRequest)
{
CancellationToken = cancellationToken,
Route = RouteMatch.UnsafeFromRoot(UrlPath.Normalize(serverRequest.Url.AbsolutePath, false))

};

await _handler.HandleContextAsync(context).ConfigureAwait(false);
var serverResponse = context.TestResponse;
var responseCookies = serverResponse.Headers.Get(HttpHeaderNames.SetCookie);
if (!string.IsNullOrEmpty(responseCookies))
CookieContainer.SetCookies(serverRequest.Url, responseCookies);

var response = new HttpResponseMessage((HttpStatusCode) serverResponse.StatusCode) {
var response = new HttpResponseMessage((HttpStatusCode)serverResponse.StatusCode)
{
RequestMessage = request,
Version = serverResponse.ProtocolVersion,
ReasonPhrase = serverResponse.StatusDescription,
Expand All @@ -57,39 +62,29 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response;
}

private static ResponseHeaderType GetResponseHeaderType(string name)
{
// Not all headers are created equal in System.Net.Http.
// If a header is a "content" header, adding it to a HttpResponseMessage directly
// will cause an InvalidOperationException.
// The list of known headers with their respective "header types"
// is conveniently hidden in an internal class of System.Net.Http,
// because nobody outside the .NET team will ever need them, right?
// https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs
// Here are the "content" headers, extracted on 2019-07-06:
switch (name)
{
// Content-Length is set automatically and shall not be touched
case HttpHeaderNames.ContentLength:
return ResponseHeaderType.None;

// These headers belong to Content
case HttpHeaderNames.Allow:
case HttpHeaderNames.ContentDisposition:
case HttpHeaderNames.ContentEncoding:
case HttpHeaderNames.ContentLanguage:
case HttpHeaderNames.ContentLocation:
case HttpHeaderNames.ContentMD5:
case HttpHeaderNames.ContentRange:
case HttpHeaderNames.ContentType:
case HttpHeaderNames.Expires:
case HttpHeaderNames.LastModified:
return ResponseHeaderType.Content;

// All other headers belong to the response
default:
return ResponseHeaderType.Response;
}
}
// Not all headers are created equal in System.Net.Http.
// If a header is a "content" header, adding it to a HttpResponseMessage directly
// will cause an InvalidOperationException.
// The list of known headers with their respective "header types"
// is conveniently hidden in an internal class of System.Net.Http,
// because nobody outside the .NET team will ever need them, right?
// https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs
// Here are the "content" headers, extracted on 2019-07-06:
private static ResponseHeaderType GetResponseHeaderType(string name) => name switch {
// Content-Length is set automatically and shall not be touched
HttpHeaderNames.ContentLength => ResponseHeaderType.None,
// These headers belong to Content
HttpHeaderNames.Allow => ResponseHeaderType.Content,
HttpHeaderNames.ContentDisposition => ResponseHeaderType.Content,
HttpHeaderNames.ContentEncoding => ResponseHeaderType.Content,
HttpHeaderNames.ContentLanguage => ResponseHeaderType.Content,
HttpHeaderNames.ContentLocation => ResponseHeaderType.Content,
HttpHeaderNames.ContentMD5 => ResponseHeaderType.Content,
HttpHeaderNames.ContentRange => ResponseHeaderType.Content,
HttpHeaderNames.ContentType => ResponseHeaderType.Content,
HttpHeaderNames.Expires => ResponseHeaderType.Content,
HttpHeaderNames.LastModified => ResponseHeaderType.Content,
_ => ResponseHeaderType.Response
};
}
}
10 changes: 5 additions & 5 deletions src/EmbedIO.Testing/Internal/TestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public TestRequest(HttpRequestMessage clientRequest)

public bool HasEntityBody { get; }

public Stream InputStream => _content?.ReadAsStreamAsync().Await();
public Stream? InputStream => _content?.ReadAsStreamAsync().Await();

public Encoding ContentEncoding { get; }

Expand All @@ -95,19 +95,19 @@ public TestRequest(HttpRequestMessage clientRequest)

public bool IsSecureConnection => false;

public string UserAgent { get; }
public string? UserAgent { get; }

public bool IsWebSocketRequest => false;

public IPEndPoint LocalEndPoint { get; }

public string ContentType { get; }
public string? ContentType { get; }

public long ContentLength64 => 0;

public bool IsAuthenticated => false;

public Uri UrlReferrer => null;
public Uri? UrlReferrer => null;

private static HttpVerbs HttpMethodToVerb(HttpMethod method)
{
Expand Down Expand Up @@ -135,4 +135,4 @@ private static HttpVerbs HttpMethodToVerb(HttpMethod method)
return HttpVerbs.Any;
}
}
}
}
8 changes: 4 additions & 4 deletions src/EmbedIO.Testing/Internal/TestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class TestResponse : IHttpResponse, IDisposable

public long ContentLength64 { get; set; }

public string ContentType { get; set; }
public string? ContentType { get; set; }

public Stream OutputStream { get; } = new MemoryStream();

Expand All @@ -32,9 +32,9 @@ internal sealed class TestResponse : IHttpResponse, IDisposable

public Version ProtocolVersion { get; } = HttpVersion.Version11;

public byte[] Body { get; private set; }
public byte[]? Body { get; private set; }

public string StatusDescription { get; set; }
public string? StatusDescription { get; set; }

internal bool IsClosed { get; private set; }

Expand All @@ -54,7 +54,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public string GetBodyAsString()
public string? GetBodyAsString()
{
if (!(OutputStream is MemoryStream ms)) return null;

Expand Down
30 changes: 9 additions & 21 deletions src/EmbedIO.Testing/MockFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void Start(CancellationToken cancellationToken)
}

/// <inheritdoc />
public MappedResourceInfo MapUrlPath(string urlPath, IMimeTypeProvider mimeTypeProvider)
public MappedResourceInfo? MapUrlPath(string urlPath, IMimeTypeProvider mimeTypeProvider)
{
if (string.IsNullOrEmpty(urlPath))
return null;
Expand All @@ -99,9 +99,9 @@ public MappedResourceInfo MapUrlPath(string urlPath, IMimeTypeProvider mimeTypeP
}

/// <inheritdoc />
public Stream OpenFile(string path)
public Stream? OpenFile(string path)
{
var (name, entry) = FindEntry(path);
var (_, entry) = FindEntry(path);
return entry is MockFile file ? new MemoryStream(file.Data, false) : null;
}

Expand Down Expand Up @@ -189,24 +189,12 @@ private byte[] CreateRandomData(int length)
return default;
}

private MappedResourceInfo GetResourceInfo(string path, string name, MockDirectoryEntry entry, IMimeTypeProvider mimeTypeProvider)
{
switch (entry)
{
case MockFile file:
return MappedResourceInfo.ForFile(
path,
name,
file.LastModifiedUtc,
file.Data.Length,
mimeTypeProvider.GetMimeType(Path.GetExtension(name)));
case MockDirectory directory:
return MappedResourceInfo.ForDirectory(string.Empty, name, _root.LastModifiedUtc);
default:
return null;
}
}

private MappedResourceInfo? GetResourceInfo(string path, string name, MockDirectoryEntry entry, IMimeTypeProvider mimeTypeProvider) => entry switch {
MockFile file => MappedResourceInfo.ForFile(path, name, file.LastModifiedUtc, file.Data.Length, mimeTypeProvider.GetMimeType(Path.GetExtension(name))),
MockDirectory _ => MappedResourceInfo.ForDirectory(string.Empty, name, _root.LastModifiedUtc),
_ => null
};

private static string AppendNameToPath(string path, string name)
=> string.IsNullOrEmpty(path) ? name : $"{path}/{name}";
}
Expand Down
Loading

0 comments on commit 0093d4e

Please sign in to comment.