Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210911] (Java) Url Validation #210

Open
JuHyun419 opened this issue Sep 11, 2021 · 0 comments
Open

[20210911] (Java) Url Validation #210

JuHyun419 opened this issue Sep 11, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

JuHyun419 commented Sep 11, 2021

Url Validation(apache commons)

  • localhost url 검사 -> ALLOW_LOCAL_URLS
// https://mvnrepository.com/artifact/commons-validator/commons-validator
implementation group: 'commons-validator', name: 'commons-validator', version: '1.7'

public class UrlValidation {

    private UrlValidation () { }

    public static boolean isValidUrl(String url) {
        return isValidUrl(url, null);
    }

    public static boolean isValidUrl(String url, String[] schemes) {
        return new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS).isValid(url);
    }

}


import org.junit.jupiter.api.Test;

import static com.juhyun.shorturl.util.UrlValidation.isValidUrl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class UrlValidationTest {

    @Test
    void localhost_url_통과한다() {
        // given
        String localUrl1 = "http://localhost:8080";
        String localUrl2 = "https://localhost:8080";
        String localUrl3 = "http://localhost:3000";
        String localUrl4 = "https://localhost:3000";

        // when & then
        assertThat(isValidUrl(localUrl1)).isEqualTo(true);
        assertThat(isValidUrl(localUrl2)).isEqualTo(true);
        assertThat(isValidUrl(localUrl3)).isEqualTo(true);
        assertThat(isValidUrl(localUrl4)).isEqualTo(true);
    }

    @Test
    void 일반_url_통과한다() {
        // given
        String successUrl1 = "http://foo.bar.com";
        String successUrl2 = "https://foo.bar.com";
        String successUrl3 = "http://juhyun.com/wiki/$3asdasd-is";

        // when & then
        assertThat(isValidUrl(successUrl1)).isEqualTo(true);
        assertThat(isValidUrl(successUrl2)).isEqualTo(true);
        assertThat(isValidUrl(successUrl3)).isEqualTo(true);
    }

    @Test
    void 특정_스키마를_설정한_url_통과한다() {
        // given
        String[] schemes = {"ftp", "ldap", "juhyun"};
        String url1 = "ldap://foo.bar.com";
        String url2 = "juhyun://foo.bar.com";

        // when & then
        assertThat(isValidUrl(schemes, url1)).isEqualTo(true);
        assertThat(isValidUrl(schemes, url2)).isEqualTo(true);
    }

    @Test
    void 유효하지않은_url_실패한다() {
        // given
        String failUrl1 = "foo.bar.com";
        String failUrl2 = "ldap://foo.bar.com:8080";
        String failUrl3 = "telnet://192.168.100.2:80";

        // when & then
        assertThat(isValidUrl(failUrl1)).isEqualTo(false);
        assertThat(isValidUrl(failUrl2)).isEqualTo(false);
        assertThat(isValidUrl(failUrl3)).isEqualTo(false);
    }

}
@JuHyun419 JuHyun419 added the Java label Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant