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

Commit bde4531

Browse files
adding last position in header if we're on the head link
1 parent 74fb4ac commit bde4531

5 files changed

Lines changed: 75 additions & 11 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace SqlStreamStore.HAL.Tests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using Shouldly;
8+
using Xunit;
9+
10+
public class HeadersTests : IDisposable
11+
{
12+
private const string StreamId = "a-stream";
13+
private readonly SqlStreamStoreHalMiddlewareFixture _fixture;
14+
15+
public HeadersTests()
16+
{
17+
_fixture = new SqlStreamStoreHalMiddlewareFixture();
18+
}
19+
20+
public void Dispose() => _fixture.Dispose();
21+
22+
public static IEnumerable<object[]> Methods()
23+
{
24+
yield return new object[] { HttpMethod.Head };
25+
yield return new object[] { HttpMethod.Get };
26+
}
27+
28+
[Theory, MemberData(nameof(Methods))]
29+
public async Task all_stream_head_link(HttpMethod method)
30+
{
31+
await _fixture.WriteNMessages(StreamId, 10);
32+
33+
var position = await _fixture.StreamStore.ReadHeadPosition();
34+
35+
using(var response = await _fixture.HttpClient.SendAsync(new HttpRequestMessage(method, "/stream")))
36+
{
37+
response.IsSuccessStatusCode.ShouldBeTrue();
38+
39+
response.Headers.GetValues(Constants.Headers.HeadPosition)
40+
.ShouldBe(new[] { $"{position}" });
41+
}
42+
}
43+
}
44+
}

src/SqlStreamStore.HAL/AllStreamResource.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<Response> GetPage(
3535
: Task.FromResult<string>(null))
3636
.ToArray());
3737

38-
return new Response(
38+
var response = new Response(
3939
new HALResponse(new object())
4040
.AddLinks(Links.SelfFeed(options))
4141
.AddLinks(Links.Navigation(page, options.Self))
@@ -54,6 +54,17 @@ public async Task<Response> GetPage(
5454
payload
5555
}).AddLinks(
5656
Links.Self(message)))));
57+
58+
if(options.FromPositionInclusive == Position.End)
59+
{
60+
var headPosition = page.Messages.Length > 0
61+
? page.Messages[0].Position
62+
: Position.End;
63+
64+
response.Headers[Constants.Headers.HeadPosition] = new[] { $"{headPosition}" };
65+
}
66+
67+
return response;
5768
}
5869

5970
public async Task<Response> GetMessage(

src/SqlStreamStore.HAL/Constants.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ internal static class Constants
88
public static class Headers
99
{
1010
public const string ExpectedVersion = "SSS-ExpectedVersion";
11-
11+
public const string HeadPosition = "SSS-HeadPosition";
12+
1213
public static class ContentTypes
1314
{
1415
public const string ProblemDetails = "application/problem+json";
@@ -27,5 +28,11 @@ public static class ContentTypes
2728
[405] = "Method Not Allowed",
2829
[409] = "Conflict"
2930
});
31+
32+
public static class ReadDirection
33+
{
34+
public const int Forwards = 1;
35+
public const int Backwards = -1;
36+
}
3037
}
3138
}

src/SqlStreamStore.HAL/ReadAllStreamOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public ReadAllStreamOptions(IOwinRequest request)
1616
EmbedPayload = request.Query.Get("e") != null;
1717

1818
ReadDirection = request.Query.Get("d") == "f"
19-
? 1
20-
: -1;
19+
? Constants.ReadDirection.Forwards
20+
: Constants.ReadDirection.Backwards;
2121

2222
if(!long.TryParse(request.Query.Get("p"), out _fromPositionInclusive))
2323
{
@@ -35,12 +35,12 @@ public ReadAllStreamOptions(IOwinRequest request)
3535
public bool EmbedPayload { get; }
3636
public int ReadDirection { get; }
3737

38-
public string Self => ReadDirection > 0
38+
public string Self => ReadDirection == Constants.ReadDirection.Forwards
3939
? LinkFormatter.FormatForwardLink("stream", MaxCount, FromPositionInclusive)
4040
: LinkFormatter.FormatBackwardLink("stream", MaxCount, FromPositionInclusive);
4141

4242
public Func<IReadonlyStreamStore, CancellationToken, Task<ReadAllPage>> GetReadOperation()
43-
=> (streamStore, ct) => ReadDirection > 0
43+
=> (streamStore, ct) => ReadDirection == Constants.ReadDirection.Forwards
4444
? streamStore.ReadAllForwards(_fromPositionInclusive, _maxCount, EmbedPayload, ct)
4545
: streamStore.ReadAllBackwards(_fromPositionInclusive, _maxCount, EmbedPayload, ct);
4646
}

src/SqlStreamStore.HAL/ReadStreamOptions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public ReadStreamOptions(IOwinRequest request)
1818
EmbedPayload = request.Query.Get("e") != null;
1919

2020
ReadDirection = request.Query.Get("d") == "f"
21-
? 1
22-
: -1;
21+
? Constants.ReadDirection.Forwards
22+
: Constants.ReadDirection.Backwards;
2323

2424
if(!int.TryParse(request.Query.Get("p"), out _fromVersionInclusive))
2525
{
26-
_fromVersionInclusive = ReadDirection > 0 ? StreamVersion.Start : StreamVersion.End;
26+
_fromVersionInclusive = ReadDirection == Constants.ReadDirection.Forwards
27+
? StreamVersion.Start
28+
: StreamVersion.End;
2729
}
2830

2931
if(!int.TryParse(request.Query.Get("m"), out _maxCount))
@@ -38,12 +40,12 @@ public ReadStreamOptions(IOwinRequest request)
3840
public int ReadDirection { get; }
3941
public string StreamId { get; }
4042

41-
public string Self => ReadDirection > 0
43+
public string Self => ReadDirection == Constants.ReadDirection.Forwards
4244
? LinkFormatter.FormatForwardLink(StreamId, MaxCount, FromVersionInclusive)
4345
: LinkFormatter.FormatBackwardLink(StreamId, MaxCount, FromVersionInclusive);
4446

4547
public Func<IReadonlyStreamStore, CancellationToken, Task<ReadStreamPage>> GetReadOperation()
46-
=> (streamStore, ct) => ReadDirection > 0
48+
=> (streamStore, ct) => ReadDirection == Constants.ReadDirection.Forwards
4749
? streamStore.ReadStreamForwards(StreamId, _fromVersionInclusive, _maxCount, EmbedPayload, ct)
4850
: streamStore.ReadStreamBackwards(StreamId, _fromVersionInclusive, _maxCount, EmbedPayload, ct);
4951
}

0 commit comments

Comments
 (0)