]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
Many fixes and additions
[goircd.git] / client_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "testing"
22 )
23
24 func TestNewClient(t *testing.T) {
25         conn := NewTestingConn()
26         events := make(chan ClientEvent)
27         host := "foohost"
28         hostname = &host
29         client := NewClient(conn, events)
30         defer func() {
31                 client.Close()
32         }()
33
34         event := <-events
35         if event.eventType != EventNew {
36                 t.Fatal("no NEW event", event)
37         }
38         conn.inbound <- "foo"
39         event = <-events
40         if (event.eventType != EventMsg) || (event.text != "foo") {
41                 t.Fatal("no first MSG", event)
42         }
43         conn.inbound <- "bar"
44         event = <-events
45         if (event.eventType != EventMsg) || (event.text != "bar") {
46                 t.Fatal("no second MSG", event)
47         }
48         conn.inbound <- ""
49         event = <-events
50         if event.eventType != EventDel {
51                 t.Fatal("no client termination", event)
52         }
53 }
54
55 func TestClientReplies(t *testing.T) {
56         conn := NewTestingConn()
57         host := "foohost"
58         hostname = &host
59         client := NewClient(conn, make(chan ClientEvent, 2))
60         defer func() {
61                 client.Close()
62         }()
63         client.nickname = "мойник"
64
65         client.Reply("hello")
66         if r := <-conn.outbound; r != ":foohost hello\r\n" {
67                 t.Fatal("did not recieve hello message", r)
68         }
69
70         client.ReplyParts("200", "foo", "bar")
71         if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
72                 t.Fatal("did not recieve 200 message", r)
73         }
74
75         client.ReplyNicknamed("200", "foo", "bar")
76         if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
77                 t.Fatal("did not recieve nicknamed message", r)
78         }
79
80         client.ReplyNotEnoughParameters("CMD")
81         if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
82                 t.Fatal("did not recieve 461 message", r)
83         }
84 }