|
| 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