forked from micronaut-projects/micronaut-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72e53ea
commit 3bb6009
Showing
7 changed files
with
350 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/server/upload/UploadController.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.micronaut.docs.server.upload | ||
|
||
// tag::imports[] | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.multipart.CompletedFileUpload | ||
import io.micronaut.http.multipart.StreamingFileUpload | ||
import io.reactivex.Single | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
// end::imports[] | ||
|
||
// tag::class[] | ||
@Controller("/upload") | ||
class UploadController { | ||
// end::class[] | ||
|
||
// tag::upload[] | ||
@Post(value = "/", consumes = [MediaType.MULTIPART_FORM_DATA]) // <1> | ||
fun upload(file: StreamingFileUpload): Single<HttpResponse<String>> { // <2> | ||
val tempFile = File.createTempFile(file.filename, "temp") | ||
val uploadPublisher = file.transferTo(tempFile) // <3> | ||
return Single.fromPublisher(uploadPublisher) // <4> | ||
.map { success -> | ||
if (success) { | ||
HttpResponse.ok("Uploaded") | ||
} else { | ||
HttpResponse.status<String>(HttpStatus.CONFLICT) | ||
.body("Upload Failed") | ||
} | ||
} | ||
} | ||
// end::upload[] | ||
|
||
// tag::completedUpload[] | ||
@Post(value = "/completed", consumes = [MediaType.MULTIPART_FORM_DATA]) // <1> | ||
fun uploadCompleted(file: CompletedFileUpload): HttpResponse<String> { // <2> | ||
return try { | ||
val tempFile = File.createTempFile(file.filename, "temp") //<3> | ||
val path = Paths.get(tempFile.absolutePath) | ||
Files.write(path, file.bytes) //<3> | ||
HttpResponse.ok("Uploaded") | ||
} catch (exception: IOException) { | ||
HttpResponse.badRequest("Upload Failed") | ||
} | ||
|
||
} | ||
// end::completedUpload[] | ||
|
||
// tag::bytesUpload[] | ||
@Post(value = "/bytes", consumes = [MediaType.MULTIPART_FORM_DATA]) // <1> | ||
fun uploadBytes(file: ByteArray, fileName: String): HttpResponse<String> { // <2> | ||
return try { | ||
val tempFile = File.createTempFile(fileName, "temp") | ||
val path = Paths.get(tempFile.absolutePath) | ||
Files.write(path, file) // <3> | ||
HttpResponse.ok("Uploaded") | ||
} catch (exception: IOException) { | ||
HttpResponse.badRequest("Upload Failed") | ||
} | ||
|
||
} | ||
// end::bytesUpload[] | ||
|
||
// tag::endclass[] | ||
} | ||
// end::endclass[] |
61 changes: 61 additions & 0 deletions
61
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/server/upload/UploadControllerSpec.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.micronaut.docs.server.upload | ||
|
||
import io.kotlintest.shouldBe | ||
import io.kotlintest.shouldThrow | ||
import io.kotlintest.specs.StringSpec | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.RxHttpClient | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.http.client.multipart.MultipartBody | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import io.reactivex.Flowable | ||
|
||
class UploadControllerSpec: StringSpec() { | ||
|
||
val embeddedServer = autoClose( | ||
ApplicationContext.run(EmbeddedServer::class.java) | ||
) | ||
|
||
val client = autoClose( | ||
embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.getURL()) | ||
) | ||
|
||
init { | ||
"test file upload"() { | ||
val body = MultipartBody.builder() | ||
.addPart("file", "file.json", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".toByteArray()) | ||
.build() | ||
|
||
val flowable = Flowable.fromPublisher(client.exchange( | ||
HttpRequest.POST("/upload", body) | ||
.contentType(MediaType.MULTIPART_FORM_DATA) | ||
.accept(MediaType.TEXT_PLAIN_TYPE), | ||
String::class.java | ||
)) | ||
val response = flowable.blockingFirst() | ||
|
||
response.status() shouldBe HttpStatus.OK | ||
response.body.get() shouldBe "Uploaded" | ||
} | ||
|
||
"test completed file upload with no name but with bytes"() { | ||
val body = MultipartBody.builder() | ||
.addPart("file", "", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".toByteArray()) | ||
.build() | ||
|
||
val flowable = Flowable.fromPublisher(client.exchange( | ||
HttpRequest.POST("/upload/completed", body) | ||
.contentType(MediaType.MULTIPART_FORM_DATA) | ||
.accept(MediaType.TEXT_PLAIN_TYPE), | ||
String::class.java | ||
)) | ||
|
||
val ex = shouldThrow<HttpClientResponseException> { flowable.blockingFirst() } | ||
|
||
ex.message shouldBe "Required argument [CompletedFileUpload file] not specified" | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.