Skip to content

Commit a62c920

Browse files
Adding Max HTTP Response Size IT (#901)
* Adding integ test for HTTP response max size setting Signed-off-by: Dennis Toepker <toepkerd@amazon.com> * misc build fixes Signed-off-by: Dennis Toepker <toepkerd@amazon.com> --------- Signed-off-by: Dennis Toepker <toepkerd@amazon.com> Co-authored-by: Dennis Toepker <toepkerd@amazon.com>
1 parent fa4007c commit a62c920

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

notifications/core/src/main/kotlin/org/opensearch/notifications/core/setting/PluginSettings.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ internal object PluginSettings {
153153
private const val DEFAULT_SOCKET_TIMEOUT_MILLISECONDS = 50000
154154

155155
/**
156-
* Default maximum HTTP response string length
156+
* Default maximum HTTP response size
157157
*/
158-
private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).getBytes().toInt()
158+
private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).bytes.toInt()
159159

160160
/**
161161
* Default email header length. minimum value from 100 reference emails
@@ -235,7 +235,7 @@ internal object PluginSettings {
235235
var socketTimeout: Int
236236

237237
/**
238-
* Maximum HTTP response string length
238+
* Maximum HTTP response size
239239
*/
240240
@Volatile
241241
var maxHttpResponseSize: Int
@@ -513,7 +513,7 @@ internal object PluginSettings {
513513
val clusterMaxHttpResponseSize = clusterService.clusterSettings.get(MAX_HTTP_RESPONSE_SIZE)
514514
if (clusterMaxHttpResponseSize != null) {
515515
log.debug("$LOG_PREFIX:$MAX_HTTP_RESPONSE_SIZE_KEY -autoUpdatedTo-> $clusterMaxHttpResponseSize")
516-
socketTimeout = clusterSocketTimeout
516+
maxHttpResponseSize = clusterMaxHttpResponseSize
517517
}
518518
val clusterAllowedConfigTypes = clusterService.clusterSettings.get(ALLOWED_CONFIG_TYPES)
519519
if (clusterAllowedConfigTypes != null) {

notifications/notifications/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ integTest {
233233
if (usingRemoteCluster) {
234234
filter {
235235
excludeTestsMatching "org.opensearch.integtest.send.SendTestMessageWithMockServerIT"
236+
excludeTestsMatching "org.opensearch.integtest.MaxHTTPResponseSizeIT"
236237
}
237238
}
238239
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.integtest
7+
8+
import com.sun.net.httpserver.HttpServer
9+
import org.junit.AfterClass
10+
import org.junit.Assert
11+
import org.junit.BeforeClass
12+
import org.opensearch.core.rest.RestStatus
13+
import org.opensearch.notifications.NotificationPlugin
14+
import org.opensearch.rest.RestRequest
15+
import java.net.InetAddress
16+
import java.net.InetSocketAddress
17+
18+
internal class MaxHTTPResponseSizeIT : PluginRestTestCase() {
19+
fun `test HTTP response has truncated size`() {
20+
// update max http response size setting
21+
val updateSettingJsonString = """
22+
{
23+
"transient": {
24+
"opensearch.notifications.core.max_http_response_size": "8"
25+
}
26+
}
27+
""".trimIndent()
28+
29+
val updateSettingsResponse = executeRequest(
30+
RestRequest.Method.PUT.name,
31+
"/_cluster/settings",
32+
updateSettingJsonString,
33+
RestStatus.OK.status
34+
)
35+
Assert.assertNotNull(updateSettingsResponse)
36+
logger.info("update settings response: $updateSettingsResponse")
37+
Thread.sleep(1000)
38+
39+
val url = "http://${server.address.hostString}:${server.address.port}/webhook"
40+
41+
val createRequestJsonString = """
42+
{
43+
"config":{
44+
"name":"this is a sample config name",
45+
"description":"this is a sample config description",
46+
"config_type":"webhook",
47+
"is_enabled":true,
48+
"webhook":{
49+
"url":"$url",
50+
"header_params": {
51+
"Content-type": "text/plain"
52+
}
53+
}
54+
}
55+
}
56+
""".trimIndent()
57+
val configId = createConfigWithRequestJsonString(createRequestJsonString)
58+
Assert.assertNotNull(configId)
59+
Thread.sleep(1000)
60+
61+
// send test message
62+
val sendResponse = executeRequest(
63+
RestRequest.Method.POST.name,
64+
"${NotificationPlugin.PLUGIN_BASE_URI}/feature/test/$configId",
65+
"",
66+
RestStatus.OK.status
67+
)
68+
69+
logger.info("response: $sendResponse")
70+
71+
val statusText = sendResponse.getAsJsonArray("status_list")[0].asJsonObject["delivery_status"].asJsonObject["status_text"].asString
72+
73+
// we set the max HTTP response size to 8 bytes, which means the expected string length of the response is 8 / 2 (bytes per Java char) = 4
74+
Assert.assertEquals(4, statusText.length)
75+
}
76+
77+
companion object {
78+
private lateinit var server: HttpServer
79+
80+
@JvmStatic
81+
@BeforeClass
82+
fun setupWebhook() {
83+
server = HttpServer.create(InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0)
84+
85+
server.createContext("/webhook") {
86+
val response = "This is a longer than usual response that should be truncated"
87+
it.sendResponseHeaders(200, response.length.toLong())
88+
val os = it.responseBody
89+
os.write(response.toByteArray())
90+
os.close()
91+
}
92+
93+
server.start()
94+
}
95+
96+
@JvmStatic
97+
@AfterClass
98+
fun stopMockServer() {
99+
server.stop(1)
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)