Skip to content

Commit

Permalink
Fixed failing test due to the charset not being set.
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesoliveira committed Jun 26, 2024
1 parent ce20c86 commit 1b688c7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.apache.http.HttpStatus
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import java.time.ZonedDateTime
Expand Down Expand Up @@ -476,27 +475,24 @@ class RestServerOpenApiTest : RestServerTestBase() {
}
}

@Disabled("Will come back later")
@Test
fun `GET swagger UI should return html with reference to swagger json`() {
val apiSpec = client.call(GET, WebRequest<Any>("swagger"))
assertEquals(HttpStatus.SC_OK, apiSpec.responseStatus)
assertEquals("text/html", apiSpec.headers["Content-Type"])
assertEquals("text/html;charset=utf-8", apiSpec.headers["Content-Type"])
val expected = """url: "/${context.basePath}/${apiVersion.versionPath}/swagger.json""""
assertTrue(apiSpec.body!!.contains(expected))
}

@Disabled("Will come back later")
@Test
fun `GET swagger UI with trailing slash in path should return html with reference to swagger json without trailing slash`() {
val apiSpec = client.call(GET, WebRequest<Any>("swagger/"))
assertEquals(HttpStatus.SC_OK, apiSpec.responseStatus)
assertEquals("text/html", apiSpec.headers["Content-Type"])
assertEquals("text/html;charset=utf-8", apiSpec.headers["Content-Type"])
val expected = """url: "/${context.basePath}/${apiVersion.versionPath}/swagger.json""""
assertTrue(apiSpec.body!!.contains(expected))
}

@Disabled("Will come back later")
@Test
fun `GET swagger UI dependencies should return non empty result`() {
val baseClient = TestHttpClientUnirestImpl("http://${restServerSettings.address.host}:${server.port}/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ internal object ContextUtils {

private val log = LoggerFactory.getLogger(ContextUtils::class.java)

const val contentTypeApplicationJson = "application/json"

private const val CORDA_X500_NAME = "O=HTTP REST Server, L=New York, C=US"

private fun <T> withMDC(user: String, method: String, path: String, block: () -> T): T {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.corda.rest.server.impl.context

import io.javalin.http.ContentType
import io.javalin.http.Context
import net.corda.rest.ResponseCode
import net.corda.rest.response.ResponseEntity
Expand All @@ -18,7 +19,7 @@ fun Context.buildJsonResult(result: Any?, returnType: Class<*>) {
}
}
(result as? String) != null ->
ctx.contentType(ContextUtils.contentTypeApplicationJson).result(result).status(ResponseCode.OK.statusCode)
ctx.contentType(ContentType.APPLICATION_JSON.name).result(result).status(ResponseCode.OK.statusCode)
result != null -> {
// If the return type does not specify a response code (is not a HttpResponse) we default the status to 200 - OK.
ctx.json(result).status(ResponseCode.OK.statusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import net.corda.rest.server.impl.apigen.processing.RouteProvider
import net.corda.rest.server.impl.apigen.processing.openapi.OpenApiInfoProvider
import net.corda.rest.server.impl.context.ClientHttpRequestContext
import net.corda.rest.server.impl.context.ContextUtils.authenticate
import net.corda.rest.server.impl.context.ContextUtils.contentTypeApplicationJson
import net.corda.rest.server.impl.context.ContextUtils.invokeHttpMethod
import net.corda.rest.server.impl.context.ContextUtils.userNotAuthorized
import net.corda.rest.server.impl.security.RestAuthenticationProvider
Expand All @@ -39,7 +38,6 @@ import net.corda.web.server.JavalinStarter
import org.eclipse.jetty.http2.HTTP2Cipher
import org.eclipse.jetty.server.HttpConnectionFactory
import org.eclipse.jetty.server.SecureRequestCustomizer
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.ServerConnector
import org.eclipse.jetty.server.SslConnectionFactory
import org.eclipse.jetty.util.ssl.SslContextFactory
Expand Down Expand Up @@ -114,7 +112,7 @@ internal class RestServerInternal(
createInsecureServer(config)
}

config.http.defaultContentType = contentTypeApplicationJson
config.http.defaultContentType = ContentType.APPLICATION_JSON.name
// config.bundledPlugins.enableCors { cors ->
// cors.addRule { corsConfig ->
// corsConfig.anyHost()
Expand Down Expand Up @@ -297,7 +295,7 @@ internal class RestServerInternal(
openApiInfoProviders.forEach { openApiInfoProvider ->
get(
openApiInfoProvider.pathForOpenApiJson
) { ctx -> ctx.result(openApiInfoProvider.openApiString).contentType(contentTypeApplicationJson) }
) { ctx -> ctx.result(openApiInfoProvider.openApiString).contentType(ContentType.APPLICATION_JSON.name) }
get(openApiInfoProvider.pathForOpenApiUI, openApiInfoProvider.swaggerUIRenderer)
}
log.trace { "Add OpenApi route completed." }
Expand Down

0 comments on commit 1b688c7

Please sign in to comment.