You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IrcClientImplTest.kt 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.io.LineBufferedSocket
  3. import com.dmdirc.ktirc.model.Profile
  4. import com.dmdirc.ktirc.model.Server
  5. import com.nhaarman.mockitokotlin2.*
  6. import kotlinx.coroutines.channels.Channel
  7. import kotlinx.coroutines.runBlocking
  8. import org.junit.jupiter.api.Test
  9. import org.junit.jupiter.api.assertThrows
  10. internal class IrcClientImplTest {
  11. companion object {
  12. private const val HOST = "thegibson.com"
  13. private const val PORT = 12345
  14. private const val NICK = "AcidBurn"
  15. private const val REAL_NAME = "Kate Libby"
  16. private const val USER_NAME = "acidb"
  17. private const val PASSWORD = "HackThePlanet"
  18. }
  19. private val readLineChannel = Channel<ByteArray>(10)
  20. private val mockSocket = mock<LineBufferedSocket> {
  21. on { readLines(any()) } doReturn readLineChannel
  22. }
  23. private val mockSocketFactory = mock<(String, Int) -> LineBufferedSocket> {
  24. on { invoke(HOST, PORT) } doReturn mockSocket
  25. }
  26. @Test
  27. fun `IrcClientImpl uses socket factory to create a new socket on connect`() {
  28. runBlocking {
  29. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  30. client.socketFactory = mockSocketFactory
  31. readLineChannel.close()
  32. client.connect()
  33. verify(mockSocketFactory).invoke(HOST, PORT)
  34. }
  35. }
  36. @Test
  37. fun `IrcClientImpl throws if socket already exists`() {
  38. runBlocking {
  39. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  40. client.socketFactory = mockSocketFactory
  41. readLineChannel.close()
  42. client.connect()
  43. assertThrows<IllegalStateException> {
  44. runBlocking {
  45. client.connect()
  46. }
  47. }
  48. }
  49. }
  50. @Test
  51. fun `IrcClientImpl sends basic connection strings`() {
  52. runBlocking {
  53. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  54. client.socketFactory = mockSocketFactory
  55. readLineChannel.close()
  56. client.connect()
  57. with(inOrder(mockSocket).verify(mockSocket)) {
  58. sendLine("NICK :$NICK")
  59. sendLine("USER $USER_NAME localhost $HOST :$REAL_NAME")
  60. }
  61. }
  62. }
  63. @Test
  64. fun `IrcClientImpl sends password first, when present`() {
  65. runBlocking {
  66. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  67. client.socketFactory = mockSocketFactory
  68. readLineChannel.close()
  69. client.connect()
  70. with(inOrder(mockSocket).verify(mockSocket)) {
  71. sendLine("PASS :$PASSWORD")
  72. sendLine("NICK :$NICK")
  73. }
  74. }
  75. }
  76. }