-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial impl for HttpLoggerInterceptor. Changed structure of LogInterceptor. Simplified LoggingStrategy * Introduced flatMap for body, add timeMeasuring in http log * improved tests for Curl logging * Http log interecptor tests
- Loading branch information
1 parent
b79ac37
commit ef6f909
Showing
13 changed files
with
412 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package io.github.rybalkinsd.kohttp.dsl | ||
|
||
import io.github.rybalkinsd.kohttp.jackson.ext.toJson | ||
import io.github.rybalkinsd.kohttp.assertContainsAtLeast | ||
import io.github.rybalkinsd.kohttp.assertContainsExactly | ||
import io.github.rybalkinsd.kohttp.client.defaultHttpClient | ||
import io.github.rybalkinsd.kohttp.client.fork | ||
import io.github.rybalkinsd.kohttp.interceptors.logging.HttpLoggingInterceptor | ||
import io.github.rybalkinsd.kohttp.jackson.ext.toJson | ||
import org.junit.Test | ||
import java.io.File | ||
import kotlin.test.assertEquals | ||
|
@@ -29,7 +32,13 @@ class HttpPostDslKtTest { | |
"email" to "[email protected]" | ||
) | ||
|
||
httpPost { | ||
val client = defaultHttpClient.fork { | ||
interceptors { | ||
+HttpLoggingInterceptor() | ||
} | ||
} | ||
|
||
httpPost(client) { | ||
host = "postman-echo.com" | ||
path = "/post" | ||
|
||
|
36 changes: 0 additions & 36 deletions
36
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/LoggingInterceptor.kt
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
...rc/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/logging/CurlLoggingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.rybalkinsd.kohttp.interceptors.logging | ||
|
||
import io.github.rybalkinsd.kohttp.ext.asSequence | ||
import io.github.rybalkinsd.kohttp.util.flatMap | ||
import okhttp3.* | ||
|
||
/** | ||
* Request Logging Interceptor | ||
* | ||
* Logs cURL commands for outgoing requests. | ||
* | ||
* @param log log consumer | ||
* | ||
* --> | ||
* curl -X POST -H "one: 42" -H "cookie: aaa=bbb; ccc=42" \ | ||
* --data $'login=user&email=john.doe%40gmail.com' \ | ||
* "http://postman-echo.com/post?arg=iphone" | ||
* --- | ||
* | ||
* @since 0.11.0 | ||
* @author doyaaaaaken, gokul, sergey | ||
*/ | ||
class CurlLoggingInterceptor( | ||
private val log: (String) -> Unit = ::println | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
log("-->") | ||
val command = buildCurlCommand(request) | ||
log(command) | ||
log("---") | ||
|
||
return chain.proceed(request) | ||
} | ||
|
||
private fun buildCurlCommand(request: Request) = buildString { | ||
append("curl -X ${request.method()}") | ||
append(buildCurlHeaderOption(request.headers())) | ||
append(buildBodyOption(request.body())) | ||
append(""" "${request.url()}"""") | ||
} | ||
|
||
private fun buildCurlHeaderOption(headers: Headers): String { | ||
return headers.asSequence().map { (name, value) -> | ||
val trimmedValue = value.trimDoubleQuote() | ||
""" -H "$name: $trimmedValue"""" | ||
}.joinToString("") | ||
} | ||
|
||
private fun buildBodyOption(body: RequestBody?): String { | ||
if (body == null) return "" | ||
return body.flatMap { | ||
it.replace("\n", "\\n") | ||
.replace("\r", "\\r") | ||
}.run { | ||
" -d '$this'" | ||
} | ||
} | ||
|
||
} | ||
|
||
private fun String.trimDoubleQuote() = | ||
if (startsWith('"') && endsWith('"')) { | ||
substring(1, length - 1) | ||
} else this |
51 changes: 0 additions & 51 deletions
51
...p/src/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/logging/CurlLoggingStrategy.kt
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
...rc/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/logging/HttpLoggingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.rybalkinsd.kohttp.interceptors.logging | ||
|
||
import io.github.rybalkinsd.kohttp.ext.asSequence | ||
import io.github.rybalkinsd.kohttp.util.flatMap | ||
import okhttp3.Headers | ||
import okhttp3.Interceptor | ||
import okhttp3.RequestBody | ||
import okhttp3.Response | ||
import java.util.* | ||
|
||
/** | ||
* Logs HTTP requests. | ||
* | ||
* @param log log consumer | ||
* | ||
* --> 706213ee-3a26-4d2d-ab09-f4895f7ce295 | ||
* POST http://postman-echo.com/post?arg=iphone | ||
* one 42 | ||
* cookie aaa=bbb; ccc=42 | ||
* | ||
* login=user&email=john.doe%40gmail.com | ||
* --- | ||
* <-- 706213ee-3a26-4d2d-ab09-f4895f7ce295 | ||
* 200 http://postman-echo.com/post?arg=iphone | ||
* Content-Type application/json; charset=utf-8 | ||
* Server nginx | ||
* Vary Accept-Encoding | ||
* Connection keep-alive | ||
* --- | ||
* | ||
* @since 0.11.0 | ||
* @author sergey | ||
*/ | ||
class HttpLoggingInterceptor( | ||
private val log: (String) -> Unit = ::println | ||
) : Interceptor { | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
val connection = chain.connection() | ||
val id = UUID.randomUUID() | ||
|
||
log("--> $id") | ||
log("${request.method()} ${request.url()} ${connection?.protocol() ?: ""}") | ||
log(request.headers()) | ||
log("") | ||
request.body()?.let { log(it) } | ||
log("---") | ||
|
||
|
||
val start = System.currentTimeMillis() | ||
val timing: Long | ||
val response: Response = try { | ||
chain.proceed(request).also { | ||
timing = System.currentTimeMillis() - start | ||
} | ||
} catch (e: Exception) { | ||
log("<-- FAIL: $e") | ||
throw e | ||
} | ||
|
||
log("<-- $id in $timing ms") | ||
log("${response.code()} ${response.request().url()}") | ||
log(response.headers()) | ||
log("---") | ||
return response | ||
} | ||
|
||
private fun log(headers: Headers) { | ||
headers.asSequence().forEach { (k, v) -> | ||
log("$k $v") | ||
} | ||
} | ||
|
||
private fun log(body: RequestBody) { | ||
body.flatMap { | ||
it.replace("\n", "\\n") | ||
.replace("\r", "\\r") | ||
}.also { | ||
log(it) | ||
} | ||
} | ||
} | ||
|
28 changes: 0 additions & 28 deletions
28
...p/src/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/logging/HttpLoggingStrategy.kt
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/interceptors/logging/LoggingStrategy.kt
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/util/ResponseBodyExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.rybalkinsd.kohttp.util | ||
|
||
import okhttp3.RequestBody | ||
import okio.Buffer | ||
|
||
/** | ||
* Created by Sergey Rybalkin on 2019-09-11. | ||
*/ | ||
internal inline fun <reified T> RequestBody.flatMap(transform: (String) -> T): T = transform( | ||
Buffer().use { | ||
writeTo(it) | ||
it.readUtf8() | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.