Search in sources :

Example 26 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class NumberParameterValidatorTest method validate_withValueGreaterThanMax_shouldFail_ifMaxSpecified.

@Test
public void validate_withValueGreaterThanMax_shouldFail_ifMaxSpecified() {
    Status status = classUnderTest.validate("1.1", floatParam(null, new BigDecimal(1.0)));
    Assert.assertNotNull(status);
    // request parameter number above max
    Assert.assertEquals("ERR11012", status.getCode());
}
Also used : Status(com.networknt.status.Status) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 27 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class ParameterValidatorsTest method validate_withInvalidNumberParam_shouldFail.

@Test
public void validate_withInvalidNumberParam_shouldFail() {
    Status status = parameterValidators.validate("1.0a", floatParam());
    Assert.assertNotNull(status);
    // request parameter invalid format
    Assert.assertEquals("ERR11010", status.getCode());
}
Also used : Status(com.networknt.status.Status) Test(org.junit.Test)

Example 28 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class StringParameterValidatorTest method validate_withNullValue_shouldFail_whenRequired.

@Test
public void validate_withNullValue_shouldFail_whenRequired() {
    Status status = classUnderTest.validate(null, stringParam(true));
    Assert.assertNotNull(status);
    // request parameter missing
    Assert.assertEquals("ERR11001", status.getCode());
}
Also used : Status(com.networknt.status.Status) Test(org.junit.Test)

Example 29 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class StringParameterValidatorTest method validate_withEmptyValue_shouldFail_whenRequired.

@Test
public void validate_withEmptyValue_shouldFail_whenRequired() {
    Status status = classUnderTest.validate("", stringParam(true));
    Assert.assertNotNull(status);
    // request parameter missing
    Assert.assertEquals("ERR11001", status.getCode());
}
Also used : Status(com.networknt.status.Status) Test(org.junit.Test)

Example 30 with Status

use of com.networknt.status.Status in project light-4j by networknt.

the class BodyHandler method handleRequest.

/**
 * Check the header starts with application/json and parse it into map or list
 * based on the first character "{" or "[". Ignore other content type values.
 *
 * @param exchange HttpServerExchange
 * @throws Exception Exception
 */
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    // parse the body to map or list if content type is application/json
    String contentType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE);
    if (contentType != null && contentType.startsWith("application/json")) {
        if (exchange.isInIoThread()) {
            exchange.dispatch(this);
            return;
        }
        exchange.startBlocking();
        InputStream is = exchange.getInputStream();
        if (is != null) {
            try {
                if (is.available() != -1) {
                    Object body;
                    String s = new Scanner(is, "UTF-8").useDelimiter("\\A").next();
                    s = s.trim();
                    if (s.startsWith("{")) {
                        body = Config.getInstance().getMapper().readValue(s, new TypeReference<HashMap<String, Object>>() {
                        });
                    } else if (s.startsWith("[")) {
                        body = Config.getInstance().getMapper().readValue(s, new TypeReference<List<Object>>() {
                        });
                    } else {
                        // error here. The content type in head doesn't match the body.
                        Status status = new Status(CONTENT_TYPE_MISMATCH, contentType);
                        exchange.setStatusCode(status.getStatusCode());
                        exchange.getResponseSender().send(status.toString());
                        return;
                    }
                    exchange.putAttachment(REQUEST_BODY, body);
                }
            } catch (IOException e) {
                logger.error("IOException: ", e);
                Status status = new Status(CONTENT_TYPE_MISMATCH, contentType);
                exchange.setStatusCode(status.getStatusCode());
                exchange.getResponseSender().send(status.toString());
                return;
            }
        }
    }
    next.handleRequest(exchange);
}
Also used : Status(com.networknt.status.Status) Scanner(java.util.Scanner) InputStream(java.io.InputStream) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Aggregations

Status (com.networknt.status.Status)71 Test (org.junit.Test)45 Http2Client (com.networknt.client.Http2Client)19 ClientException (com.networknt.exception.ClientException)19 URI (java.net.URI)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 ClientConnection (io.undertow.client.ClientConnection)17 ClientRequest (io.undertow.client.ClientRequest)17 ClientResponse (io.undertow.client.ClientResponse)17 IOException (java.io.IOException)12 FrameworkException (com.networknt.exception.FrameworkException)9 HttpString (io.undertow.util.HttpString)7 IntegerProperty (io.swagger.models.properties.IntegerProperty)4 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 JsonSchema (com.networknt.schema.JsonSchema)3 ValidationMessage (com.networknt.schema.ValidationMessage)3