-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BrowserTest.kt
101 lines (88 loc) · 3.22 KB
/
BrowserTest.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
package com.interaso.webpush
import com.microsoft.playwright.*
import com.microsoft.playwright.assertions.*
import com.microsoft.playwright.assertions.PlaywrightAssertions.*
import io.ktor.server.application.*
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.html.*
import io.ktor.server.http.content.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.util.*
import kotlinx.coroutines.*
import kotlinx.html.*
import org.junit.jupiter.api.*
import java.nio.file.*
import kotlin.io.path.*
@OptIn(ExperimentalPathApi::class)
class BrowserTest {
@Test
fun shouldReceiveNotification() {
val vapidKeys = VapidKeys.fromUncompressedBytes(
"BJwwFRoDoOx2vQPfvbeo-m1fZZHo6lIjtyTlWHjLNSCtHuWdGryZD5xt0LeawVQq7G60ioID1sC33fEoQT8jCzg",
"P5GjTLppISlmUyNiZqZi0HNq7GXFniAdcBECNsKBxfI",
)
val webPush = WebPushService("mailto:oss@interaso.com", vapidKeys)
val notification = "Test"
val server = embeddedServer(CIO, port = 0) {
routing {
staticResources("/", null)
get("/") {
call.respondHtml {
head { script(type = "module", src = "test.js") {} }
body {}
}
}
get("/vapid") {
call.respondBytes(webPush.vapidKeys.applicationServerKey)
}
post("/send") {
val params = call.receiveParameters()
val endpoint: String by params
val p256dh: String by params
val auth: String by params
webPush.send(notification, endpoint, p256dh, auth)
call.respondText("OK")
}
}
}
startServer(server) { port ->
openPage("http://127.0.0.1:$port") { page ->
assertThat(page.locator("body")).hasText(
notification,
LocatorAssertions.HasTextOptions().setTimeout(60_000.0),
)
}
}
}
private fun openPage(url: String, block: (Page) -> Unit) {
val tempDir = Files.createTempDirectory("test")
val contextOptions = BrowserType.LaunchPersistentContextOptions()
.setHeadless(false) // TODO: does not work in headless mode
.setChromiumSandbox(false)
.setIgnoreHTTPSErrors(true)
.setPermissions(listOf("notifications"))
try {
Playwright.create().use { playwright ->
playwright.chromium().launchPersistentContext(tempDir, contextOptions).use { context ->
context.newPage().use { page ->
page.navigate(url)
block(page)
}
}
}
} finally {
tempDir.deleteRecursively()
}
}
private fun startServer(server: ApplicationEngine, block: (Int) -> Unit) {
try {
server.start()
block(runBlocking { server.resolvedConnectors().first().port })
} finally {
server.stop()
}
}
}