Skip to content

Commit

Permalink
Resolve authenticator metadata (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpassing authored Mar 23, 2024
1 parent fc84836 commit 5ade4ce
Show file tree
Hide file tree
Showing 20 changed files with 1,980 additions and 174 deletions.
3 changes: 2 additions & 1 deletion sources/Jpki.NUnit/AssertThrows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// under the License.
//

using NUnit.Framework;
using System;
using System.Reflection;

Expand Down Expand Up @@ -51,7 +50,9 @@ private static Exception Unwrap(this Exception e)
{
try
{
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
code().Wait();
#pragma warning restore VSTHRD002
}
catch (AggregateException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Jpki.Powershell.Runtime.Http;
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -36,9 +37,9 @@
namespace Jpki.Powershell.Test.Runtime.Http
{
[TestFixture]
public class TestRestClient
public class TestJsonResource
{
private static readonly Uri SampleRestUrl =
private static readonly Uri SampleUrl =
new Uri("https://accounts.google.com/.well-known/openid-configuration");

private static readonly Uri NotFoundUrl =
Expand All @@ -47,82 +48,54 @@ public class TestRestClient
private static readonly Uri NoContentUrl =
new Uri("https://gstatic.com/generate_204");

private static readonly UserAgent userAgent = new UserAgent(
"test",
new Version(1, 0),
Environment.OSVersion.VersionString);

public class SampleResource
public class Body
{
[JsonPropertyName("issuer")]
public string? Issuer { get; set; }
}

//---------------------------------------------------------------------
// GetString.
// Get.
//---------------------------------------------------------------------

[Test]
public async Task WhenUrlPointsToNoContent_ThenGetStringReturnsEmptyString()
public async Task GetContent()
{
var client = new RestClient();
var result = await client
.GetStringAsync(
NoContentUrl,
CancellationToken.None)
var response = await client
.Resource<JsonResource<Body>>(SampleUrl)
.Get()
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);

AssertThat.AreEqual(string.Empty, result);
}

[Test]
public void WhenUrlNotFound_ThenGetStringThrowsException()
{
var client = new RestClient();
AssertThrows.AggregateException<HttpRequestException>(
() => client.GetStringAsync(
NotFoundUrl,
CancellationToken.None));
AssertThat.AreEqual(HttpStatusCode.OK, response.StatusCode);
AssertThat.IsNotNull(response.Body?.Issuer);
}

//---------------------------------------------------------------------
// GetJson.
//---------------------------------------------------------------------

[Test]
public async Task WhenUrlPointsToJson_ThenGetJsonReturnsObject()
public async Task GetNoContent()
{
var client = new RestClient();
var result = await client
.GetJsonAsync<SampleResource>(
SampleRestUrl,
CancellationToken.None)
var response = await client
.Resource<JsonResource<Body>>(NoContentUrl)
.Get()
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);

AssertThat.IsNotNull(result?.Issuer);
AssertThat.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
AssertThat.IsNull(response.Body);
}

[Test]
public async Task WhenUrlPointsToNoContent_ThenGetJsonReturnsNull()
public void GetNotFound()
{
var client = new RestClient();
var result = await client
.GetJsonAsync<SampleResource>(
NoContentUrl,
CancellationToken.None)
.ConfigureAwait(false);

AssertThat.IsNull(result);
}

[Test]
public void WhenUrlNotFound_ThenGetJsonThrowsException()
{
var client = new RestClient();
AssertThrows.AggregateException<HttpRequestException>(
() => client.GetJsonAsync<SampleResource>(
NotFoundUrl,
CancellationToken.None));
() => client
.Resource<JsonResource<Body>>(NotFoundUrl)
.Get()
.ExecuteAsync(CancellationToken.None));
}
}
}
Expand Down
105 changes: 105 additions & 0 deletions sources/Jpki.Powershell.Test/Runtime/Http/TestTextResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// Copyright 2024 Johannes Passing
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

using Jpki.Powershell.Runtime.Http;
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Jpki.Powershell.Test.Runtime.Http
{
[TestFixture]
public class TestHtmlResource
{
private static readonly Uri SampleUrl =
new Uri("https://accounts.google.com/");

private static readonly Uri NotFoundUrl =
new Uri("https://gstatic.com/generate_404");

private static readonly Uri NoContentUrl =
new Uri("https://gstatic.com/generate_204");

private class HtmlResource : TextResource
{
public override string ExpectedContentType => "text/html";
}

//---------------------------------------------------------------------
// Get.
//---------------------------------------------------------------------

[Test]
public async Task GetContent()
{
var client = new RestClient();
var response = await client
.Resource<HtmlResource>(SampleUrl)
.Get()
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);

AssertThat.AreEqual(HttpStatusCode.OK, response.StatusCode);
AssertThat.IsNotNull(response.Body);
}

[Test]
public async Task GetNoContent()
{
var client = new RestClient();
var response = await client
.Resource<HtmlResource>(NoContentUrl)
.Get()
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);

AssertThat.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
AssertThat.AreEqual(string.Empty, response.Body);
}

[Test]
public void GetUnexpectedContentType()
{
var client = new RestClient();

AssertThrows.AggregateException<UnexpectedContentTypeException>(
() => client
.Resource<TextResource>(SampleUrl)
.Get()
.ExecuteAsync(CancellationToken.None));
}

[Test]
public void GetNotFound()
{
var client = new RestClient();

AssertThrows.AggregateException<HttpRequestException>(
() => client
.Resource<HtmlResource>(NotFoundUrl)
.Get()
.ExecuteAsync(CancellationToken.None));
}
}
}
44 changes: 44 additions & 0 deletions sources/Jpki.Powershell.Test/Runtime/TestLinqExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Copyright 2024 Johannes Passing
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

using Jpki.Powershell.Runtime;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;

namespace Jpki.Powershell.Test.Runtime
{
[TestFixture]
public class TestLinqExtensions
{
//---------------------------------------------------------------------
// EnsureNotNull.
//---------------------------------------------------------------------

[Test]
public void WhenEnumIsNull_EnsureNotNullReturnsEmpty()
{
IEnumerable<string>? e = null;
AssertThat.IsNotNull(e.EnsureNotNull());
AssertThat.AreEqual(0, e.EnsureNotNull().Count());
}
}
}
Loading

0 comments on commit 5ade4ce

Please sign in to comment.