mirror of https://github.com/Minestom/Minestom.git
Fix exception on invalid username or UUID (#532)
This commit is contained in:
parent
549a9a9b52
commit
62b9e80d46
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue