Skip to content

Commit

Permalink
fix: process correct class when clicking class in multi-class Kotlin …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
tangcent committed Jul 22, 2023
1 parent fd150c5 commit 5b02310
Show file tree
Hide file tree
Showing 74 changed files with 2,906 additions and 852 deletions.
3 changes: 2 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ ignore:
- "gradlew.bat"
- "**/*Dialog.kt"
- "**/*Configurable.kt"
- "**/*Action.kt"
- "**/*Action.kt"
- ".*model.*"
4 changes: 4 additions & 0 deletions .github/workflows/co.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
with:
java-version: ${{ matrix.java-version }}
distribution: 'zulu'
- name: Install Xvfb
run: sudo apt-get update && sudo apt-get install -y xvfb
- name: Start Xvfb
run: Xvfb :99 -screen 0 1024x768x24 &
- name: Generate coverage report
run: |
./gradlew check --stacktrace
Expand Down
18 changes: 15 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ tasks.create("codeCoverageReport", JacocoReport::class) {
executionData(
fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
)
subprojects.forEach {
sourceDirectories.from(it.file("src/main/kotlin"))
classDirectories.from(it.file("build/classes/kotlin/main"))

val exclusiveDirectories = listOf("**/common/model/**")

subprojects.forEach { project ->
sourceDirectories.from(project.files("src/main/kotlin").map {
fileTree(it).matching {
exclude(exclusiveDirectories)
}
})
classDirectories.from(project.files("build/classes/kotlin/main").map {
fileTree(it).matching {
exclude(exclusiveDirectories)
}
})
}

reports {
xml.required.set(true)
xml.outputLocation.set(file("${buildDir}/reports/jacoco/report.xml").apply { parentFile.mkdirs() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object HttpMethod {
if (method.isBlank()) {
return NO_METHOD
}
val standardMethod = method.toUpperCase()
val standardMethod = method.uppercase()
if (ALL_METHODS.contains(standardMethod)) {
return standardMethod
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ object KVUtils {
useFieldAsAttr(value, attr)
}
}

is Extensible -> {
model.getPropertyValue("value")?.let {
model.setExt(attr, it)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ fun Request.header(name: String): String? {
if (this.headers.isNullOrEmpty()) {
return null
}
val lowerName = name.toLowerCase()
val lowerName = name.lowercase()
return this.headers!!
.stream()
.filter { it.name?.toLowerCase() == lowerName }
.filter { it.name?.lowercase() == lowerName }
.map { it.value }
.firstOrNull()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,4 @@ object FileSizeUtils {
require(directory.exists()) { "$directory does not exist" }
require(directory.isDirectory) { "$directory is not a directory" }
}

}
110 changes: 68 additions & 42 deletions common-api/src/main/kotlin/com/itangcent/http/ApacheHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.config.SocketConfig
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
Expand All @@ -24,7 +25,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.impl.cookie.BasicClientCookie
import org.apache.http.impl.cookie.BasicClientCookie2
import org.apache.http.message.BasicNameValuePair
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.ssl.SSLContexts
import org.apache.http.util.toByteArray
import java.io.Closeable
import java.io.File
Expand All @@ -35,6 +36,16 @@ import javax.net.ssl.SSLContext

@ScriptTypeName("httpClient")
open class ApacheHttpClient : HttpClient {
companion object {
private fun String.createContentType(charset: String = "UTF-8"): ContentType {
val contentType = ContentType.parse(this)
return if (contentType.charset == null) {
contentType.withCharset(charset)
} else {
contentType
}
}
}

private val apacheCookieStore: ApacheCookieStore

Expand All @@ -47,25 +58,25 @@ open class ApacheHttpClient : HttpClient {
this.apacheCookieStore = ApacheCookieStore(basicCookieStore)
this.httpClientContext!!.cookieStore = basicCookieStore
this.httpClient = HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManager().also {
it.maxTotal = 50
it.defaultMaxPerRoute = 20
})
.setDefaultSocketConfig(
SocketConfig.custom()
.setSoTimeout(30 * 1000)
.build()
)
.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectTimeout(30 * 1000)
.setConnectionRequestTimeout(30 * 1000)
.setSocketTimeout(30 * 1000)
.setCookieSpec(CookieSpecs.STANDARD).build()
)
.setSSLHostnameVerifier(NOOP_HOST_NAME_VERIFIER)
.setSSLSocketFactory(SSLSF)
.build()
.setConnectionManager(PoolingHttpClientConnectionManager().also {
it.maxTotal = 50
it.defaultMaxPerRoute = 20
})
.setDefaultSocketConfig(
SocketConfig.custom()
.setSoTimeout(30 * 1000)
.build()
)
.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectTimeout(30 * 1000)
.setConnectionRequestTimeout(30 * 1000)
.setSocketTimeout(30 * 1000)
.setCookieSpec(CookieSpecs.STANDARD).build()
)
.setSSLHostnameVerifier(NOOP_HOST_NAME_VERIFIER)
.setSSLSocketFactory(SSLSF)
.build()
}

constructor(httpClient: org.apache.http.client.HttpClient) {
Expand Down Expand Up @@ -102,13 +113,13 @@ open class ApacheHttpClient : HttpClient {
}

val requestBuilder = RequestBuilder.create(request.method())
.setUri(url)
.setUri(url)

request.headers()?.forEach {
requestBuilder.addHeader(it.name(), it.value())
}

if (request.method().toUpperCase() != "GET") {
if (request.method().uppercase() != "GET") {
var requestEntity: HttpEntity? = null
if (request.params().notNullOrEmpty()) {
if (request.contentType()?.startsWith("application/x-www-form-urlencoded") == true) {
Expand Down Expand Up @@ -141,15 +152,31 @@ open class ApacheHttpClient : HttpClient {
requestEntity = entityBuilder.build()
}
}
if (request.body() != null) {
val body = request.body()
if (body != null) {
if (requestEntity != null) {
SpiUtils.loadService(ILogger::class)
?.warn("The request with a body should not set content-type:${request.contentType()}")
?.warn("The request with a body should not set content-type:${request.contentType()}")
}
requestEntity = when (body) {
is HttpEntity -> {
body
}

is String -> {
StringEntity(
body,
request.contentType()?.createContentType() ?: ContentType.APPLICATION_JSON
)
}

else -> {
StringEntity(
body.toJson(),
ContentType.APPLICATION_JSON
)
}
}
requestEntity = StringEntity(
request.body().toJson(),
ContentType.APPLICATION_JSON
)
}
if (requestEntity != null) {
requestBuilder.entity = requestEntity
Expand Down Expand Up @@ -218,7 +245,7 @@ class ApacheCookieStore : CookieStore {
*/
override fun addCookies(cookies: Array<Cookie>?) {
cookies?.map { cookie -> cookie.asApacheCookie() }
?.forEach { cookieStore.addCookie(it) }
?.forEach { cookieStore.addCookie(it) }
}

/**
Expand Down Expand Up @@ -247,8 +274,8 @@ class ApacheCookieStore : CookieStore {
*/
@ScriptTypeName("response")
class ApacheHttpResponse(
private val request: HttpRequest,
private val response: org.apache.http.HttpResponse
private val request: HttpRequest,
private val response: org.apache.http.HttpResponse
) : AbstractHttpResponse() {

/**
Expand Down Expand Up @@ -393,11 +420,11 @@ fun Cookie.asApacheCookie(): org.apache.http.cookie.Cookie {
return this.getWrapper()
}
val cookie =
if (this.getPorts() == null || this.getCommentURL() == null) {
BasicClientCookie(this.getName(), this.getValue())
} else {
BasicClientCookie2(this.getName(), this.getValue())
}
if (this.getPorts() == null || this.getCommentURL() == null) {
BasicClientCookie(this.getName(), this.getValue())
} else {
BasicClientCookie2(this.getName(), this.getValue())
}
cookie.comment = this.getComment()
cookie.domain = this.getDomain()
cookie.path = this.getPath()
Expand All @@ -412,14 +439,13 @@ fun Cookie.asApacheCookie(): org.apache.http.cookie.Cookie {
return cookie
}

var SSLCONTEXT: SSLContext = SSLContextBuilder().loadTrustMaterial(null
) { _, _ -> true }.build()
var SSLCONTEXT: SSLContext = SSLContexts.createSystemDefault()

/**
* Never authenticate the host
*/
val NOOP_HOST_NAME_VERIFIER: HostnameVerifier = HostnameVerifier { _, _ -> true }
val NOOP_HOST_NAME_VERIFIER: HostnameVerifier = NoopHostnameVerifier.INSTANCE

var SSLSF: SSLConnectionSocketFactory = SSLConnectionSocketFactory(
SSLCONTEXT, arrayOf("TLSv1", "TLSv1.1", "TLSv1.2"), null,
NOOP_HOST_NAME_VERIFIER)
val SSLSF: SSLConnectionSocketFactory = SSLConnectionSocketFactory(
SSLCONTEXT, NOOP_HOST_NAME_VERIFIER
)
4 changes: 2 additions & 2 deletions common-api/src/main/kotlin/com/itangcent/http/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ interface HttpResponse : Closeable {
* @param headerName the header name to find
* @return {@code true} if at least one header with the name be found, {@code false} otherwise
*/
fun containsHeader(headerName: String): Boolean?
fun containsHeader(headerName: String): Boolean

/**
* Gets all of the headers with the given name. The returned array
Expand Down Expand Up @@ -1026,7 +1026,7 @@ abstract class AbstractHttpResponse : HttpResponse {
* @param headerName the header name to find
* @return {@code true} if at least one header with the name be found, {@code false} otherwise
*/
override fun containsHeader(headerName: String): Boolean? {
override fun containsHeader(headerName: String): Boolean {
return headers()?.any { it.name().equalIgnoreCase(headerName) } ?: false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
package com.itangcent.common.constant

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

/**
* Test case for [HttpMethod]
*/
internal class HttpMethodTest {

@Test
fun preferMethod() {

Assertions.assertEquals("ALL", HttpMethod.preferMethod(""))

Assertions.assertEquals("GET", HttpMethod.preferMethod("GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("XXX.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("XXX.POST"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("POST.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("GET.POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("POST_GET"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("GET_POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET]"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("[POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET][POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[POST][GET]"))
fun `test preferMethod function`() {

assertEquals("ALL", HttpMethod.preferMethod(""))
assertEquals("ALL", HttpMethod.preferMethod("foo"))

assertEquals("GET", HttpMethod.preferMethod("GET"))
assertEquals("POST", HttpMethod.preferMethod("POST"))
assertEquals("DELETE", HttpMethod.preferMethod("DELETE"))
assertEquals("PUT", HttpMethod.preferMethod("PUT"))
assertEquals("PATCH", HttpMethod.preferMethod("PATCH"))
assertEquals("OPTIONS", HttpMethod.preferMethod("OPTIONS"))
assertEquals("TRACE", HttpMethod.preferMethod("TRACE"))
assertEquals("HEAD", HttpMethod.preferMethod("HEAD"))

assertEquals("GET", HttpMethod.preferMethod("get"))
assertEquals("POST", HttpMethod.preferMethod("post"))
assertEquals("DELETE", HttpMethod.preferMethod("delete"))
assertEquals("PUT", HttpMethod.preferMethod("put"))
assertEquals("PATCH", HttpMethod.preferMethod("patch"))
assertEquals("OPTIONS", HttpMethod.preferMethod("options"))
assertEquals("TRACE", HttpMethod.preferMethod("trace"))
assertEquals("HEAD", HttpMethod.preferMethod("head"))

assertEquals("GET", HttpMethod.preferMethod("XXX.GET"))
assertEquals("POST", HttpMethod.preferMethod("XXX.POST"))
assertEquals("GET", HttpMethod.preferMethod("POST.GET"))
assertEquals("POST", HttpMethod.preferMethod("GET.POST"))

assertEquals("GET", HttpMethod.preferMethod("POST_GET"))
assertEquals("GET", HttpMethod.preferMethod("GET_POST"))

assertEquals("GET", HttpMethod.preferMethod("[GET]"))
assertEquals("POST", HttpMethod.preferMethod("[POST]"))
assertEquals("GET", HttpMethod.preferMethod("[GET][POST]"))
assertEquals("GET", HttpMethod.preferMethod("[POST][GET]"))
}
}
Loading

0 comments on commit 5b02310

Please sign in to comment.