Skip to content

Commit

Permalink
Merge pull request #42 from AlexZeitler/main
Browse files Browse the repository at this point in the history
Identify non boosted HTMX Requests
  • Loading branch information
khalidabuhakmeh authored Feb 9, 2024
2 parents 5ef50e7 + 994dff0 commit bafadce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Htmx/HtmxHttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ public static bool IsHtmxBoosted(this HttpRequest? request)
{
return request?.Headers.GetValueOrDefault(HtmxRequestHeaders.Keys.Boosted, false) is true;
}

/// <summary>
/// true if the request is an HTMX Request that is not HTMX Boosted
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool IsHtmxNonBoosted(this HttpRequest? request)
{
return request?.IsHtmx() is true && !request.IsHtmxBoosted();
}

/// <summary>
/// true if the request is an HTMX Request that is not HTMX Boosted
/// </summary>
/// <param name="request">The HTTP Request</param>
/// <param name="values">All the potential Htmx Header Values</param>
/// <returns></returns>
public static bool IsHtmxNonBoosted(this HttpRequest? request, out HtmxRequestHeaders? values)
{
var isHtmx = request.IsHtmxNonBoosted();
values = request is not null && isHtmx ? new HtmxRequestHeaders(request) : null;
return isHtmx;
}

/// <summary>
/// Set the Htmx Response Headers
Expand Down
33 changes: 33 additions & 0 deletions test/Htmx.Tests/HtmxHttpRequestExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,37 @@ public void Can_determine_request_is_boosted()
{
Assert.True(Request.IsHtmxBoosted());
}

[Fact]
public void Can_determine_request_is_htmx_non_boosted()
{
var request = new FakeHttpRequest();
request.Headers.Add(HtmxRequestHeaders.Keys.Request, "true");

Assert.True(request.IsHtmxNonBoosted());
}

[Fact]
public void Can_get_values_when_request_is_htmx_non_boosted()
{
var request = new FakeHttpRequest();
request.Headers.Add(HtmxRequestHeaders.Keys.Request, "true");
request.Headers.Add(HtmxRequestHeaders.Keys.HistoryRestoreRequest, "true");
request.Headers.Add(HtmxRequestHeaders.Keys.Trigger, "trigger");
request.Headers.Add(HtmxRequestHeaders.Keys.Prompt, "prompt");
request.Headers.Add(HtmxRequestHeaders.Keys.CurrentUrl, "/");
request.Headers.Add(HtmxRequestHeaders.Keys.Target, "123");
request.Headers.Add(HtmxRequestHeaders.Keys.TriggerName, "trigger-name");
request.Headers.Add(HtmxRequestHeaders.Keys.Boosted, "false");

var isHtmxNonBoosted = request.IsHtmxNonBoosted(out var values);
Assert.True(isHtmxNonBoosted);
Assert.NotNull(values);
Assert.True(values.HistoryRestoreRequest);
Assert.Equal("trigger", values.Trigger);
Assert.Equal("prompt", values.Prompt);
Assert.Equal("/", values.CurrentUrl);
Assert.Equal("123", values.Target);
Assert.Equal("trigger-name", values.TriggerName);
}
}

0 comments on commit bafadce

Please sign in to comment.