|
| 1 | +package io.specto.hoverfly.ruletest; |
| 2 | + |
| 3 | +import io.specto.hoverfly.junit.core.SimulationSource; |
| 4 | +import io.specto.hoverfly.junit.dsl.HoverflyDsl; |
| 5 | +import io.specto.hoverfly.junit.dsl.HttpBodyConverter; |
| 6 | +import io.specto.hoverfly.junit.dsl.ResponseCreators; |
| 7 | +import io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers; |
| 8 | +import io.specto.hoverfly.junit.rule.HoverflyRule; |
| 9 | +import org.assertj.core.api.Assertions; |
| 10 | +import org.junit.ClassRule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.springframework.http.HttpEntity; |
| 13 | +import org.springframework.http.HttpMethod; |
| 14 | +import org.springframework.web.client.RestTemplate; |
| 15 | + |
| 16 | +public class HoverflyMatchingPriorityTest { |
| 17 | + |
| 18 | + @ClassRule |
| 19 | + public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(); |
| 20 | + |
| 21 | + @Test |
| 22 | + public void shouldPassWhenSpecificBodyMatcherDeclaredFirst() { |
| 23 | + hoverflyRule.simulate(SimulationSource.dsl( |
| 24 | + HoverflyDsl.service("https://test.com") |
| 25 | + .post("/sameUrl") |
| 26 | + .body(HoverflyMatchers.matchesPartialJson(HttpBodyConverter.jsonWithSingleQuotes("{'attr':'value'}"))) |
| 27 | + .willReturn(ResponseCreators.success()) |
| 28 | + .post("/sameUrl") |
| 29 | + .anyBody() |
| 30 | + .willReturn(ResponseCreators.badRequest()) |
| 31 | + )); |
| 32 | + Assertions.assertThat(new RestTemplate().exchange("https://test.com/sameUrl", HttpMethod.POST, new HttpEntity<>(" {\n" |
| 33 | + + " \"test\": 1,\n" |
| 34 | + + " \"attr\": \"value\",\n" |
| 35 | + + " \"list\": [ \"A\", \"B\" ]\n" |
| 36 | + + " }"), String.class)) |
| 37 | + .isNotNull(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldPassWhenSpecificBodyMatcherDeclaredLast() { |
| 42 | + hoverflyRule.simulate(SimulationSource.dsl( |
| 43 | + HoverflyDsl.service("https://test.com") |
| 44 | + .post("/sameUrl") |
| 45 | + .anyBody() |
| 46 | + .willReturn(ResponseCreators.badRequest()) |
| 47 | + .post("/sameUrl") |
| 48 | + .body(HoverflyMatchers.matchesPartialJson(HttpBodyConverter.jsonWithSingleQuotes("{'attr':'value'}"))) |
| 49 | + .willReturn(ResponseCreators.success()) |
| 50 | + |
| 51 | + )); |
| 52 | + Assertions.assertThat(new RestTemplate().exchange("https://test.com/sameUrl", HttpMethod.POST, new HttpEntity<>(" {\n" |
| 53 | + + " \"test\": 1,\n" |
| 54 | + + " \"attr\": \"value\",\n" |
| 55 | + + " \"list\": [ \"A\", \"B\" ]\n" |
| 56 | + + " }"), String.class)) |
| 57 | + .isNotNull(); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments