Fix exception on invalid username or UUID (#532)

This commit is contained in:
GreatWyrm 2021-11-15 13:23:20 -05:00 committed by GitHub
parent 549a9a9b52
commit 62b9e80d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -39,6 +39,11 @@ public final class MojangUtils {
try {
// Retrieve from the rate-limited Mojang API
final String response = URLUtils.getText(url);
// If our response is "", that means the url did not get a proper object from the url
// So the username or UUID was invalid, and therefore we return null
if(response.isEmpty()) {
return null;
}
return JsonParser.parseString(response).getAsJsonObject();
} catch (IOException e) {
MinecraftServer.getExceptionManager().handleException(e);

View File

@ -0,0 +1,21 @@
package misc;
import net.minestom.server.utils.mojang.MojangUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TestMojangUtils {
@Test
public void testValidNameWorks() {
var result = MojangUtils.fromUsername("jeb_");
assertNotNull(result);
assertEquals("jeb_", result.get("name").getAsString());
}
@Test
public void testInvalidNameReturnsNull() {
var result = MojangUtils.fromUsername("jfdsa84vvcxadubasdfcvn"); // Longer than 16, always invalid
assert result == null;
}
}