Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -35,7 +35,8 @@ public class RandomUtil {
*/
public static int randomInt(final int min, final int max) {
ThreadLocalRandom random = ThreadLocalRandom.current();
return random.nextInt(max - min + 1) + min;
// Widen the exclusive upper bound to preserve inclusive ranges across integer overflow.
return (int) random.nextLong(min, (long) max + 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -45,6 +46,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -147,6 +149,24 @@ public void testDoExecuteWithoutCachedHandle() {
verify(chain).execute(any());
}

@ParameterizedTest
@CsvSource({"0, 2147483647", "2147483647, 2147483647", "0, 0", "10, 15"})
public void testIntegerResponseSupportsInclusiveBounds(final int min, final int max) {
RuleData ruleData = buildRuleData();
MockHandle mockHandle = new MockHandle();
mockHandle.setResponseContent(String.format("${int|%d-%d}", min, max));
MockPluginHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), mockHandle);

StepVerifier.create(mockPlugin.doExecute(exchange, chain, mock(SelectorData.class), ruleData)).verifyComplete();

String body = exchange.getResponse().getBodyAsString().block();
int value = Integer.parseInt(body);
assertTrue(value >= min && value <= max);
assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, exchange.getResponse().getHeaders().getContentLength());
verify(chain, never()).execute(any());
}

@Test
public void testGetOrder() {
final int result = mockPlugin.getOrder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,41 @@
package org.apache.shenyu.plugin.mock.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test case for {@link MockUtil}.
*/
public final class MockUtilTest {

@ParameterizedTest
@CsvSource({
"0, 2147483647",
"-2147483648, 2147483647",
"-2147483648, 0",
"-2147483648, -1",
"2147483647, 2147483647",
"-2147483648, -2147483648",
"0, 0",
"10, 15"
})
public void randomIntShouldSupportInclusiveIntegerBounds(final int min, final int max) {
for (int i = 0; i < 100; i++) {
int value = MockUtil.randomInt(min, max);
assertTrue(value >= min && value <= max, "Generated value must remain within the inclusive bounds");
}
}

@ParameterizedTest
@CsvSource({"1, 0", "2147483647, -2147483648"})
public void randomIntShouldRejectReversedBounds(final int min, final int max) {
assertThrows(IllegalArgumentException.class, () -> MockUtil.randomInt(min, max));
}

@Test
public void zhShouldReturnLengthsWithinTheInclusiveRange() {
for (int i = 0; i < 500; i++) {
Expand Down
Loading