Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit f2260d5

Browse files
simplified options requests
1 parent a918180 commit f2260d5

16 files changed

Lines changed: 68 additions & 104 deletions

src/SqlStreamStore.HAL.Tests/OptionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static IEnumerable<object[]> OptionsAllowedMethodCases()
4343
yield return new object[]
4444
{
4545
"/streams/a-stream/0",
46-
new[] { HttpMethod.Get, HttpMethod.Head, HttpMethod.Options }
46+
new[] { HttpMethod.Get, HttpMethod.Head, HttpMethod.Delete, HttpMethod.Options }
4747
};
4848
}
4949

src/SqlStreamStore.HAL/AllStream/AllStreamResource.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using System;
44
using System.Linq;
5-
using System.Net.Http;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using Halcyon.HAL;
@@ -13,13 +12,6 @@ internal class AllStreamResource : IResource
1312
private readonly IStreamStore _streamStore;
1413
private readonly bool _useCanonicalUrls;
1514

16-
public HttpMethod[] Allowed { get; } =
17-
{
18-
HttpMethod.Get,
19-
HttpMethod.Head,
20-
HttpMethod.Options
21-
};
22-
2315
public AllStreamResource(IStreamStore streamStore, bool useCanonicalUrls)
2416
{
2517
if(streamStore == null)

src/SqlStreamStore.HAL/AllStream/ReadAllStreamMiddleware.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ public static IApplicationBuilder UseAllStream(
1919

2020
return builder
2121
.MapWhen(IsGet, inner => inner.Use(GetStream(allStream)))
22-
.MapWhen(IsOptions, inner => inner.UseOptions(allStream))
2322
.UseAllowedMethods(allStream);
2423
}
2524

2625
private static bool IsGet(HttpContext context) => context.IsGetOrHead();
2726

28-
private static bool IsOptions(HttpContext context) => context.IsOptions();
29-
3027
private static MidFunc GetStream(AllStreamResource allStream) => async (context, next) =>
3128
{
3229
var options = new ReadAllStreamOperation(context.Request);

src/SqlStreamStore.HAL/AllStreamMessage/AllStreamMessageResource.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace SqlStreamStore.HAL.AllStreamMessage
22
{
33
using System;
4-
using System.Net.Http;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Halcyon.HAL;
@@ -12,13 +11,6 @@ internal class AllStreamMessageResource : IResource
1211
{
1312
private readonly IStreamStore _streamStore;
1413

15-
public HttpMethod[] Allowed { get; } =
16-
{
17-
HttpMethod.Get,
18-
HttpMethod.Head,
19-
HttpMethod.Options
20-
};
21-
2214
public AllStreamMessageResource(IStreamStore streamStore)
2315
{
2416
if(streamStore == null)

src/SqlStreamStore.HAL/AllStreamMessage/ReadAllStreamMessageMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static IApplicationBuilder UseAllStreamMessage(
1818

1919
return builder
2020
.MapWhen(HttpMethod.Get, inner => inner.Use(GetStreamMessage(allStreamMessages)))
21-
.MapWhen(HttpMethod.Options, inner => inner.UseOptions(allStreamMessages))
2221
.UseAllowedMethods(allStreamMessages);
2322
}
2423

src/SqlStreamStore.HAL/ApplicationBuilderExtensions.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace SqlStreamStore.HAL
55
using System.Net.Http;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Http;
89
using MidFunc = System.Func<
910
Microsoft.AspNetCore.Http.HttpContext,
1011
System.Func<System.Threading.Tasks.Task>,
@@ -23,17 +24,33 @@ public static IApplicationBuilder MapWhen(
2324
StringComparison.OrdinalIgnoreCase),
2425
configure);
2526

26-
public static IApplicationBuilder UseAllowedMethods(this IApplicationBuilder builder, IResource resource)
27-
=> builder.Use((context, next) =>
27+
public static IApplicationBuilder UseAllowedMethods<TResource>(this IApplicationBuilder builder, TResource resource)
28+
where TResource : IResource
29+
{
30+
var allowed = ResourceMethods.Discover<TResource>();
31+
32+
Task AllowedMethods(HttpContext context, Func<Task> next)
2833
{
29-
if(!resource.Allowed.Contains(new HttpMethod(context.Request.Method)))
34+
if(!allowed.Contains(new HttpMethod(context.Request.Method)))
3035
{
3136
context.Response.StatusCode = 405;
3237
return Task.CompletedTask;
3338
}
3439

3540
return next();
36-
});
41+
}
42+
43+
44+
return builder
45+
.Use(Options(allowed))
46+
.Use(AllowedMethods);
47+
}
48+
49+
private static MidFunc Options(HttpMethod[] allowed) => (context, next) =>
50+
{
51+
context.SetStandardCorsHeaders(allowed);
3752

53+
return Task.CompletedTask;
54+
};
3855
}
3956
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
namespace SqlStreamStore.HAL
22
{
3-
using System.Net.Http;
4-
53
internal interface IResource
6-
{
7-
HttpMethod[] Allowed { get; }
8-
}
4+
{ }
95
}
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace SqlStreamStore.HAL.Index
22
{
3-
using Halcyon.HAL;
3+
using System;
4+
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Http;
67
using MidFunc = System.Func<
@@ -17,13 +18,9 @@ public static IApplicationBuilder UseIndex(this IApplicationBuilder builder)
1718

1819
return builder
1920
.MapWhen(IsGetIndex, inner => inner.Use(Index()))
20-
.MapWhen(IsOptions, inner => inner.UseOptions(index))
2121
.MapWhen(IsIndex, inner => inner.UseAllowedMethods(index));
2222
}
2323

24-
private static bool IsOptions(HttpContext context)
25-
=> context.IsOptions() && context.Request.Path.IsIndex();
26-
2724
private static bool IsGetIndex(HttpContext context)
2825
=> context.IsGetOrHead() && context.Request.Path.IsIndex();
2926

@@ -32,15 +29,13 @@ private static bool IsIndex(HttpContext context)
3229

3330
private static MidFunc Index()
3431
{
35-
var response = new Response(new HALResponse(null)
36-
.AddLinks(
37-
TheLinks
38-
.RootedAt(string.Empty)
39-
.Index().Self()
40-
.Find()
41-
.Add(Constants.Relations.Feed, "stream")));
32+
var resource = new IndexResource();
33+
34+
var response = resource.Get();
35+
36+
Task Index(HttpContext context, Func<Task> next) => context.WriteResponse(response);
4237

43-
return (context, next) => context.WriteResponse(response);
38+
return Index;
4439
}
4540
}
4641
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace SqlStreamStore.HAL.Index
22
{
3-
using System.Net.Http;
3+
using Halcyon.HAL;
44

55
internal class IndexResource : IResource
66
{
7-
public HttpMethod[] Allowed { get; } =
8-
{
9-
HttpMethod.Get,
10-
HttpMethod.Head,
11-
HttpMethod.Options
12-
};
7+
public Response Get() => new Response(new HALResponse(null)
8+
.AddLinks(
9+
TheLinks
10+
.RootedAt(string.Empty)
11+
.Index().Self()
12+
.Find()
13+
.Add(Constants.Relations.Feed, "stream")));
1314
}
1415
}

src/SqlStreamStore.HAL/OptionsMiddleware.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)