Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation and logging #652

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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