|
| 1 | +/* |
| 2 | + |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.server.http; |
| 20 | + |
| 21 | +import com.arcadedb.ContextConfiguration; |
| 22 | +import com.arcadedb.GlobalConfiguration; |
| 23 | +import com.arcadedb.server.BaseGraphServerTest; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.io.DataOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.net.HttpURLConnection; |
| 30 | +import java.net.URL; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Base64; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Regression test for https://github.com/ArcadeData/arcadedb/issues/3567 |
| 39 | + * When the request body exceeds the configured limit, the server should return |
| 40 | + * HTTP 413 with a descriptive JSON error body (not a silent 400 with no content). |
| 41 | + */ |
| 42 | +class HttpBodySizeLimitTest extends BaseGraphServerTest { |
| 43 | + |
| 44 | + private static final long SMALL_LIMIT_BYTES = 1024L; // 1KB for testing |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void onServerConfiguration(final ContextConfiguration config) { |
| 48 | + config.setValue(GlobalConfiguration.SERVER_HTTP_BODY_CONTENT_MAX_SIZE, SMALL_LIMIT_BYTES); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void requestExceedingBodyLimitReturns413WithJsonError() throws Exception { |
| 53 | + testEachServer((serverIndex) -> { |
| 54 | + final HttpURLConnection connection = (HttpURLConnection) new URL( |
| 55 | + "http://127.0.0.1:248" + serverIndex + "/api/v1/command/graph").openConnection(); |
| 56 | + connection.setRequestMethod("POST"); |
| 57 | + connection.setRequestProperty("Authorization", |
| 58 | + "Basic " + Base64.getEncoder().encodeToString(("root:" + DEFAULT_PASSWORD_FOR_TESTS).getBytes())); |
| 59 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 60 | + |
| 61 | + // Body exceeds the 1KB limit |
| 62 | + final byte[] largeBody = new byte[(int) (SMALL_LIMIT_BYTES * 2)]; |
| 63 | + Arrays.fill(largeBody, (byte) 'x'); |
| 64 | + connection.setRequestProperty("Content-Length", Integer.toString(largeBody.length)); |
| 65 | + connection.setDoOutput(true); |
| 66 | + |
| 67 | + try (final DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { |
| 68 | + wr.write(largeBody); |
| 69 | + } catch (IOException ignored) { |
| 70 | + // Server may close connection early after sending 413; this is expected |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + final int responseCode = connection.getResponseCode(); |
| 75 | + assertThat(responseCode).isEqualTo(413); |
| 76 | + |
| 77 | + final String errorBody = readErrorStream(connection); |
| 78 | + assertThat(errorBody).contains("error"); |
| 79 | + assertThat(errorBody).contains("arcadedb.server.httpBodyContentMaxSize"); |
| 80 | + } finally { |
| 81 | + connection.disconnect(); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void requestWithinBodyLimitSucceeds() throws Exception { |
| 88 | + testEachServer((serverIndex) -> { |
| 89 | + // A small query well within the 1KB limit should succeed normally |
| 90 | + final HttpURLConnection connection = (HttpURLConnection) new URL( |
| 91 | + "http://127.0.0.1:248" + serverIndex + "/api/v1/query/graph").openConnection(); |
| 92 | + connection.setRequestMethod("POST"); |
| 93 | + connection.setRequestProperty("Authorization", |
| 94 | + "Basic " + Base64.getEncoder().encodeToString(("root:" + DEFAULT_PASSWORD_FOR_TESTS).getBytes())); |
| 95 | + |
| 96 | + final String payload = "{\"language\":\"sql\",\"command\":\"SELECT 1\"}"; |
| 97 | + final byte[] data = payload.getBytes(StandardCharsets.UTF_8); |
| 98 | + connection.setRequestProperty("Content-Length", Integer.toString(data.length)); |
| 99 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 100 | + connection.setDoOutput(true); |
| 101 | + |
| 102 | + try (final DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { |
| 103 | + wr.write(data); |
| 104 | + } |
| 105 | + connection.connect(); |
| 106 | + |
| 107 | + try { |
| 108 | + assertThat(connection.getResponseCode()).isEqualTo(200); |
| 109 | + } finally { |
| 110 | + connection.disconnect(); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + private String readErrorStream(final HttpURLConnection connection) { |
| 116 | + try { |
| 117 | + final InputStream errorStream = connection.getErrorStream(); |
| 118 | + if (errorStream == null) |
| 119 | + return ""; |
| 120 | + return new String(errorStream.readAllBytes(), StandardCharsets.UTF_8); |
| 121 | + } catch (IOException e) { |
| 122 | + return ""; |
| 123 | + } |
| 124 | + } |
| 125 | +} |