sbt/server-test/src/test/scala/testpkg/ResponseTest.scala

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
3.4 KiB
Scala
Raw Normal View History

/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package testpkg
import scala.concurrent.duration.*
// starts svr using server-test/response and perform custom server tests
2024-03-19 13:03:12 +01:00
class ResponseTest extends AbstractServerTest {
override val testDirectory: String = "response"
2024-03-19 13:03:12 +01:00
test("response from a command") {
svr.sendJsonRpc("""{ "jsonrpc": "2.0", "id": "10", "method": "foo/export", "params": {} }""")
assert(svr.waitForString(10.seconds) { s =>
2024-06-05 16:34:34 +02:00
if (!s.contains("systemOut"))
println(s)
2024-10-28 04:55:30 +01:00
s.contains(""""id":"10"""") &&
s.contains("scala-library-2.12.21.jar")
})
}
2024-03-19 13:03:12 +01:00
test("response from a task") {
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "11", "method": "foo/rootClasspath", "params": {} }"""
)
assert(svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains(""""id":"11"""") &&
s.contains("scala-library-2.12.21.jar")
})
}
2024-03-19 13:03:12 +01:00
test("a command failure") {
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "12", "method": "foo/fail", "params": {} }"""
)
assert(svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains(""""error":{"code":-33000,"message":"fail message"""")
})
}
2024-03-19 13:03:12 +01:00
test("a command failure with custom code") {
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "13", "method": "foo/customfail", "params": {} }"""
)
assert(svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains(""""error":{"code":500,"message":"some error"""")
})
}
2024-03-19 13:03:12 +01:00
test("a command with a notification") {
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "14", "method": "foo/notification", "params": {} }"""
)
assert(svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("""{"jsonrpc":"2.0","method":"foo/something","params":"something"}""")
})
}
2020-05-12 09:00:44 +02:00
2024-03-19 13:03:12 +01:00
test("respond concurrently from a task and the handler") {
2020-05-12 09:00:44 +02:00
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "15", "method": "foo/respondTwice", "params": {} }"""
)
assert {
svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("\"id\":\"15\"")
2020-05-12 09:00:44 +02:00
}
}
assert {
// the second response should never be sent
svr.neverReceive(500.milliseconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("\"id\":\"15\"")
2020-05-12 09:00:44 +02:00
}
}
}
2024-03-19 13:03:12 +01:00
test("concurrent result and error") {
2020-05-12 09:00:44 +02:00
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "16", "method": "foo/resultAndError", "params": {} }"""
)
assert {
svr.waitForString(10.seconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("\"id\":\"16\"")
2020-05-12 09:00:44 +02:00
}
}
assert {
// the second response (result or error) should never be sent
svr.neverReceive(500.milliseconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("\"id\":\"16\"")
2020-05-12 09:00:44 +02:00
}
}
}
2024-03-19 13:03:12 +01:00
test("response to a notification should not be sent") {
2020-05-12 09:00:44 +02:00
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "method": "foo/customNotification", "params": {} }"""
)
assert {
svr.neverReceive(500.milliseconds) { s =>
if (!s.contains("systemOut")) println(s)
2024-10-28 04:55:30 +01:00
s.contains("\"result\":\"notification result\"")
2020-05-12 09:00:44 +02:00
}
}
}
}