Skip to content

Commit

Permalink
Merge pull request #652 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Update documentation and logging
  • Loading branch information
jaguililla committed Aug 26, 2023
2 parents 75e6a3d + 369e7a0 commit 589efef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.warning.mode=all
org.gradle.console=plain

# Gradle
version=3.0.2
version=3.0.3
group=com.hexagonkt
description=The atoms of your platform

Expand Down Expand Up @@ -37,7 +37,7 @@ mockkVersion=1.13.7
junitVersion=5.10.0
gatlingVersion=3.9.5
jmhVersion=1.37
mkdocsMaterialVersion=9.2.3
mkdocsMaterialVersion=9.2.4
mermaidDokkaVersion=0.4.4
nativeToolsVersion=0.9.24

Expand Down
14 changes: 12 additions & 2 deletions http/http/src/main/kotlin/com/hexagonkt/http/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.hexagonkt.http

import com.hexagonkt.core.assertEnabled
import com.hexagonkt.core.Jvm
import com.hexagonkt.core.logging.Logger
import com.hexagonkt.core.media.MediaType
import com.hexagonkt.http.model.*
import java.lang.IllegalStateException
import java.math.BigInteger
import java.net.URLDecoder
import java.net.URLEncoder
Expand All @@ -22,6 +24,8 @@ val BODY_TYPES = setOf(String::class, ByteArray::class, Int::class, Long::class)

val BODY_TYPES_NAMES = BODY_TYPES.joinToString(", ") { it.simpleName.toString() }

private val logger: Logger = Logger(SslSettings::class.java.packageName)

fun checkHeaders(headers: Headers) {
if (!assertEnabled)
return
Expand Down Expand Up @@ -124,6 +128,12 @@ fun bodyToBytes(body: Any): ByteArray =
is ByteArray -> body
is Int -> BigInteger.valueOf(body.toLong()).toByteArray()
is Long -> BigInteger.valueOf(body).toByteArray()
else ->
error("Unsupported body type: ${body.javaClass.simpleName}. Must be: $BODY_TYPES_NAMES")
else -> {
val className = body.javaClass.simpleName
val message = "Unsupported body type: $className. Must be: $BODY_TYPES_NAMES"
val exception = IllegalStateException(message)

logger.error(exception)
throw exception
}
}
5 changes: 5 additions & 0 deletions http/http_handlers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ feature (as [http_server_jetty]) in order to create an HTTP server.

[http_server_jetty]: /http_server_jetty

# Chaining changes
The response can be modified chaining `send` calls (or its utility methods). However, If calls are
not chained, only the last one will be applied. I.e.: `send().send()` will apply both calls changes,
while `send(); send()` will pass only the last send method result.

# Context on HTTP processing
An HTTP server is nothing more than a function that takes a request and returns a response. Requests
and responses comply with several Web standards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import java.net.InetAddress
import java.net.URI
import kotlin.test.assertEquals

@TestInstance(PER_CLASS)
Expand Down Expand Up @@ -240,20 +241,38 @@ abstract class SamplesTest(

// callbackResponse
get("/response") {
response.body // Get response content
response.status // Get the response status
response.contentType // Get the content type
response.body // Get response content
response.status // Get the response status
response.contentType // Get the content type

status // Shortcut of `response.status`
status // Shortcut of `response.status`

send(
status = UNAUTHORIZED_401, // Set status code to 401
body = "Hello", // Sets content to Hello
contentType = ContentType(APPLICATION_XML), // Set application/xml content type
headers = response.headers
+ Header("foo", "bar") // Sets header FOO with single value bar
+ Header("baz", "1", "2") // Sets header FOO values with [ bar ]
+ Header("foo", "bar") // Sets header FOO with single value bar
+ Header("baz", "1", "2") // Sets header FOO values with [ bar ]
)

// Utility methods for generating common responses
unauthorized("401: authorization missing")
forbidden("403: access not granted")
internalServerError("500: server error")
serverError(NOT_IMPLEMENTED_501, RuntimeException("Error"))
ok("Correct")
badRequest("400: incorrect request")
notFound("404: Missing resource")
created("201: Created")
redirect(FOUND_302, URI("/location"))

// The response can be modified chaining send calls (or its utility methods)
ok("Replacing headers").send(headers = Headers())

// If calls are not chained, only the last one will be applied
ok("Intending to replace headers")
send(headers = Headers()) // This will be passed, but previous ok will be ignored
}
// callbackResponse

Expand Down Expand Up @@ -338,7 +357,7 @@ abstract class SamplesTest(
assertEquals("Invalid request", callResponse.body)
assertEquals(OK_200, it.get("/request", body = "body", contentType = json).status)

assertEquals(UNAUTHORIZED_401, it.get("/response").status)
assertEquals(NOT_FOUND_404, it.get("/response").status)
assertEquals(OK_200, it.get("/pathParam/param").status)
assertEquals(OK_200, it.get("/queryParam").status)
assertEquals(OK_200, it.get("/formParam").status)
Expand Down
5 changes: 4 additions & 1 deletion site/assets/css/mkdocs.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ ul.md-source__facts {

div.md-hero + nav.md-tabs {
background: #31415C;
text-align: center;
}

ul.md-tabs__list {
justify-content: center;
}

.md-hero {
Expand Down

0 comments on commit 589efef

Please sign in to comment.