Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Octokit.Webhooks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore", "samples\AspNe
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{174C8B3B-E8D3-4845-AE7C-8C0DEC43354F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Octokit.Webhooks.TestUtils", "test\Octokit.Webhooks.TestUtils\Octokit.Webhooks.TestUtils.csproj", "{48EB4752-1A23-4DD2-AF86-21ED12475F8B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -86,6 +88,10 @@ Global
{2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Release|Any CPU.Build.0 = Release|Any CPU
{48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -98,6 +104,7 @@ Global
{E46C1DB5-F21C-46A9-8D17-F08278BC0BF8} = {719809C2-A551-4C4A-9EFD-B10FB5E35BC0}
{566DF0E2-1288-4083-9B55-4C8B69BB1432} = {EFE1E5ED-D337-4874-82EC-D9FA0BC7D3AB}
{2EE1581B-E8B6-409E-8879-6F1EFC8C682B} = {174C8B3B-E8D3-4845-AE7C-8C0DEC43354F}
{48EB4752-1A23-4DD2-AF86-21ED12475F8B} = {E1B24F25-B8A4-46EE-B7EB-7803DCFC543F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {73F36209-F8D6-4066-8951-D97729F773CF}
Expand Down
1 change: 1 addition & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageReference Include="coverlet.collector" PrivateAssets="all" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.3.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0"/>
<PackageReference Include="Moq" Version="4.16.1"/>
<PackageReference Include="xunit" Version="2.4.1"/>
Expand Down
5 changes: 1 addition & 4 deletions test/Octokit.Webhooks.Test/Octokit.Webhooks.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

<ItemGroup Label="Project References">
<ProjectReference Include="..\..\src\Octokit.Webhooks\Octokit.Webhooks.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Resources" />
<ProjectReference Include="..\Octokit.Webhooks.TestUtils\Octokit.Webhooks.TestUtils.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions test/Octokit.Webhooks.Test/TestWebhookEventProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Octokit.Webhooks.Test
{
public class TestWebhookEventProcessor : WebhookEventProcessor
{
}
}
22 changes: 22 additions & 0 deletions test/Octokit.Webhooks.Test/WebhookEventProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Octokit.Webhooks.Test
{
using System;
using FluentAssertions;
using Xunit;

public class WebhookEventProcessorTests
{
private readonly WebhookEventProcessor webhookEventProcessor;

public WebhookEventProcessorTests() => this.webhookEventProcessor = new TestWebhookEventProcessor();

[Theory]
[ClassData(typeof(WebhookEventProcessorTestsData))]
public void CanDeserialize(WebhookHeaders headers, string payload, Type expectedType)
{
var result = this.webhookEventProcessor.DeserializeWebhookEvent(headers, payload);

result.Should().BeAssignableTo(expectedType);
}
}
}
36 changes: 36 additions & 0 deletions test/Octokit.Webhooks.Test/WebhookEventProcessorTestsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Octokit.Webhooks.Test
{
using System.Collections;
using System.Collections.Generic;
using System.IO;
using CaseExtensions;
using Octokit.Webhooks.TestUtils;

public class WebhookEventProcessorTestsData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
var resourcesDirectory = ResourceUtils.GetResources();
var files = Directory.GetFiles(resourcesDirectory, "*.json", SearchOption.AllDirectories);
foreach (var file in files)
{
var relativeResource = file.Replace($"{resourcesDirectory}{Path.DirectorySeparatorChar}", string.Empty);
var parts = relativeResource.Split(Path.DirectorySeparatorChar);
var headers = new WebhookHeaders
{
Event = parts[0],
};
var expectedType = ClassUtils.GetEventTypeByName(parts[0].ToPascalCase());
var content = ResourceUtils.ReadResource(relativeResource);
yield return new object[]
{
headers,
content,
expectedType,
};
}
}

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}
}
15 changes: 15 additions & 0 deletions test/Octokit.Webhooks.TestUtils/ClassUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Octokit.Webhooks.TestUtils
{
using System;
using System.Linq;
using System.Reflection;

public static class ClassUtils
{
public static Type GetEventTypeByName(string name) => Assembly.Load("Octokit.Webhooks")
.GetTypes()
.Single(t =>
t.FullName!.StartsWith("Octokit.Webhooks.Events.", StringComparison.InvariantCulture) &&
t.Name == $"{name}Event");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Build">
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions test/Octokit.Webhooks.TestUtils/ResourceUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Octokit.Webhooks.TestUtils
{
using System;
using System.IO;
using System.Reflection;

public static class ResourceUtils
{
public static string GetResources()
{
var baseUrl = new Uri(Assembly.GetExecutingAssembly().Location);
var basePath = Uri.UnescapeDataString(baseUrl.AbsolutePath);
var dirPath = Path.GetDirectoryName(basePath);
return Path.Combine(dirPath!, "Resources");
}

public static string ReadResource(string relativePath) => File.ReadAllText(GetResourcePath(relativePath));

private static string GetResourcePath(string relativePath) => Path.Combine(GetResources(), relativePath);
}
}