Search in sources :

Example 1 with BadRequestException

use of core.framework.web.exception.BadRequestException in project core-ng-project by neowu.

the class RequestParserTest method requestURLIsTooLong.

@Test
void requestURLIsTooLong() {
    HttpServerExchange exchange = new HttpServerExchange(null, -1);
    exchange.getRequestHeaders().put(Headers.HOST, "localhost");
    exchange.setRequestURI("/path");
    StringBuilder builder = new StringBuilder(1000);
    for (int i = 0; i < 100; i++) {
        builder.append("1234567890");
    }
    exchange.setQueryString(builder.toString());
    RequestImpl request = new RequestImpl(exchange, null);
    request.scheme = "http";
    request.port = 80;
    BadRequestException exception = assertThrows(BadRequestException.class, () -> parser.requestURL(request, exchange));
    assertThat(exception.getMessage()).contains("requestURL is too long");
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) BadRequestException(core.framework.web.exception.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 2 with BadRequestException

use of core.framework.web.exception.BadRequestException in project core-ng-project by neowu.

the class URLParamParserTest method failedToParseEnum.

@Test
void failedToParseEnum() {
    BadRequestException exception = assertThrows(BadRequestException.class, () -> URLParamParser.parse("V2", TestEnum.class));
    assertThat(exception.getMessage()).contains("failed to parse");
}
Also used : BadRequestException(core.framework.web.exception.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 3 with BadRequestException

use of core.framework.web.exception.BadRequestException in project core-ng-project by neowu.

the class PathParamsTest method putEmptyPathParam.

@Test
void putEmptyPathParam() {
    BadRequestException exception = assertThrows(BadRequestException.class, () -> pathParams.put("id", ""));
    assertThat(exception.getMessage()).contains("name=id, value=");
}
Also used : BadRequestException(core.framework.web.exception.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 4 with BadRequestException

use of core.framework.web.exception.BadRequestException in project core-ng-project by neowu.

the class RequestParser method requestURL.

String requestURL(RequestImpl request, HttpServerExchange exchange) {
    StringBuilder builder = new StringBuilder();
    if (exchange.isHostIncludedInRequestURI()) {
        // GET can use absolute url as request uri, http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
        builder.append(exchange.getRequestURI());
    } else {
        String scheme = request.scheme;
        int port = request.port;
        builder.append(scheme).append("://").append(exchange.getHostName());
        if (!("http".equals(scheme) && port == 80) && !("https".equals(scheme) && port == 443)) {
            builder.append(':').append(port);
        }
        builder.append(exchange.getRequestURI());
    }
    String queryString = exchange.getQueryString();
    if (!Strings.isEmpty(queryString))
        builder.append('?').append(queryString);
    String requestURL = builder.toString();
    if (requestURL.length() > MAX_URL_LENGTH)
        throw new BadRequestException(Strings.format("requestURL is too long, requestURL={}...(truncated)", requestURL.substring(0, MAX_URL_LENGTH / 8)));
    return requestURL;
}
Also used : BadRequestException(core.framework.web.exception.BadRequestException) HttpString(io.undertow.util.HttpString)

Aggregations

BadRequestException (core.framework.web.exception.BadRequestException)4 Test (org.junit.jupiter.api.Test)3 HttpServerExchange (io.undertow.server.HttpServerExchange)1 HttpString (io.undertow.util.HttpString)1