Skip to content

Commit

Permalink
KTOR-8011: fix HTTP method is logged with the class name (#4624)
Browse files Browse the repository at this point in the history
* KTOR-8011: fix HTTP method is logged with the class name

* KTOR-8011: fix tests
  • Loading branch information
anton-erofeev authored Jan 27, 2025
1 parent a259433 commit c25705d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LoggingMockedTests {
fun testLogResponseWithException() = testWithEngine(MockEngine, retries = 5) {
val testLogger = TestLogger(
"REQUEST: ${URLBuilder.origin}",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -40,7 +40,7 @@ class LoggingMockedTests {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: ${URLBuilder.origin}",
"COMMON HEADERS",
"+++RESPONSE ${URLBuilder.origin} failed with exception: CustomError[PARSE ERROR]",
Expand Down Expand Up @@ -94,7 +94,7 @@ class LoggingMockedTests {
fun testLogResponseWithExceptionSingle() = testWithEngine(MockEngine) {
val testLogger = TestLogger(
"REQUEST: ${URLBuilder.origin}",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -105,7 +105,7 @@ class LoggingMockedTests {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: ${URLBuilder.origin}",
"COMMON HEADERS",
"RESPONSE ${URLBuilder.origin} failed with exception: CustomError[PARSE ERROR]",
Expand Down Expand Up @@ -150,7 +150,7 @@ class LoggingMockedTests {
fun testLoggingWithForm() = testWithEngine(MockEngine) {
val testLogger = TestLogger(
"REQUEST: http://localhost/",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -168,7 +168,7 @@ class LoggingMockedTests {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://localhost/",
"COMMON HEADERS",
"BODY Content-Type: null",
Expand Down Expand Up @@ -219,7 +219,7 @@ class LoggingMockedTests {
fun testFilterRequest() = testWithEngine(MockEngine) {
val testLogger = TestLogger(
"REQUEST: http://somewhere/filtered_path",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -230,7 +230,7 @@ class LoggingMockedTests {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: http://somewhere/filtered_path",
"COMMON HEADERS",
"BODY Content-Type: null",
Expand Down Expand Up @@ -264,7 +264,7 @@ class LoggingMockedTests {
fun testSanitizeHeaders() = testWithEngine(MockEngine) {
val testLogger = TestLogger {
line("REQUEST: http://localhost/")
line("METHOD: HttpMethod(value=GET)")
line("METHOD: GET")
line("COMMON HEADERS")
line("-> Accept: */*")
line("-> Accept-Charset: UTF-8")
Expand All @@ -273,7 +273,7 @@ class LoggingMockedTests {
line("CONTENT HEADERS")
line("-> Content-Length: 0")
line("RESPONSE: 200 OK")
line("METHOD: HttpMethod(value=GET)")
line("METHOD: GET")
line("FROM: http://localhost/")
line("COMMON HEADERS")
line("-> Sanitized: ***")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class LoggingTest : ClientLoader() {
fun testLoggingLevelBody() = clientTests(except("native:CIO")) {
val logger = TestLogger(
"REQUEST: http://localhost:8080/logging",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"BODY Content-Type: null",
"BODY START",
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: http://localhost:8080/logging",
"BODY Content-Type: text/plain; charset=UTF-8",
"BODY START",
Expand All @@ -76,7 +76,7 @@ class LoggingTest : ClientLoader() {
fun testLogLevelAll() = clientTests(except("native:CIO")) {
val logger = TestLogger(
"REQUEST: http://localhost:8080/logging",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -87,7 +87,7 @@ class LoggingTest : ClientLoader() {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: http://localhost:8080/logging",
"COMMON HEADERS",
"???-> Connection: keep-alive",
Expand All @@ -106,14 +106,14 @@ class LoggingTest : ClientLoader() {
fun testLogLevelHeaders() = clientTests {
val logger = TestLogger {
line("REQUEST: http://localhost:8080/logging")
line("METHOD: HttpMethod(value=GET)")
line("METHOD: GET")
line("COMMON HEADERS")
line("-> Accept: */*")
line("-> Accept-Charset: UTF-8")
line("CONTENT HEADERS")
line("-> Content-Length: 0")
line("RESPONSE: 200 OK")
line("METHOD: HttpMethod(value=GET)")
line("METHOD: GET")
line("FROM: http://localhost:8080/logging")
line("COMMON HEADERS")
optional("-> Connection: close")
Expand All @@ -128,9 +128,9 @@ class LoggingTest : ClientLoader() {
fun testLogLevelInfo() = clientTests {
val logger = TestLogger(
"REQUEST: http://localhost:8080/logging",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: http://localhost:8080/logging"
)
checkLog(logger, HttpMethod.Get, "", null, LogLevel.INFO)
Expand All @@ -146,7 +146,7 @@ class LoggingTest : ClientLoader() {
fun testLogPostBody() = clientTests(except("native:CIO")) {
val testLogger = TestLogger(
"REQUEST: http://localhost:8080/logging",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -158,7 +158,7 @@ class LoggingTest : ClientLoader() {
content,
"BODY END",
"RESPONSE: 201 Created",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://localhost:8080/logging",
"COMMON HEADERS",
"???-> Connection: close",
Expand Down Expand Up @@ -202,7 +202,7 @@ class LoggingTest : ClientLoader() {
fun testLogPostMalformedUtf8Body() = clientTests(except("native:CIO")) {
val testLogger = TestLogger(
"REQUEST: http://localhost:8080/logging/non-utf",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -214,7 +214,7 @@ class LoggingTest : ClientLoader() {
"�o",
"BODY END",
"RESPONSE: 201 Created",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://localhost:8080/logging/non-utf",
"COMMON HEADERS",
"???-> Connection: close",
Expand Down Expand Up @@ -258,7 +258,7 @@ class LoggingTest : ClientLoader() {
fun testRequestAndResponseBody() = clientTests(except("native:CIO")) {
val testLogger = TestLogger(
"REQUEST: http://127.0.0.1:8080/content/echo",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -270,7 +270,7 @@ class LoggingTest : ClientLoader() {
"test",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://127.0.0.1:8080/content/echo",
"COMMON HEADERS",
"???-> Connection: close",
Expand Down Expand Up @@ -308,7 +308,7 @@ class LoggingTest : ClientLoader() {
fun testRequestContentTypeInLog() = clientTests(except("Darwin", "native:CIO", "DarwinLegacy")) {
val testLogger = TestLogger(
"REQUEST: http://127.0.0.1:8080/content/echo",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -320,7 +320,7 @@ class LoggingTest : ClientLoader() {
"test",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://127.0.0.1:8080/content/echo",
"COMMON HEADERS",
"???-> Connection: keep-alive",
Expand Down Expand Up @@ -360,7 +360,7 @@ class LoggingTest : ClientLoader() {
fun testLoggingWithCompression() = clientTests(except("Darwin", "DarwinLegacy", "native:CIO", "web:CIO")) {
val testLogger = TestLogger(
"REQUEST: http://127.0.0.1:8080/compression/deflate",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"COMMON HEADERS",
"-> Accept: */*",
"-> Accept-Charset: UTF-8",
Expand All @@ -372,7 +372,7 @@ class LoggingTest : ClientLoader() {
"",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=GET)",
"METHOD: GET",
"FROM: http://127.0.0.1:8080/compression/deflate",
"COMMON HEADERS",
"???-> Connection: keep-alive",
Expand Down Expand Up @@ -409,7 +409,7 @@ class LoggingTest : ClientLoader() {
fun testLoggingWithStreaming() = clientTests {
val testLogger = TestLogger(
"REQUEST: http://127.0.0.1:8080/content/echo",
"METHOD: HttpMethod(value=POST)"
"METHOD: POST"
)
config {
Logging {
Expand Down Expand Up @@ -507,7 +507,7 @@ class LoggingTest : ClientLoader() {
fun testLogPostBodyWithJson() = clientTests(retries = 5) {
val testLogger = TestLogger(
"REQUEST: http://127.0.0.1:8080/content/echo",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"COMMON HEADERS",
"-> Accept: application/json",
"-> Accept-Charset: UTF-8",
Expand All @@ -519,7 +519,7 @@ class LoggingTest : ClientLoader() {
"{\"name\":\"Ktor\"}",
"BODY END",
"RESPONSE: 200 OK",
"METHOD: HttpMethod(value=POST)",
"METHOD: POST",
"FROM: http://127.0.0.1:8080/content/echo",
"COMMON HEADERS",
"???-> connection: keep-alive",
Expand Down
2 changes: 2 additions & 0 deletions ktor-http/common/src/io/ktor/http/HttpMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package io.ktor.http
* @property value contains method name
*/
public data class HttpMethod(val value: String) {
override fun toString(): String = value

@Suppress("KDocMissingDocumentation", "PublicApiImplicitType")
public companion object {
public val Get: HttpMethod = HttpMethod("GET")
Expand Down

0 comments on commit c25705d

Please sign in to comment.