forked from hexagontk/hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionTest.kt
118 lines (93 loc) · 4.33 KB
/
SessionTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.hexagonkt.http.server.examples
import com.hexagonkt.helpers.require
import com.hexagonkt.http.client.Client
import com.hexagonkt.http.client.ahc.AhcAdapter
import com.hexagonkt.http.server.Server
import com.hexagonkt.http.server.ServerPort
import com.hexagonkt.http.client.Response
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
@TestInstance(PER_CLASS)
abstract class SessionTest(adapter: ServerPort) {
// session
val server: Server = Server(adapter) {
path("/session") {
get("/id") { ok(session.id ?: "null") }
get("/access") { ok(session.lastAccessedTime?.toString() ?: "null") }
get("/new") { ok(session.isNew()) }
path("/inactive") {
get { ok(session.maxInactiveInterval ?: "null") }
put("/{time}") {
session.maxInactiveInterval = pathParameters.require("time").toInt()
}
}
get("/creation") { ok(session.creationTime ?: "null") }
post("/invalidate") { session.invalidate() }
path("/{key}") {
put("/{value}") {
session.set(pathParameters.require("key"), pathParameters.require("value"))
}
get { ok(session.get(pathParameters.require("key")).toString()) }
delete { session.remove(pathParameters.require("key")) }
}
get {
val attributes = session.attributes
val attributeTexts = attributes.entries.map { it.key + " : " + it.value }
response.headers["attributes"] = attributeTexts.joinToString(", ")
response.headers["attribute values"] = attributes.values.joinToString(", ")
response.headers["attribute names"] = attributes.keys.joinToString(", ")
response.headers["creation"] = session.creationTime.toString()
response.headers["id"] = session.id ?: ""
response.headers["last access"] = session.lastAccessedTime.toString()
response.status = 200
}
}
}
// session
private val client: Client by lazy {
Client(AhcAdapter(), "http://localhost:${server.runtimePort}")
}
@BeforeAll fun initialize() {
server.start()
}
@AfterAll fun shutdown() {
server.stop()
}
@Test fun `Attribute is added to session`() {
assert(client.put("/session/foo/bar").status == 200)
assertResponseEquals(client.get("/session/foo"), "bar")
}
@Test fun `Session attribute lifecycle test`() {
client.post("/session/invalidate")
assert(client.get("/session/id").body == "null")
assert(client.get("/session/inactive").body == "null")
assert(client.get("/session/creation").body == "null")
assert(client.get("/session/access").body == "null")
assert(client.get("/session/new").body == "true")
assert(client.put("/session/foo/bar").status == 200)
assert(client.put("/session/foo/bazz").status == 200)
assert(client.put("/session/temporal/_").status == 200)
assert(client.delete("/session/temporal").status == 200)
assert(client.get("/session").status == 200)
assertResponseEquals(client.get("/session/foo"), "bazz")
assert(client.get("/session/id").body != "null")
assert(client.get("/session/inactive").body != "null")
assert(client.get("/session/creation").body != "null")
assert(client.get("/session/access").body != "null")
assert(client.get("/session/new").body == "false")
assert(client.put("/session/inactive/10").status == 200)
assert(client.get("/session/inactive").body == "10")
client.post("/session/invalidate")
assert(client.get("/session/id").body == "null")
assert(client.get("/session/inactive").body == "null")
assert(client.get("/session/creation").body == "null")
assert(client.get("/session/access").body == "null")
}
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
assert (response?.status == status)
assert (response?.body == content)
}
}