Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@HxPushUrl(true) now retreives the path from the request #129

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.lang.reflect.Method;
import java.time.Duration;

import java.util.Objects;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.HandlerMethod;
Expand Down Expand Up @@ -60,7 +62,7 @@ public boolean preHandle(HttpServletRequest request,
if (handler instanceof HandlerMethod) {
Method method = ((HandlerMethod) handler).getMethod();
setHxLocation(response, method);
setHxPushUrl(response, method);
setHxPushUrl(request, response, method);
setHxRedirect(response, method);
setHxReplaceUrl(response, method);
setHxReswap(response, method);
Expand Down Expand Up @@ -94,10 +96,19 @@ private void setHxLocation(HttpServletResponse response, Method method) {
}
}

private void setHxPushUrl(HttpServletResponse response, Method method) {
private void setHxPushUrl(HttpServletRequest request, HttpServletResponse response, Method method) {
HxPushUrl methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxPushUrl.class);
if (methodAnnotation != null) {
setHeader(response, HX_PUSH_URL, methodAnnotation.value());
if(!Objects.equals(methodAnnotation.value(), HtmxValue.TRUE)){
setHeader(response, HX_PUSH_URL, methodAnnotation.value());
}
String path = request.getRequestURI();
String queryString = request.getQueryString();

if (queryString != null && !queryString.isEmpty()) {
path += "?" + queryString;
}
setHeader(response, HX_PUSH_URL, path);
tschuehly marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,17 @@ public void testHxLocationWithoutContextData() throws Exception {
}

@Test
public void testHxPushUrl() throws Exception {
mockMvc.perform(get("/hx-push-url"))
public void testHxPushUrlPath() throws Exception {
mockMvc.perform(get("/hx-push-url-path"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Push-Url", "/path"));
}
@Test
public void testHxPushUrl() throws Exception {
mockMvc.perform(get("/hx-push-url?test=hello"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Push-Url", "/hx-push-url?test=hello"));
}

@Test
public void testHxRedirect() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,16 @@ public String hxLocationWithContextData() {
return "";
}

@GetMapping("/hx-push-url")
@GetMapping("/hx-push-url-path")
@HxPushUrl("/path")
@ResponseBody
public String hxPushUrlPath() {
return "";
}

@GetMapping("/hx-push-url")
@HxPushUrl
tschuehly marked this conversation as resolved.
Show resolved Hide resolved
@ResponseBody
public String hxPushUrl() {
return "";
}
Expand Down