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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations