From 7ce174dba745a0230951abbf0dfe4d44bbd75269 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Thu, 22 Feb 2024 17:31:37 -0800 Subject: [PATCH] draft of integ test for HTTP response max size Signed-off-by: Dennis Toepker --- .../core/setting/PluginSettings.kt | 2 +- notifications/notifications/build.gradle | 1 + .../integtest/MaxHTTPResponseSizeIT.kt | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 notifications/notifications/src/test/kotlin/org/opensearch/integtest/MaxHTTPResponseSizeIT.kt diff --git a/notifications/core/src/main/kotlin/org/opensearch/notifications/core/setting/PluginSettings.kt b/notifications/core/src/main/kotlin/org/opensearch/notifications/core/setting/PluginSettings.kt index cb1af620..db92dc27 100644 --- a/notifications/core/src/main/kotlin/org/opensearch/notifications/core/setting/PluginSettings.kt +++ b/notifications/core/src/main/kotlin/org/opensearch/notifications/core/setting/PluginSettings.kt @@ -155,7 +155,7 @@ internal object PluginSettings { /** * Default maximum HTTP response string length */ - private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).getBytes().toInt() + private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).bytes.toInt() /** * Default email header length. minimum value from 100 reference emails diff --git a/notifications/notifications/build.gradle b/notifications/notifications/build.gradle index 7d45e886..93dc4568 100644 --- a/notifications/notifications/build.gradle +++ b/notifications/notifications/build.gradle @@ -276,6 +276,7 @@ configurations { } dependencies { + testImplementation project(path: ':opensearch-notifications-core') opensearchPlugin "org.opensearch.plugin:opensearch-notifications-core:${bwcPluginVersion}@zip" opensearchPlugin "org.opensearch.plugin:notifications:${bwcPluginVersion}@zip" } diff --git a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/MaxHTTPResponseSizeIT.kt b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/MaxHTTPResponseSizeIT.kt new file mode 100644 index 00000000..931bda27 --- /dev/null +++ b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/MaxHTTPResponseSizeIT.kt @@ -0,0 +1,79 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.integtest + +import com.sun.net.httpserver.HttpServer +import org.junit.AfterClass +import org.junit.Assert +import org.junit.BeforeClass +import org.opensearch.core.rest.RestStatus +import org.opensearch.notifications.core.NotificationCoreImpl +import org.opensearch.notifications.spi.model.DestinationMessageResponse +import org.opensearch.notifications.spi.model.MessageContent +import org.opensearch.notifications.spi.model.destination.CustomWebhookDestination +import org.opensearch.rest.RestRequest +import java.net.InetAddress +import java.net.InetSocketAddress + +internal class MaxHTTPResponseSizeIT : PluginRestTestCase() { + fun `test HTTP response has truncated size`() { + // update max http response size setting + val updateSettingJsonString = """ + { + "persistent": { + "opensearch.notifications.core.max_http_response_size": "10" + } + } + """.trimIndent() + + val updateSettingsResponse = executeRequest( + RestRequest.Method.PUT.name, + "/_cluster/settings", + updateSettingJsonString, + RestStatus.OK.status + ) + Assert.assertNotNull(updateSettingsResponse) + logger.info("update settings response: $updateSettingsResponse") + Thread.sleep(1000) + + val title = "test custom webhook" + val messageText = "{\"Content\":\"sample message\"}" + val url = "http://${server.address.hostString}:${server.address.port}/webhook" + + val destination = CustomWebhookDestination(url, mapOf("headerKey" to "headerValue"), "POST") + val message = MessageContent(title, messageText) + + val actualCustomWebhookResponse: DestinationMessageResponse = NotificationCoreImpl.sendMessage(destination, message, "ref") + + logger.info("response: ${actualCustomWebhookResponse.statusText}") + } + + companion object { + private lateinit var server: HttpServer + + @JvmStatic + @BeforeClass + fun setupWebhook() { + server = HttpServer.create(InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0) + + server.createContext("/webhook") { + val response = "This is a longer than usual response that should be truncated" + it.sendResponseHeaders(200, response.length.toLong()) + val os = it.responseBody + os.write(response.toByteArray()) + os.close() + } + + server.start() + } + + @JvmStatic + @AfterClass + fun stopMockServer() { + server.stop(1) + } + } +}