Skip to content

Commit

Permalink
Sjekker at url har query parametere før vi eventuelt bruker url templ…
Browse files Browse the repository at this point in the history
…ate (#227)

* Sjekker at url har queryparamtere.

* Refaktorerer tilUrlTemplate og lager tester
  • Loading branch information
SanderOpperud committed Mar 17, 2021
1 parent 25a9f8d commit fa18872
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@ fun CallLogging.Configuration.correlationIdAndRequestIdInMdc() {

fun CallLogging.Configuration.logRequests(
excludePaths : Set<String> = Paths.DEFAULT_EXCLUDED_PATHS,
urlTemplate: Boolean = false
templateQueryParameters: Boolean = false
) {
logger = LOG
level = Level.INFO
filter { call -> !excludePaths.contains(call.request.path()) }
if(urlTemplate) format { applicationCall -> applicationCall.request.uri.toUrlTemplate() }
if(templateQueryParameters) format { applicationCall -> applicationCall.request.uri.templateQueryParameters() }
}

fun ApplicationRequest.log(
verbose : Boolean = false,
excludePaths : Set<String> = Paths.DEFAULT_EXCLUDED_PATHS,
urlTemplate: Boolean = false
templateQueryParameters: Boolean = false
) {
if (!excludePaths.contains(call.request.path())) {
val uri = if(urlTemplate) this.uri.toUrlTemplate() else this.uri
val uri = if(templateQueryParameters) this.uri.templateQueryParameters() else this.uri
LOG.info("Request ${httpMethod.value} $uri (HTTP Version $httpVersion)")
if (verbose) {
LOG.info("Origin ${header(HttpHeaders.Origin)} (User Agent ${userAgent()})")
}
}
}

fun String.toUrlTemplate(): String {
fun String.templateQueryParameters(): String {
val urlParts = split("?")
if(urlParts.size <2) return this

val query = urlParts[1].split("&").joinToString("&") {
it.replaceAfter("=", "{${it.substringBefore("=")}}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.nav.helse.dusseldorf.ktor.core

import org.junit.Test
import kotlin.test.assertEquals

class UrlTemplateTest {

@Test
fun `Test url med query parameters`(){
assertEquals(
"http://localhost.no/dokument?eier={eier}&navn={navn}",
"http://localhost.no/dokument?eier=123&navn=noe".templateQueryParameters()
)
}

@Test
fun `Test url uten query parameters`(){
assertEquals(
"http://localhost.no/dokument",
"http://localhost.no/dokument".templateQueryParameters()
)
}

}

0 comments on commit fa18872

Please sign in to comment.