0

I'm currently getting a "Content type not set" error when trying to verify the content-type with the ResultActionsDsl (content { contentType(MediaType.APPLICATION_JSON) }).

I've tried specifying the content-type in the @PostMapping and I've added @ResponseBody to the function: two things that have been suggested elsewhere, but no love.

Thoughts?

Here are the Controller bits:

@RestController
@RequestMapping("/api/retailer")
class RetailerController(val retailerService: RetailerService, val authHelper: AuthHelper) {

    @PostMapping("/distributor/orders",
        produces = [MediaType.APPLICATION_JSON_VALUE])
    @ResponseBody
    suspend fun postDistributorOrder(
        @RequestParam cohort: String,
        @RequestParam sku: Sku,
        @RequestParam quantity: Qty,
        @RequestParam price: Price,
        request: HttpServletRequest
    ): OrderId {
        val retailerId = authHelper.getRetailerId(request)
        return retailerService.postDistributorOrder(cohort = cohort, retailerId = retailerId, sku = sku, quantity = quantity, price = price)
    }

...

}

Here are the test bits:


@WebMvcTest(RetailerController::class)
class RetailerControllerTest {

...

    @BeforeEach
    fun setup() {
        mvc = MockMvcBuilders
            .webAppContextSetup(context)
            .apply<DefaultMockMvcBuilder>(springSecurity())
            .build()
    }


    val methodUrl = "${baseUrl}/distributor/orders?cohort=redGroup&sku=111&quantity=100&price=5"

    @Test
    fun `should succeed with TeamBot role`() {
        // given
        val user = user(FastUserDetails(userWithTeamBotRole))

        // when
        val resultDsl = mvc.post(methodUrl) { with(user) }

        // then
        resultDsl.andExpect {
            status { isOk() }
            content { contentType(MediaType.APPLICATION_JSON) } // TODO: Why are we getting "Content type not set"?
        }
    }

...

}

Here are the dependency bits:


plugins {
    id("org.springframework.boot") version "2.5.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.20"
    kotlin("plugin.spring") version "1.5.20"
    kotlin("plugin.serialization") version "1.5.20"
}

dependencies {

    val kotlinWrappersKotlinVersion = "1.4.32"
    val kotlinWrappersVersion = "pre.153"
    val kotlinxHtmlVersion = "0.7.2"
    val kotlinCssVersion = "1.0.0"

    implementation(platform("io.projectreactor:reactor-bom:2020.0.8"))
    implementation("io.projectreactor:reactor-core")

    implementation(kotlin("reflect"))
    implementation(kotlin("stdlib-jdk8"))

    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.data:spring-data-redis:2.5.0")
    implementation("redis.clients:jedis:3.6.0")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.jsonwebtoken:jjwt:0.9.1")
    implementation("org.json:json:20160810")
    implementation("javax.xml.bind:jaxb-api")

    implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.15.2")

    implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:${kotlinxHtmlVersion}")
    implementation("org.jetbrains:kotlin-css:${kotlinCssVersion}-${kotlinWrappersVersion}-kotlin-${kotlinWrappersKotlinVersion}")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")

    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.5.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.5.2")
    implementation("io.springfox:springfox-boot-starter:3.0.0")

    implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")

    developmentOnly("org.springframework.boot:spring-boot-devtools")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("io.mockk:mockk:1.12.0")
    testImplementation("com.ninja-squad:springmockk:3.0.1")
    testImplementation("io.kotest:kotest-assertions-core:4.6.1")
    testImplementation("org.apache.httpcomponents:httpclient:4.5.13")
    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1")
    testImplementation("app.cash.turbine:turbine:0.7.0")
    testImplementation(kotlin("test"))

}

tasks.withType<KotlinCompile> {

    sourceCompatibility = "14"
    targetCompatibility = "14"

    kotlinOptions {
        allWarningsAsErrors = false
        suppressWarnings = false
        verbose = false
        freeCompilerArgs = listOf("-Xjsr305=strict", "-Xopt-in=kotlin.time.ExperimentalTime", "-Xopt-in=kotlin.RequiresOptIn")
        jvmTarget = "14"
        languageVersion = "1.5"
        apiVersion = "1.5"
    }
}

1 Answers1

0

Looks like you may need to specify the character encoding, according to this SO answer. Java version:

MvcResult result = mvc.perform(post("/administration")
    .contentType(MediaType.APPLICATION_JSON)
    .content(json)
    .characterEncoding("utf-8"))
    .andExpect(status().isOk())
    .andReturn();

I'll edit this post if you need the Kotlin DSL version.

Gabriel Pizarro
  • 400
  • 4
  • 15
  • Adding contentType, characterEncoding, and the "accept" contentType to the post request still results in getting "Content type not set" in the response. – Michael S. Kelly Jan 08 '22 at 20:24
  • @MichaelS.Kelly Have you tried using Postman? That would give you better debugging capabilities – Gabriel Pizarro Jan 09 '22 at 17:03
  • I want the tests to be part of the automated build pipeline. In addition, some of the APIs are Server Sent Events, which Postman doesn't support. – Michael S. Kelly Jan 10 '22 at 06:05
  • @MichaelS.Kelly I meant temporarily using Postman for this endpoint to confirm that the content-type headers are being sent, aside from your tests. – Gabriel Pizarro Jan 10 '22 at 17:01
  • good thought. I am getting a content-type header when running it outside the test. And, it's what I expected: 'application/json'. – Michael S. Kelly Jan 11 '22 at 17:11