-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from macisamuele/maci-fix-99-file-uploads
File uploads tests
- Loading branch information
Showing
7 changed files
with
272 additions
and
9 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...es/junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/PostFileEndpointTest.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,83 @@ | ||
package com.yelp.codegen.generatecodesamples | ||
|
||
import com.yelp.codegen.generatecodesamples.apis.FileApi | ||
import com.yelp.codegen.generatecodesamples.tools.MockServerApiRule | ||
import com.yelp.codegen.generatecodesamples.tools.MultiPartInfo | ||
import com.yelp.codegen.generatecodesamples.tools.decodeMultiPartBody | ||
import okhttp3.MediaType | ||
import okhttp3.RequestBody | ||
import okhttp3.mockwebserver.MockResponse | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class PostFileEndpointTest { | ||
|
||
@get:Rule | ||
val rule = MockServerApiRule() | ||
|
||
@Test | ||
fun sendFileTest() { | ||
rule.server.enqueue(MockResponse()) | ||
|
||
rule.getApi<FileApi>().postFile( | ||
clientFile = RequestBody.create(MediaType.parse("application/json"), "{}") | ||
).blockingGet() | ||
|
||
val fileApiStats = rule.server.takeRequest().decodeMultiPartBody() | ||
assertNotNull(fileApiStats.boundary) | ||
assertEquals( | ||
listOf( | ||
MultiPartInfo( | ||
contentDisposition = "form-data; name=\"client_file\"; filename=\"client_file\"", | ||
contentType = "application/json; charset=utf-8", | ||
contentLength = "2", | ||
fileContent = "{}" | ||
) | ||
), | ||
fileApiStats.parts | ||
) | ||
} | ||
|
||
@Test | ||
fun sendMultipleFilesTest() { | ||
rule.server.enqueue(MockResponse()) | ||
|
||
rule.getApi<FileApi>().postMultipleFiles( | ||
clientFile = RequestBody.create(MediaType.parse("application/json"), "{}"), | ||
certificateFile = RequestBody.create(MediaType.parse("text/plain"), "Some Text") | ||
).blockingGet() | ||
|
||
val fileApiStats = rule.server.takeRequest().decodeMultiPartBody() | ||
assertNotNull(fileApiStats.boundary) | ||
assertEquals( | ||
listOf( | ||
MultiPartInfo( | ||
contentDisposition = "form-data; name=\"client_file\"; filename=\"client_file\"", | ||
contentType = "application/json; charset=utf-8", | ||
contentLength = "2", | ||
fileContent = "{}" | ||
), | ||
MultiPartInfo( | ||
contentDisposition = "form-data; name=\"certificate_file\"; filename=\"certificate_file\"", | ||
contentType = "text/plain; charset=utf-8", | ||
contentLength = "9", | ||
fileContent = "Some Text" | ||
) | ||
), | ||
fileApiStats.parts | ||
) | ||
} | ||
|
||
@Test | ||
fun ensureThatEndpointsWithFileAndNotMultipartFormDataAreMarkedAsDeprecated() { | ||
assertTrue( | ||
FileApi::class.java.methods | ||
.first { it.name == "postFileWithoutMultipartFormData" } | ||
.getAnnotationsByType(Deprecated::class.java) | ||
.isNotEmpty() | ||
) | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/tools/MultiPartTestUtil.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,94 @@ | ||
package com.yelp.codegen.generatecodesamples.tools | ||
|
||
import okhttp3.mockwebserver.RecordedRequest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
|
||
data class FileApiStats( | ||
val boundary: String, | ||
val parts: List<MultiPartInfo> | ||
) | ||
|
||
data class MultiPartInfo( | ||
val fileContent: String? = null, | ||
val contentDisposition: String? = null, | ||
val contentLength: String? = null, | ||
val contentType: String? = null | ||
) | ||
|
||
private fun processPart(part: String): MultiPartInfo { | ||
// part would look like "\r\nHeader-Name: Header-Value\r\n...\r\n\r\nbody\r\n" | ||
// Here we're removing the first and last part as we know that are empty | ||
val partLines = part.split("\r\n").drop(1).dropLast(1) | ||
|
||
val indexBoundaryHeadersContent = partLines.indexOf("") | ||
|
||
val headers = partLines.subList(0, indexBoundaryHeadersContent).map { | ||
val (name, value) = it.split(": ", limit = 2) | ||
// Note: in theory we can have the same header name multiples times | ||
// We're not dealing with it as this is a test utility ;) | ||
name to value | ||
}.toMap() | ||
|
||
val content = partLines.subList(indexBoundaryHeadersContent, partLines.size).drop(1).joinToString("\r\n") | ||
return MultiPartInfo( | ||
fileContent = content, | ||
contentDisposition = headers["Content-Disposition"], | ||
contentType = headers["Content-Type"], | ||
contentLength = headers["Content-Length"] | ||
) | ||
} | ||
|
||
/** | ||
* Extract statistics from the body of the recorded request (assuming that it is multipart body) | ||
*/ | ||
fun RecordedRequest.decodeMultiPartBody(): FileApiStats { | ||
/** | ||
* requestBody has to be compliant to the HTTP Specifications for MultiPart (RFC 2387) | ||
* | ||
* This means that the body will look like | ||
* --<BOUNDARY>\r\n | ||
* <Content1 Header Name>: <Content1 Header Value>\r\n | ||
* [<Content1 Header Name>: <Content1 Header Value>\r\n] | ||
* \r\n | ||
* <Content1>\r\n | ||
* --<BOUNDARY>\r\n | ||
* <Content# Header Name>: <Content# Header Value>\r\n | ||
* [<Content# Header Name>: <Content# Header Value>\r\n] | ||
* \r\n | ||
* <Content#>\r\n | ||
* --<BOUNDARY>--\r\n | ||
* \r\n | ||
*/ | ||
val requestBody = this.body.readUtf8() | ||
|
||
val boundaryLine = requestBody.split("\r\n").first() | ||
|
||
assertTrue( | ||
"Boundary should start with '--'", | ||
boundaryLine.startsWith("--") | ||
) | ||
|
||
val boundary = boundaryLine.substring(startIndex = 2) | ||
val bodyParts = requestBody.split(boundaryLine) | ||
|
||
assertTrue( | ||
"Start and End Boundaries are different", | ||
bodyParts.last().startsWith("--") | ||
) | ||
assertEquals( | ||
"The last line of the body has to be empty", | ||
"\r\n", | ||
bodyParts.last().substring(startIndex = 2) | ||
) | ||
|
||
val parts = bodyParts | ||
.drop(1) | ||
.dropLast(1) | ||
.map { processPart(it) } | ||
|
||
return FileApiStats( | ||
boundary = boundary, | ||
parts = parts | ||
) | ||
} |