-
Notifications
You must be signed in to change notification settings - Fork 343
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
Showing
4 changed files
with
222 additions
and
2 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
59 changes: 59 additions & 0 deletions
59
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/common.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,59 @@ | ||
/* | ||
* Copyright 2023 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/master/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.api.http.adapter.http.router | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonElement | ||
import net.mamoe.mirai.api.http.adapter.http.plugin.forward | ||
|
||
@Serializable | ||
internal data class CommonRouter( | ||
val router: String, | ||
val body: JsonElement? | ||
) | ||
|
||
internal fun Application.commonRouter() = routing { | ||
|
||
route("/router") { | ||
get("/{pathRouter}") { | ||
val router = call.parameters["pathRouter"] ?: return@get | ||
call.forward(router + "?" + call.request.queryString()) | ||
} | ||
|
||
get { | ||
val router = call.request.queryParameters["router"] ?: return@get | ||
call.forward(router + "?" + call.request.queryString()) | ||
} | ||
|
||
post("/{router}") { | ||
call.forward(call.parameters["router"] ?: "") | ||
} | ||
|
||
post { | ||
val router = call.receive<CommonRouter>() | ||
call.forward(router.router, router.body) | ||
} | ||
} | ||
|
||
route("/echo") { | ||
get { | ||
call.respondText(call.request.queryString()) | ||
} | ||
|
||
post { | ||
call.respondText(call.receive<String>()) | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...api-http/src/test/kotlin/net/mamoe/mirai/api/http/adapter/http/router/CommonRouterTest.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,114 @@ | ||
/* | ||
* Copyright 2023 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/master/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.api.http.adapter.http.router | ||
|
||
import framework.testHttpApplication | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import net.mamoe.mirai.api.http.adapter.common.StateCode | ||
import net.mamoe.mirai.api.http.adapter.internal.dto.LongListRestfulResult | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class CommonRouterTest { | ||
|
||
@Test | ||
fun testGetRouterWithPath() = testHttpApplication { | ||
|
||
client.get("/router/botList").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
|
||
val body = it.body<LongListRestfulResult>() | ||
assertEquals(StateCode.Success.code, body.code) | ||
assertTrue(body.data.isEmpty()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetRouterWithQuery() = testHttpApplication { | ||
client.get("/router?router=botList").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
|
||
val body = it.body<LongListRestfulResult>() | ||
assertEquals(StateCode.Success.code, body.code) | ||
assertTrue(body.data.isEmpty()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetRouterWithPathAndQuery() = testHttpApplication { | ||
client.get("/router/xxx?router=botList").also { | ||
assertEquals(HttpStatusCode.NotFound, it.status) | ||
println(it.bodyAsText()) | ||
} | ||
|
||
client.get("/router/botList?router=xxx").also { | ||
val body = it.body<LongListRestfulResult>() | ||
assertEquals(StateCode.Success.code, body.code) | ||
assertTrue(body.data.isEmpty()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetRouterPassQuery() = testHttpApplication { | ||
client.get("/router/echo?qq=123").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("qq=123", it.bodyAsText()) | ||
} | ||
|
||
client.get("/router?router=echo&qq=123").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("router=echo&qq=123", it.bodyAsText()) | ||
} | ||
|
||
client.get("/router?router=/echo&qq=123").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("router=/echo&qq=123", it.bodyAsText()) | ||
} | ||
|
||
client.get("/router?router=%2Fecho&qq=123").also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("router=%2Fecho&qq=123", it.bodyAsText()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testPostRouter() = testHttpApplication { | ||
client.post { | ||
url("/router/echo?qq=123") | ||
setBody("hello world") | ||
}.also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("hello world", it.bodyAsText()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testPostRouterWithJson() = testHttpApplication { | ||
client.post("/router") { | ||
contentType(ContentType.Application.Json) | ||
setBody("""{"router": "echo", "body": "hello world"}""") | ||
}.also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("\"hello world\"", it.bodyAsText()) | ||
} | ||
|
||
client.post("/router") { | ||
contentType(ContentType.Application.Json) | ||
setBody("""{"router": "echo", "body": null}""") | ||
}.also { | ||
assertEquals(HttpStatusCode.OK, it.status) | ||
assertEquals("null", it.bodyAsText()) | ||
} | ||
} | ||
} |