Skip to content

Commit

Permalink
Merge pull request hexagontk#629 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Add utility methods and fix parent POM
  • Loading branch information
jaguililla authored May 18, 2023
2 parents 78c6cb2 + a2f5321 commit 0d42043
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 42 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.warning.mode=all
org.gradle.console=plain

# Gradle
version=2.8.5
version=2.8.6
group=com.hexagonkt
description=The atoms of your platform

Expand Down
27 changes: 27 additions & 0 deletions http/src/main/kotlin/com/hexagonkt/http/model/HttpRequestPort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ interface HttpRequestPort : HttpMessage {
certificateChain: List<X509Certificate> = this.certificateChain,
): HttpRequestPort

operator fun plus(header: Header): HttpRequestPort =
with(headers = headers + header)

operator fun plus(queryParameter: QueryParameter): HttpRequestPort =
with(queryParameters = queryParameters + queryParameter)

operator fun plus(part: HttpPart): HttpRequestPort =
with(parts = parts + part)

operator fun plus(formParameter: FormParameter): HttpRequestPort =
with(formParameters = formParameters + formParameter)

operator fun plus(cookie: Cookie): HttpRequestPort =
with(cookies = cookies + cookie)

operator fun plus(headers: Headers): HttpRequestPort =
with(headers = this.headers + headers)

operator fun plus(queryParameters: QueryParameters): HttpRequestPort =
with(queryParameters = this.queryParameters + queryParameters)

operator fun plus(parts: List<HttpPart>): HttpRequestPort =
with(parts = this.parts + parts)

operator fun plus(formParameters: FormParameters): HttpRequestPort =
with(formParameters = this.formParameters + formParameters)

fun certificate(): X509Certificate? =
certificateChain.firstOrNull()

Expand Down
12 changes: 12 additions & 0 deletions http/src/main/kotlin/com/hexagonkt/http/model/HttpResponsePort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,16 @@ interface HttpResponsePort : HttpMessage {
onPong: WsSession.(data: ByteArray) -> Unit = this.onPong,
onClose: WsSession.(status: Int, reason: String) -> Unit = this.onClose,
): HttpResponsePort

operator fun plus(header: Header): HttpResponsePort =
with(headers = headers + header)

operator fun plus(cookie: Cookie): HttpResponsePort =
with(cookies = cookies + cookie)

operator fun plus(headers: Headers): HttpResponsePort =
with(headers = this.headers + headers)

operator fun plus(cookies: List<Cookie>): HttpResponsePort =
with(cookies = this.cookies + cookies)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.hexagonkt.http.patterns

import com.hexagonkt.core.assertEnabled
import com.hexagonkt.http.patterns.TemplatePathPattern.Companion.WILDCARD

