Add EchoServerResponseParser.kt to Kotlin Echo API client (#17887)

* add kotlin jvm retrofit2 coroutine echo client

* add tests for echo server parser

* add workflow

* remove coroutines sample
This commit is contained in:
William Cheng 2024-02-19 10:52:58 +08:00 committed by GitHub
parent 7d539bcb78
commit 28b75377b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,50 @@
name: Kotlin Client (Echo API) JDK17
on:
push:
paths:
- samples/client/echo_api/kotlin**
pull_request:
paths:
- samples/client/echo_api/kotlin**
env:
GRADLE_VERSION: 7.4
jobs:
build:
name: Build Kotlin Client JDK17
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sample:
# clients
- samples/client/echo_api/kotlin-jvm-spring-3-restclient
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 17
- name: Cache maven dependencies
uses: actions/cache@v4
env:
cache-name: maven-repository
with:
path: |
~/.gradle
key: ${{ runner.os }}-${{ github.job }}-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }}
- name: Install Gradle wrapper
uses: eskatos/gradle-command-action@v3
with:
gradle-version: ${{ env.GRADLE_VERSION }}
build-root-directory: ${{ matrix.sample }}
arguments: wrapper
- name: Setup node.js
uses: actions/setup-node@v4
- name: Run echo server
run: |
git clone https://github.com/wing328/http-echo-server -b openapi-generator-test-server
(cd http-echo-server && npm install && npm start &)
- name: Build
working-directory: ${{ matrix.sample }}
run: ./gradlew build

1
.gitignore vendored
View File

@ -212,6 +212,7 @@ samples/server/petstore/kotlin-springboot/build
samples/client/petstore/kotlin*/src/main/kotlin/test/
samples/client/petstore/kotlin*/build/
samples/server/others/kotlin-server/jaxrs-spec/build/
samples/client/echo_api/kotlin-jvm-spring-3-restclient/build/
# haskell
.stack-work

View File

@ -0,0 +1,74 @@
package org.openapitools.client
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import org.openapitools.client.apis.BodyApi
import org.openapitools.client.apis.QueryApi
import org.openapitools.client.models.Category
import org.openapitools.client.models.Pet
import org.openapitools.client.models.Tag
class PetApiTest : ShouldSpec() {
init {
val petId:Long = 10006
val queryApi = QueryApi("http://localhost:3000")
val bodyApi = BodyApi("http://localhost:3000")
should("return a pet") {
val pet : Pet = Pet(
id = petId,
name = "kotlin client test",
photoUrls = listOf("http://test_kotlin_unit_test.com"),
category = Category(petId, "test kotlin category"),
tags = listOf(Tag(petId, "test kotlin tag"))
)
/* comment out the following as for some reasons http request body contains
a line "cc" before the JSON payload, e.g.
Echo mode switched on for this request
--> POST /echo/body/Pet HTTP/1.1
--> Connection: Upgrade, HTTP2-Settings
--> Host: localhost:3000
--> HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA
--> Transfer-encoding: chunked
--> Upgrade: h2c
--> User-Agent: Java-http-client/17.0.10
--> Accept: application/json
--> Content-Type: application/json
-->
-->
Found the content-type: application/json
Found the blank line before the response body
<--
[socket#8] event: data
--> cc
--> {"name":"kotlin client test","photoUrls":["http://test_kotlin_unit_test.com"],"id":10006,"category":{"id":10006,"name":"test kotlin category"},"tags":[{"id":10006,"name":"test kotlin tag"}],"status":null}
-->
<-- cc
<-- {"name":"kotlin client test","photoUrls":["http://test_kotlin_unit_test.com"],"id":10006,"category":{"id":10006,"name":"test kotlin category"},"tags":[{"id":10006,"name":"test kotlin tag"}],"status":null}
<--
[socket#8] event: data
val result : Pet = bodyApi.testEchoBodyPet(pet)
result.id shouldBe (petId)
result.name shouldBe ("kotlin client test")
result.photoUrls[0] shouldBe ("http://test_kotlin_unit_test.com")
result.category!!.id shouldBe (petId)
result.category!!.name shouldBe ("test kotlin category")
result.tags!![0].id shouldBe (petId)
result.tags!![0].name shouldBe ("test kotlin tag")
*/
}
should("test echo server response parser") {
val response: String = queryApi.testQueryStyleFormExplodeTrueArrayString()
val result = EchoServerResponseParser(response)
result.path shouldBe ("/query/style_form/explode_true/array_string")
result.method shouldBe ("GET")
result.body shouldBe ("")
}
}
}

View File

@ -0,0 +1,44 @@
package org.openapitools.client
class EchoServerResponseParser(response: String) {
lateinit var method: String
lateinit var path: String
lateinit var protocol: String
val headers = hashMapOf<String, String>()
val body: String
init {
require(response.isNotEmpty()) { "Echo server response cannot be null" }
val lines = response.lineSequence().iterator()
var firstLine = true
var bodyStart = false
val bodyBuilder = StringBuilder()
for (line in lines) {
if (firstLine) {
val items = line.split(" ")
method = items[0]
path = items[1]
protocol = items[2]
firstLine = false
continue
}
if (bodyStart) {
bodyBuilder.append(line)
bodyBuilder.append("\n")
}
if (line.isEmpty()) {
bodyStart = true
continue
}
val keyValue = line.split(": ")
if (keyValue.size == 2) {
headers[keyValue[0]] = keyValue[1]
}
}
body = bodyBuilder.toString().trimEnd()
}
}