|
| 1 | +import com.google.gson.JsonObject |
| 2 | +import io.netty.handler.codec.http.FullHttpResponse |
| 3 | +import net.ccbluex.netty.http.HttpServer |
| 4 | +import net.ccbluex.netty.http.model.RequestObject |
| 5 | +import net.ccbluex.netty.http.util.httpOk |
| 6 | +import okhttp3.OkHttpClient |
| 7 | +import okhttp3.Request |
| 8 | +import okhttp3.Response |
| 9 | +import org.junit.jupiter.api.* |
| 10 | +import java.io.File |
| 11 | +import java.nio.file.Files |
| 12 | +import kotlin.concurrent.thread |
| 13 | +import kotlin.test.assertEquals |
| 14 | +import kotlin.test.assertNotNull |
| 15 | +import kotlin.test.assertTrue |
| 16 | + |
| 17 | +/** |
| 18 | + * Test class for the HttpServer, focusing on verifying the routing capabilities |
| 19 | + * and correctness of responses from different endpoints. |
| 20 | + */ |
| 21 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 22 | +class HttpMiddlewareServerTest { |
| 23 | + |
| 24 | + private lateinit var serverThread: Thread |
| 25 | + private val client = OkHttpClient() |
| 26 | + |
| 27 | + /** |
| 28 | + * This method sets up the necessary environment before any tests are run. |
| 29 | + * It creates a temporary directory with dummy files and starts the HTTP server |
| 30 | + * in a separate thread. |
| 31 | + */ |
| 32 | + @BeforeAll |
| 33 | + fun initialize() { |
| 34 | + // Start the HTTP server in a separate thread |
| 35 | + serverThread = thread { |
| 36 | + startHttpServer() |
| 37 | + } |
| 38 | + |
| 39 | + // Allow the server some time to start |
| 40 | + Thread.sleep(1000) |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This method cleans up resources after all tests have been executed. |
| 45 | + * It stops the server and deletes the temporary directory. |
| 46 | + */ |
| 47 | + @AfterAll |
| 48 | + fun cleanup() { |
| 49 | + serverThread.interrupt() |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * This function starts the HTTP server with routing configured for |
| 54 | + * different difficulty levels. |
| 55 | + */ |
| 56 | + private fun startHttpServer() { |
| 57 | + val server = HttpServer() |
| 58 | + |
| 59 | + server.routeController.apply { |
| 60 | + get("/", ::static) |
| 61 | + } |
| 62 | + |
| 63 | + server.middleware { requestContext, fullHttpResponse -> |
| 64 | + // Add custom headers to the response |
| 65 | + fullHttpResponse.headers().add("X-Custom-Header", "Custom Value") |
| 66 | + |
| 67 | + // Add a custom header if there is a query parameter |
| 68 | + if (requestContext.params.isNotEmpty()) { |
| 69 | + fullHttpResponse.headers().add("X-Query-Param", |
| 70 | + requestContext.params.entries.joinToString(",")) |
| 71 | + } |
| 72 | + |
| 73 | + fullHttpResponse |
| 74 | + } |
| 75 | + |
| 76 | + server.start(8080) // Start the server on port 8080 |
| 77 | + } |
| 78 | + |
| 79 | + @Suppress("UNUSED_PARAMETER") |
| 80 | + fun static(requestObject: RequestObject): FullHttpResponse { |
| 81 | + return httpOk(JsonObject().apply { |
| 82 | + addProperty("message", "Hello, World!") |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Utility function to make HTTP GET requests to the specified path. |
| 88 | + * |
| 89 | + * @param path The path for the request. |
| 90 | + * @return The HTTP response. |
| 91 | + */ |
| 92 | + private fun makeRequest(path: String): Response { |
| 93 | + val request = Request.Builder() |
| 94 | + .url("http://localhost:8080$path") |
| 95 | + .build() |
| 96 | + return client.newCall(request).execute() |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Test the root endpoint ("/") and verify that it returns the correct number |
| 101 | + * of files in the directory. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + fun testRootEndpoint() { |
| 105 | + val response = makeRequest("/") |
| 106 | + assertEquals(200, response.code(), "Expected status code 200") |
| 107 | + |
| 108 | + val responseBody = response.body()?.string() |
| 109 | + assertNotNull(responseBody, "Response body should not be null") |
| 110 | + |
| 111 | + assertTrue(responseBody.contains("Hello, World!"), "Response should contain 'Hello, World!'") |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test the root endpoint ("/") with a query parameter and verify that the |
| 116 | + * custom header is added to the response. |
| 117 | + */ |
| 118 | + @Test |
| 119 | + fun testRootEndpointWithQueryParam() { |
| 120 | + val response = makeRequest("/?param1=value1¶m2=value2") |
| 121 | + assertEquals(200, response.code(), "Expected status code 200") |
| 122 | + |
| 123 | + val responseBody = response.body()?.string() |
| 124 | + assertNotNull(responseBody, "Response body should not be null") |
| 125 | + |
| 126 | + assertTrue(responseBody.contains("Hello, World!"), "Response should contain 'Hello, World!'") |
| 127 | + assertTrue(response.headers("X-Custom-Header").contains("Custom Value"), "Custom header should be present") |
| 128 | + assertTrue(response.headers("X-Query-Param").contains("param1=value1,param2=value2"), |
| 129 | + "Query parameter should be present in the response") |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments