Skip to content

Commit

Permalink
Add methods to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
fracassi-marco committed Apr 16, 2020
1 parent e78b494 commit fd71a88
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 27 deletions.
70 changes: 70 additions & 0 deletions README.md-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Daikon

![Daikon](./logo.svg)

Daikon is a simple and minimal framework for creating web applications in Kotlin with minimal effort.
The main goals are:
* Use a framework easy to test
* Build a simple web application in less then 5 minutes
* Can run multiple instances of HTTP server at the same time
* Deploy application in container, with server start time less than 100ms

## How to add Daikon to your project
[![](https://jitpack.io/v/daikonweb/daikon.svg)](https://jitpack.io/#daikonweb/daikon)

### Gradle
- Add JitPack in your root build.gradle at the end of repositories:
```
repositories {
...
maven { url 'https://jitpack.io' }
}
```

- Add the dependency
```
implementation 'com.github.DaikonWeb:daikon:1.2.4'
```

### Maven
- Add the JitPack repository to your build file
```
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```
- Add the dependency
```
<dependency>
<groupId>com.github.DaikonWeb</groupId>
<artifactId>daikon</artifactId>
<version>1.2.3</version>
</dependency>
```

## Getting Started
```
HttpServer()
.get("/foo") { _, res -> res.write("Hello foo") }
.post("/bar") { _, res -> res.write("Bye bar") }
.start().use {
assertThat(get("/foo").text).isEqualTo("Hello foo")
assertThat(post("/bar").text).isEqualTo("Bye bar")
}
```

## Resources
* Documentation: https://daikonweb.github.io
* Examples: https://github.com/DaikonWeb/daikon-examples

## Authors

* **[Marco Fracassi](https://github.com/fracassi-marco)**
* **[Alessio Coser](https://github.com/AlessioCoser)**

## License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

compile 'org.eclipse.jetty:jetty-server:9.4.27.v20200227'
compile 'org.eclipse.jetty:jetty-servlet:9.4.27.v20200227'
compile 'com.github.DaikonWeb:daikon-core:1.3.1'
compile 'com.github.DaikonWeb:daikon-core:1.3.2'

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
Expand Down
32 changes: 11 additions & 21 deletions src/main/kotlin/daikon/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,28 @@ class HttpRequest(private val request: HttpServletRequest) : Request {
return request.getAttribute(key)!! as T
}

override fun method(): Method {
return Method.valueOf(request.method)
}
override fun hasAttribute(key: String) = request.attributeNames.toList().contains(key)

override fun <T> attribute(key: String, value: T) {
request.setAttribute(key, value)
}
override fun method() = Method.valueOf(request.method)

override fun path(): String {
return request.requestURI
}
override fun <T> attribute(key: String, value: T) = request.setAttribute(key, value)

override fun url(): String {
return request.requestURL.toString()
}
override fun path(): String = request.requestURI

override fun body(): String {
return body
}
override fun url() = request.requestURL.toString()

override fun header(name: String): String {
return request.getHeader(name)
}
override fun body() = body

override fun hasHeader(name: String): Boolean {
return request.headerNames.toList().contains(name)
}
override fun header(name: String): String = request.getHeader(name)

override fun hasHeader(name: String) = request.headerNames.toList().contains(name)

override fun param(name: String): String {
return getBodyParameter(name) ?: request.getParameter(name) ?: pathParams.valueOf(path()).getValue(name)
}

override fun hasParam(name: String) = try { param(name); true } catch (t: Throwable) { false }

private fun getBodyParameter(name: String): String? {
if(body().isEmpty() || !hasHeader("Content-Type") || !header("Content-Type").startsWith("application/x-www-form-urlencoded")) {
return null
Expand Down
34 changes: 29 additions & 5 deletions src/test/kotlin/daikon/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ class RequestTest {

@Test
fun `query string parameter`() {
HttpServer()
.get("/") { req, _ ->
assertThat(req.hasParam("name")).isTrue()
assertThat(req.hasParam("age")).isFalse()
}
.start().use {
local("/?name=Bob").http.get()
}
}

@Test
fun `has parameter`() {
HttpServer()
.get("/") { req, res -> res.write("hello ${req.param("name")}") }
.start().use {
Expand Down Expand Up @@ -79,14 +91,13 @@ class RequestTest {
@Test
fun `check if an header is present`() {
HttpServer()
.post("/*") { req, res ->
val name = if (req.hasHeader("name")) req.header("name") else "World"
res.write("Hello $name")
.post("/*") { req, _ ->
assertThat(req.hasHeader("name")).isTrue()
assertThat(req.hasHeader("age")).isFalse()
}
.start()
.use {
assertThat(local("/").http.post().body).isEqualTo("Hello World")
assertThat(local("/").http.post(headers = mapOf("name" to "Bob")).body).isEqualTo("Hello Bob")
local("/").http.post(headers = mapOf("name" to "Bob"))
}
}

Expand Down Expand Up @@ -164,6 +175,19 @@ class RequestTest {
}
}

@Test
fun `has attribute`() {
HttpServer()
.before("/") { req, _ -> req.attribute("foo", "any") }
.get("/") { req, _ ->
assertThat(req.hasAttribute("foo")).isTrue()
assertThat(req.hasAttribute("bar")).isFalse()
}
.start().use {
local("/").http.get()
}
}

@Test
fun `attribute not found`() {
HttpServer()
Expand Down

0 comments on commit fd71a88

Please sign in to comment.