Last active
March 6, 2021 20:46
-
-
Save mikaelhg/1a747d50dae816de5a6fdd95f589d666 to your computer and use it in GitHub Desktop.
Spring multipart file upload test with TestRestTemplate and ByteArrayResource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.servlet.multipart.max-file-size: 300MB | |
spring.servlet.multipart.max-request-size: 300MB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.tasks.testing.logging.TestLogEvent.* | |
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | |
plugins { | |
id("org.springframework.boot") version "2.4.3" | |
id("io.spring.dependency-management") version "1.0.11.RELEASE" | |
kotlin("jvm") version "1.4.30" | |
kotlin("plugin.spring") version "1.4.30" | |
} | |
group = "com.example" | |
version = "0.0.1-SNAPSHOT" | |
java { | |
toolchain { | |
languageVersion.set(JavaLanguageVersion.of(15)) | |
} | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
implementation("org.springframework.boot:spring-boot-starter-web") { | |
exclude(group = "org.springframework.boot", module = "spring-boot-starter-tomcat") | |
} | |
implementation("org.springframework.boot:spring-boot-starter-undertow") | |
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") | |
implementation("org.jetbrains.kotlin:kotlin-reflect") | |
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | |
testImplementation("org.springframework.boot:spring-boot-starter-test") | |
} | |
tasks.withType<KotlinCompile> { | |
kotlinOptions { | |
freeCompilerArgs = listOf("-Xjsr305=strict") | |
jvmTarget = "15" | |
} | |
} | |
tasks.withType<Test> { | |
useJUnitPlatform() | |
minHeapSize = "1g" | |
maxHeapSize = "1g" | |
testLogging { | |
events = setOf(PASSED, FAILED, SKIPPED, STANDARD_OUT, STANDARD_ERROR) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.uploadtest | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.boot.runApplication | |
import org.springframework.web.bind.annotation.PostMapping | |
import org.springframework.web.bind.annotation.RequestParam | |
import org.springframework.web.bind.annotation.RestController | |
import org.springframework.web.multipart.MultipartFile | |
@SpringBootApplication | |
class UploadTestApplication | |
fun main(args: Array<String>) { | |
runApplication<UploadTestApplication>(*args) | |
} | |
@RestController | |
class ApiController { | |
@PostMapping("/binary") | |
fun binary(@RequestParam data: MultipartFile) { | |
println(data) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.uploadtest | |
import org.junit.jupiter.api.Assertions | |
import org.junit.jupiter.api.Test | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.context.SpringBootTest | |
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT | |
import org.springframework.boot.test.web.client.TestRestTemplate | |
import org.springframework.core.io.ByteArrayResource | |
import org.springframework.http.MediaType | |
import org.springframework.http.RequestEntity | |
import org.springframework.util.LinkedMultiValueMap | |
import org.springframework.util.unit.DataSize | |
@SpringBootTest(webEnvironment = RANDOM_PORT) | |
class UploadTestApplicationTests { | |
companion object { | |
val SIZE = DataSize.ofMegabytes(200).toBytes().toInt() | |
} | |
@Autowired | |
lateinit var restTemplate: TestRestTemplate | |
@Test | |
fun maximumUploadSize() { | |
val resource = object : ByteArrayResource(ByteArray(SIZE)) { | |
override fun getFilename() = "data.txt" | |
} | |
val request = RequestEntity.post("/binary") | |
.contentType(MediaType.MULTIPART_FORM_DATA) | |
.body(LinkedMultiValueMap<String, Any>().apply { add("data", resource) }) | |
val response = restTemplate.exchange(request, String::class.java) | |
Assertions.assertTrue(response.statusCode.is2xxSuccessful) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment