|
1 | 1 | package lc.server |
2 | 2 |
|
| 3 | +import org.scalatest.funsuite.AnyFunSuite |
| 4 | +import org.scalatest.BeforeAndAfterAll |
3 | 5 | import java.net.{HttpURLConnection, URL} |
4 | 6 | import java.io.{BufferedReader, InputStreamReader, OutputStreamWriter} |
5 | 7 | import lc.LCFramework |
6 | 8 |
|
7 | | -object ServerSpec { |
8 | | - def main(args: Array[String]): Unit = { |
9 | | - // Start server before tests in a thread |
10 | | - val serverRunnable = new Runnable { |
11 | | - override def run(): Unit = { |
12 | | - try { |
13 | | - LCFramework.main(Array.empty) |
14 | | - } catch { |
15 | | - case _: InterruptedException => // Expected on shutdown |
16 | | - } |
17 | | - } |
18 | | - } |
19 | | - val serverThread = new Thread(serverRunnable) |
20 | | - serverThread.start() |
21 | | - |
22 | | - // Give the server a few seconds to start |
23 | | - Thread.sleep(5000) |
24 | | - |
25 | | - try { |
26 | | - println("Running ServerSpec Test...") |
27 | | - val url = new URL("http://localhost:8888/v2/captcha") |
28 | | - val connection = url.openConnection().asInstanceOf[HttpURLConnection] |
29 | | - connection.setRequestMethod("POST") |
30 | | - connection.setRequestProperty("Content-Type", "application/json") |
31 | | - connection.setDoOutput(true) |
32 | | - |
33 | | - val payload = """{"level":"easy","media":"image/png","input_type":"text","size":"350x100"}""" |
34 | | - val out = new OutputStreamWriter(connection.getOutputStream) |
35 | | - out.write(payload) |
36 | | - out.close() |
37 | | - |
38 | | - val responseCode = connection.getResponseCode |
39 | | - assert(responseCode == 200, s"Expected 200 but got $responseCode") |
40 | | - |
41 | | - val in = new BufferedReader(new InputStreamReader(connection.getInputStream)) |
42 | | - val response = new StringBuilder |
43 | | - var line: String = in.readLine() |
44 | | - while (line != null) { |
45 | | - response.append(line) |
46 | | - line = in.readLine() |
47 | | - } |
48 | | - in.close() |
49 | | - |
50 | | - val responseString = response.toString() |
51 | | - assert(responseString.contains("id"), "Response did not contain an id") |
52 | | - println("Test Passed.") |
53 | | - } finally { |
54 | | - // Shutdown server without exit so SBT doesn't kill the VM |
55 | | - System.exit(0) |
| 9 | +class ServerSpec extends AnyFunSuite with BeforeAndAfterAll { |
| 10 | + |
| 11 | + val framework = new LCFramework() |
| 12 | + |
| 13 | + override def beforeAll(): Unit = { |
| 14 | + framework.start("tests/debug-config.json") |
| 15 | + // Give the server a moment to start and generate some captchas |
| 16 | + Thread.sleep(2000) |
| 17 | + } |
| 18 | + |
| 19 | + override def afterAll(): Unit = { |
| 20 | + framework.stop() |
| 21 | + } |
| 22 | + |
| 23 | + test("Server should respond with an id for a valid captcha request") { |
| 24 | + val url = new URL("http://localhost:8888/v2/captcha") |
| 25 | + val connection = url.openConnection().asInstanceOf[HttpURLConnection] |
| 26 | + connection.setRequestMethod("POST") |
| 27 | + connection.setRequestProperty("Content-Type", "application/json") |
| 28 | + connection.setDoOutput(true) |
| 29 | + |
| 30 | + val payload = """{"level":"debug","media":"image/png","input_type":"text","size":"350x100"}""" |
| 31 | + val out = new OutputStreamWriter(connection.getOutputStream) |
| 32 | + out.write(payload) |
| 33 | + out.close() |
| 34 | + |
| 35 | + val responseCode = connection.getResponseCode |
| 36 | + assert(responseCode == 200, s"Expected 200 but got $responseCode") |
| 37 | + |
| 38 | + val in = new BufferedReader(new InputStreamReader(connection.getInputStream)) |
| 39 | + val response = new StringBuilder |
| 40 | + var line: String = in.readLine() |
| 41 | + while (line != null) { |
| 42 | + response.append(line) |
| 43 | + line = in.readLine() |
56 | 44 | } |
| 45 | + in.close() |
| 46 | + |
| 47 | + val responseString = response.toString() |
| 48 | + assert(responseString.contains("id"), "Response did not contain an id") |
57 | 49 | } |
58 | 50 | } |
0 commit comments