fun createPathPattern(pattern: String, prefix: Boolean): PathPattern =
when {
pattern == WILDCARD -> WildcardPathPattern(prefix)
TemplatePathPattern.isTemplate(pattern) -> TemplatePathPattern(pattern, prefix)
else -> LiteralPathPattern(pattern, prefix)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class TemplatePathPattern(
private const val PARAMETER_PREFIX = "{"
private const val PARAMETER_SUFFIX = "}"

private const val WILDCARD = "*"
internal const val WILDCARD = "*"
private const val PARAMETER = "\\$PARAMETER_PREFIX\\w+$PARAMETER_SUFFIX"

private val REGEX_CHARACTERS = listOf('(', ')', '|', '?', '+', '[', ']')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hexagonkt.http.patterns

import com.hexagonkt.http.patterns.TemplatePathPattern.Companion.WILDCARD

data class WildcardPathPattern(override val prefix: Boolean = false) : PathPattern {

override val pattern: String = WILDCARD

override fun addPrefix(prefix: String?): PathPattern =
if (prefix == null) this
else createPathPattern("$prefix$WILDCARD", this.prefix)

override fun matches(requestUrl: String): Boolean =
true

override fun extractParameters(requestUrl: String): Map<String, String> =
mapOf(1.toString() to requestUrl)
}
50 changes: 50 additions & 0 deletions http/src/test/kotlin/com/hexagonkt/http/model/HttpRequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,54 @@ internal class HttpRequestTest {
testQueryParameters = QueryParameters()
assertEquals(URL("http://localhost:9999/path"), TestRequest.url())
}

@Test fun `HTTP Request operators work ok`() {
val httpRequest = httpRequestData()

val header = Header("h", "v")
assertEquals(
httpRequest + header,
httpRequest.copy(headers = httpRequest.headers + header)
)
assertEquals(
httpRequest + Headers(header),
httpRequest.copy(headers = httpRequest.headers + header)
)

val queryParameter = QueryParameter("h", "v")
assertEquals(
httpRequest + queryParameter,
httpRequest.copy(queryParameters = httpRequest.queryParameters + queryParameter)
)
assertEquals(
httpRequest + QueryParameters(queryParameter),
httpRequest.copy(queryParameters = httpRequest.queryParameters + queryParameter)
)

val httpPart = HttpPart("h", "v")
assertEquals(
httpRequest + httpPart,
httpRequest.copy(parts = httpRequest.parts + httpPart)
)
assertEquals(
httpRequest + listOf(httpPart),
httpRequest.copy(parts = httpRequest.parts + httpPart)
)

val formParameter = FormParameter("h", "v")
assertEquals(
httpRequest + formParameter,
httpRequest.copy(formParameters = httpRequest.formParameters + formParameter)
)
assertEquals(
httpRequest + FormParameters(formParameter),
httpRequest.copy(formParameters = httpRequest.formParameters + formParameter)
)

val cookie = Cookie("n", "v")
assertEquals(
httpRequest + cookie,
httpRequest.copy(cookies = httpRequest.cookies + cookie)
)
}
}
44 changes: 34 additions & 10 deletions http/src/test/kotlin/com/hexagonkt/http/model/HttpResponseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,50 @@ internal class HttpResponseTest {
)

@Test fun `HTTP Response comparison works ok`() {
val httpRequest = httpResponseData()
val httpResponse = httpResponseData()

assertEquals(httpRequest, httpRequest)
assertEquals(httpResponse, httpResponse)
assertEquals(httpResponseData(), httpResponseData())
assertFalse(httpRequest.equals(""))
assertFalse(httpResponse.equals(""))

val headers = Headers(Header("h1", "v1"))
val cookies = listOf(Cookie("p", "v"))
val contentType = ContentType(TEXT_RICHTEXT)

assertNotEquals(httpRequest, httpRequest.with(body = "body"))
assertNotEquals(httpRequest, httpRequest.with(headers = headers))
assertNotEquals(httpRequest, httpRequest.with(contentType = contentType))
assertNotEquals(httpRequest, httpRequest.with(cookies = cookies))
assertNotEquals(httpRequest, httpRequest.with(status = OK_200))
assertNotEquals(httpResponse, httpResponse.with(body = "body"))
assertNotEquals(httpResponse, httpResponse.with(headers = headers))
assertNotEquals(httpResponse, httpResponse.with(contentType = contentType))
assertNotEquals(httpResponse, httpResponse.with(cookies = cookies))
assertNotEquals(httpResponse, httpResponse.with(status = OK_200))

assertEquals(httpRequest.hashCode(), httpResponseData().hashCode())
assertEquals(httpResponse.hashCode(), httpResponseData().hashCode())
assertEquals(
httpRequest.copy(contentType = null).hashCode(),
httpResponse.copy(contentType = null).hashCode(),
httpResponseData(null).hashCode()
)
}

@Test fun `HTTP Response operators work ok`() {
val httpResponse = httpResponseData()

val header = Header("h", "v")
assertEquals(
httpResponse + header,
httpResponse.copy(headers = httpResponse.headers + header)
)
assertEquals(
httpResponse + Headers(header),
httpResponse.copy(headers = httpResponse.headers + header)
)

val cookie = Cookie("n", "v")
assertEquals(
httpResponse + cookie,
httpResponse.copy(cookies = httpResponse.cookies + cookie)
)
assertEquals(
httpResponse + listOf(cookie),
httpResponse.copy(cookies = httpResponse.cookies + cookie)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hexagonkt.http.patterns

import kotlin.test.Test
import kotlin.test.*

internal class WildcardPathPatternTest {

@Test fun `Prefixes are matched if pattern is prefix`() {
val regexPath = WildcardPathPattern()
assert(regexPath.matches("/alpha/bravo/tango"))

val regexPathPrefix = WildcardPathPattern(true)
assert(regexPathPrefix.matches("/alpha/bravo/tango"))
}

@Test fun `Prefixes can be appended to patterns`() {
val regexPath = WildcardPathPattern()
assert(regexPath.addPrefix(null).matches("/alpha/bravo"))
assertFalse(regexPath.addPrefix("/prefix").matches("/alpha/bravo"))
assertTrue(regexPath.addPrefix("/prefix").matches("/prefix/alpha/bravo"))
}

@Test fun `A wildcard path have a single parameter`() {
val pathWithoutData = WildcardPathPattern()
assertEquals("*", pathWithoutData.pattern)
assertEquals(mapOf("1" to "/alpha"), pathWithoutData.extractParameters("/alpha"))
}

@Test fun `Adding a prefix keep the prefix flag`() {
WildcardPathPattern(true).addPrefix("/b").let {
assertEquals("/b*", it.pattern)
assertTrue(it.prefix)
}
WildcardPathPattern(false).addPrefix("/b").let {
assertEquals("/b*", it.pattern)
assertFalse(it.prefix)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ abstract class ClientTest(
callback = {
val contentType = ContentType(APPLICATION_JSON, charset = Charsets.UTF_8)
val bodyString = request.bodyString()
val bodyHeader = if (bodyString.endsWith("\n") || bodyString.contains("{")) "json" else bodyString
val bodyHeader =
if (bodyString.endsWith("\n") || bodyString.contains("{")) "json"
else bodyString

ok(
body = bodyString,
headers = response.headers
Expand All @@ -85,9 +88,7 @@ abstract class ClientTest(

@Test fun `Form parameters are sent correctly`() {
callback = {
val headers = Headers(
formParameters.httpFields.map { (k, v) -> Header(k, v.values) }
)
val headers = Headers(formParameters.httpFields.map { (k, v) -> Header(k, v.values) })
ok(headers = headers)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,24 +377,24 @@ abstract class SamplesTest(

val server = HttpServer(serverAdapter()) {
// filters
on("/*") { send(headers = response.headers + Header("b-all", "true")) }
on("/*") { send(response + Header("b-all", "true")) }

on("/filters/*") { send(headers = response.headers + Header("b-filters", "true")) }
on("/filters/*") { send(response + Header("b-filters", "true")) }
get("/filters/route") { ok("filters route") }
after("/filters/*") { send(headers = response.headers + Header("a-filters", "true")) }
after("/filters/*") { send(response + Header("a-filters", "true")) }

get("/filters") { ok("filters") }

path("/nested") {
on("*") { send(headers = response.headers + Header("b-nested", "true")) }
on { send(headers = response.headers + Header("b-nested-2", "true")) }
on("*") { send(response + Header("b-nested", "true")) }
on { send(response + Header("b-nested-2", "true")) }
get("/filters") { ok("nested filters") }
get("/halted") { send(HttpStatus(499), "halted") }
get { ok("nested also") }
after("*") { send(headers = response.headers + Header("a-nested", "true")) }
after("*") { send(response + Header("a-nested", "true")) }
}

after("/*") { send(headers = response.headers + Header("a-all", "true")) }
after("/*") { send(response + Header("a-all", "true")) }
// filters
}

Expand Down
33 changes: 14 additions & 19 deletions starters/kotlin_pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,21 @@
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
Expand Down Expand Up @@ -123,7 +118,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
<configuration>
<archive>
<manifest>
Expand Down

0 comments on commit 0d42043

Please sign in to comment.