-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict mapping to the handler method to target and triggered elemen…
…t in @HxRequest
- Loading branch information
Showing
4 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequestMappingHandlerMappingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.github.wimdeblauwe.htmx.spring.boot.mvc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxRequestHeader.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(HtmxRequestMappingHandlerMappingTestController.class) | ||
@WithMockUser | ||
public class HtmxRequestMappingHandlerMappingTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private TestService service; | ||
|
||
@Test | ||
void testHxRequestWithTargetBar() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-target") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TARGET.getValue(), "bar")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("bar")); | ||
} | ||
|
||
@Test | ||
void testHxRequestWithTargetFoo() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-target") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TARGET.getValue(), "foo")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("foo")); | ||
} | ||
|
||
@Test | ||
void testHxRequestWithTriggerIdBar() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-trigger-id") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TRIGGER.getValue(), "bar")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("bar")); | ||
} | ||
|
||
@Test | ||
void testHxRequestWithTriggerIdFoo() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-trigger-id") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TRIGGER.getValue(), "foo")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("foo")); | ||
} | ||
|
||
@Test | ||
void testHxRequestWithTriggerNameBar() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-trigger-name") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TRIGGER_NAME.getValue(), "bar")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("bar")); | ||
} | ||
|
||
@Test | ||
void testHxRequestWithTriggerNameFoo() throws Exception { | ||
mockMvc.perform(get("/hx-request-with-trigger-name") | ||
.header(HX_REQUEST.getValue(), "true") | ||
.header(HX_TRIGGER_NAME.getValue(), "foo")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("foo")); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...thub/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequestMappingHandlerMappingTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.wimdeblauwe.htmx.spring.boot.mvc; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@Controller | ||
public class HtmxRequestMappingHandlerMappingTestController { | ||
|
||
@HxRequest(target = "bar") | ||
@GetMapping("/hx-request-with-target") | ||
@ResponseBody | ||
public String hxRequestWithTargetBar() { | ||
return "bar"; | ||
} | ||
|
||
@HxRequest(target = "foo") | ||
@GetMapping("/hx-request-with-target") | ||
@ResponseBody | ||
public String hxRequestWithTargetFoo() { | ||
return "foo"; | ||
} | ||
|
||
@HxRequest(triggerId = "bar") | ||
@GetMapping("/hx-request-with-trigger-id") | ||
@ResponseBody | ||
public String hxRequestWithTriggerIdBar() { | ||
return "bar"; | ||
} | ||
|
||
@HxRequest(triggerId = "foo") | ||
@GetMapping("/hx-request-with-trigger-id") | ||
@ResponseBody | ||
public String hxRequestWithTriggerIdFoo() { | ||
return "foo"; | ||
} | ||
|
||
@HxRequest(triggerName = "bar") | ||
@GetMapping("/hx-request-with-trigger-name") | ||
@ResponseBody | ||
public String hxRequestWithTriggerNameBar() { | ||
return "bar"; | ||
} | ||
|
||
@HxRequest(triggerName = "foo") | ||
@GetMapping("/hx-request-with-trigger-name") | ||
@ResponseBody | ||
public String hxRequestWithTriggerNameFoo() { | ||
return "foo"; | ||
} | ||
|
||
} |