-
Notifications
You must be signed in to change notification settings - Fork 1
/
nntp_integration_test.go
153 lines (107 loc) · 3.48 KB
/
nntp_integration_test.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// +build integration
package nntp_test
import (
"crypto/tls"
"fmt"
"io"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mrincompetent/nntp"
)
var testTime = time.Date(2020, 5, 1, 0, 0, 0, 0, time.Now().Location())
const (
testGroup = "a.b.binaries.tvseries"
)
type LoggingConnection struct {
t testing.TB
c io.ReadWriteCloser
}
func (c *LoggingConnection) Read(p []byte) (n int, err error) {
pp := make([]byte, len(p))
n, err = c.c.Read(pp)
copy(p, pp)
c.t.Log(string(pp))
return n, err
}
func (c *LoggingConnection) Write(p []byte) (n int, err error) {
pp := make([]byte, len(p))
copy(pp, p)
c.t.Log(string(pp))
return c.c.Write(p)
}
func (c *LoggingConnection) Close() error {
return c.c.Close()
}
func GetIntegrationClient(t testing.TB) *nntp.Client {
address := os.Getenv("NNTP_TEST_ADDRESS")
hostname, _, err := net.SplitHostPort(address)
require.NoError(t, err, "Failed to split NNTP_TEST_ADDRESS '%s' into hostname and port")
tlsConfig := &tls.Config{
ServerName: hostname,
InsecureSkipVerify: false,
}
conn, err := tls.Dial("tcp", address, tlsConfig)
require.NoError(t, err, "Failed to get integration test connection")
client, err := nntp.NewFromConn(&LoggingConnection{c: conn, t: t})
require.NoError(t, err, "Failed to create client from connection")
t.Cleanup(func() {
require.NoError(t, client.Quit(), "Failed to close client")
require.NoError(t, conn.Close(), "Failed to close connection")
})
return client
}
func GetAuthenticatedIntegrationClient(t testing.TB) *nntp.Client {
client := GetIntegrationClient(t)
err := client.Authenticate(os.Getenv("NNTP_TEST_USERNAME"), os.Getenv("NNTP_TEST_PASSWORD"))
require.NoError(t, err, "Failed to authenticate")
return client
}
func TestClient_Integration_Help(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
help, err := client.Help()
require.NoError(t, err, "Failed to call help")
t.Logf("Help: %s", help)
}
func TestClient_Integration_Date(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
date, err := client.Date()
require.NoError(t, err, "Failed to call date")
t.Logf("Date: %s", date)
}
func TestClient_Integration_Newsgroups(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
groups, err := client.Newsgroups(testTime)
require.NoError(t, err, "Failed to list groups")
t.Log(toJSON(t, groups[0]))
}
func TestClient_Integration_Group(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
group, err := client.Group(testGroup)
require.NoError(t, err, "Failed to change group")
t.Log(toJSON(t, group))
}
func TestClient_Integration_Xover(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
group, err := client.Group(testGroup)
require.NoError(t, err, "Failed to change group")
headers, err := client.Xover(fmt.Sprintf("%d-%d", group.High-100, group.High))
require.NoError(t, err, "Failed to list headers")
for _, header := range headers {
t.Logf("%s: %s", header.MessageID, header.Subject)
}
}
func TestClient_Integration_XoverChan(t *testing.T) {
client := GetAuthenticatedIntegrationClient(t)
group, err := client.Group(testGroup)
require.NoError(t, err, "Failed to change group")
headerChan, errChan, err := client.XoverChan(fmt.Sprintf("%d-%d", group.High-100, group.High))
require.NoError(t, err, "Failed to list headers")
for header := range headerChan {
t.Logf("%s: %s", header.MessageID, header.Subject)
}
assert.Len(t, errChan, 0)
}