|
| 1 | +package com.papsign.ktor.openapigen |
| 2 | + |
| 3 | +import TestServer.setupBaseTestServer |
| 4 | +import com.papsign.ktor.openapigen.content.type.multipart.FormDataRequest |
| 5 | +import com.papsign.ktor.openapigen.content.type.multipart.FormDataRequestType |
| 6 | +import com.papsign.ktor.openapigen.content.type.multipart.NamedFileInputStream |
| 7 | +import com.papsign.ktor.openapigen.route.apiRouting |
| 8 | +import com.papsign.ktor.openapigen.route.path.normal.post |
| 9 | +import com.papsign.ktor.openapigen.route.response.respond |
| 10 | +import com.papsign.ktor.openapigen.route.route |
| 11 | +import io.ktor.application.* |
| 12 | +import io.ktor.http.* |
| 13 | +import io.ktor.server.testing.* |
| 14 | +import org.junit.Assert.assertEquals |
| 15 | +import org.junit.Assert.assertTrue |
| 16 | +import org.junit.Test |
| 17 | + |
| 18 | +internal class FormDocumentationGenerationTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + fun formDataTestRequest() = withTestApplication({ |
| 22 | + setupBaseTestServer() |
| 23 | + apiRouting { |
| 24 | + route("form-data"){ |
| 25 | + post<Unit, TestServer.StringResponse, FormData>{ _, _ -> |
| 26 | + respond(TestServer.StringResponse("result")) |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + }) { |
| 31 | + with(handleRequest(HttpMethod.Get, "//openapi.json")) { |
| 32 | + this@withTestApplication.application.log.debug(response.content) |
| 33 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 34 | + assertTrue( |
| 35 | + response.content!!.contains( |
| 36 | + """ "paths" : { |
| 37 | + "/form-data" : { |
| 38 | + "post" : { |
| 39 | + "requestBody" : { |
| 40 | + "content" : { |
| 41 | + "application/x-www-form-urlencoded" : { |
| 42 | + "schema" : { |
| 43 | + "${"$"}ref" : "#/components/schemas/FormData" |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + },""" |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun multipartFormDataTestRequest() = withTestApplication({ |
| 56 | + setupBaseTestServer() |
| 57 | + apiRouting { |
| 58 | + route("multipart-data"){ |
| 59 | + post<Unit, TestServer.StringResponse, MultiPartForm>{ _, _ -> |
| 60 | + respond(TestServer.StringResponse("result")) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }) { |
| 65 | + with(handleRequest(HttpMethod.Get, "//openapi.json")) { |
| 66 | + this@withTestApplication.application.log.debug(response.content) |
| 67 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 68 | + assertTrue( |
| 69 | + response.content!!.contains( |
| 70 | + """ "paths" : { |
| 71 | + "/multipart-data" : { |
| 72 | + "post" : { |
| 73 | + "requestBody" : { |
| 74 | + "content" : { |
| 75 | + "multipart/form-data" : { |
| 76 | + "schema" : { |
| 77 | + "${"$"}ref" : "#/components/schemas/MultiPartForm" |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + },""" |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun defaultFormDataTestRequest() = withTestApplication({ |
| 90 | + setupBaseTestServer() |
| 91 | + apiRouting { |
| 92 | + route("default-form-data"){ |
| 93 | + post<Unit, TestServer.StringResponse, DefaultFormData>{ _, _ -> |
| 94 | + respond(TestServer.StringResponse("result")) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }) { |
| 99 | + with(handleRequest(HttpMethod.Get, "//openapi.json")) { |
| 100 | + this@withTestApplication.application.log.debug(response.content) |
| 101 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 102 | + assertTrue( |
| 103 | + response.content!!.contains( |
| 104 | + """ "paths" : { |
| 105 | + "/default-form-data" : { |
| 106 | + "post" : { |
| 107 | + "requestBody" : { |
| 108 | + "content" : { |
| 109 | + "multipart/form-data" : { |
| 110 | + "schema" : { |
| 111 | + "${"$"}ref" : "#/components/schemas/DefaultFormData" |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + },""" |
| 116 | + ) |
| 117 | + ) |
| 118 | + |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +@FormDataRequest(type = FormDataRequestType.MULTIPART) |
| 124 | +data class MultiPartForm(val userId: String, val file: NamedFileInputStream) |
| 125 | + |
| 126 | +@FormDataRequest(type = FormDataRequestType.URL_ENCODED) |
| 127 | +data class FormData(val login: String, val password: String) |
| 128 | + |
| 129 | +@FormDataRequest |
| 130 | +data class DefaultFormData(val login: String, val password: String) |
0 commit comments