-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b46a44
commit f8b35a9
Showing
13 changed files
with
106 additions
and
29 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package daikon | ||
|
||
interface Context { | ||
fun addAttribute(key: String, value: Any) | ||
fun <T> getAttribute(key: String) : T | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
package daikon | ||
|
||
class DummyRouteAction(private val action: (Request, Response) -> Unit) : RouteAction { | ||
override fun handle(request: Request, response: Response) { | ||
override fun handle(request: Request, response: Response, context: Context) { | ||
action.invoke(request, response) | ||
} | ||
} | ||
|
||
class ContextRouteAction(private val action: (Request, Response, Context) -> Unit) : RouteAction { | ||
override fun handle(request: Request, response: Response, context: Context) { | ||
action.invoke(request, response, context) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package daikon | ||
|
||
interface RouteAction { | ||
fun handle(request: Request, response: Response) | ||
fun handle(request: Request, response: Response, context: Context) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package daikon | ||
|
||
class ServerContext : Context { | ||
|
||
private val attributes = mutableMapOf<String, Any>() | ||
|
||
override fun addAttribute(key: String, value: Any) { | ||
attributes[key] = value | ||
} | ||
|
||
override fun <T> getAttribute(key: String): T { | ||
@Suppress("UNCHECKED_CAST") | ||
return attributes[key] as T | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package daikon | ||
|
||
import daikon.Localhost.get | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class HttpContextTest { | ||
|
||
@Test | ||
fun `context preserve attributes`() { | ||
HttpServer() | ||
.afterStart { ctx -> ctx.addAttribute("key", "value") } | ||
.get("/") { _, res, ctx -> res.write(ctx.getAttribute("key")) } | ||
.start().use { | ||
assertThat(get("/").text).isEqualTo("value") | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package daikon | ||
|
||
class NopAction : RouteAction { | ||
override fun handle(request: Request, response: Response) { | ||
override fun handle(request: Request, response: Response, context: Context) { | ||
} | ||
} |