forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttplisten_test.go
165 lines (136 loc) · 4.3 KB
/
httplisten_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
154
155
156
157
158
159
160
161
162
163
164
165
package inputhttplisten
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
codecjson "github.com/tsaikd/gogstash/codec/json"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistInputHandler(ModuleName, InitHandler)
config.RegistCodecHandler(codecjson.ModuleName, codecjson.InitHandler)
}
func Test_input_httplisten_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
input:
- type: httplisten
address: "127.0.0.1:8089"
path: "/"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
time.Sleep(500 * time.Millisecond)
resp, err := http.Post("http://127.0.0.1:8089/", "application/json", bytes.NewReader([]byte("{\"foo\":\"bar\"}")))
require.NoError(err)
defer resp.Body.Close()
assert.Equal(http.StatusOK, resp.StatusCode)
data, err := ioutil.ReadAll(resp.Body)
require.NoError(err)
assert.Equal([]byte{}, data)
if event, err := conf.TestGetOutputEvent(100 * time.Millisecond); assert.NoError(err) {
assert.Equal(map[string]interface{}{"foo": "bar"}, event.Extra)
}
}
func Test_input_httpslisten_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
input:
- type: httplisten
address: "127.0.0.1:8989"
path: "/tls/"
cert: "./server.pem"
key: "./server.key"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
time.Sleep(500 * time.Millisecond)
rootPEM, err := ioutil.ReadFile("./root.pem")
require.NoError(err)
roots := x509.NewCertPool()
assert.NotNil(roots)
ok := roots.AppendCertsFromPEM(rootPEM)
assert.Equal(ok, true)
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: roots}}}
resp, err := client.Post("https://127.0.0.1:8989/tls/", "application/json", bytes.NewReader([]byte("{\"foo\":\"bar\"}")))
require.NoError(err)
defer resp.Body.Close()
assert.Equal(http.StatusOK, resp.StatusCode)
data, err := ioutil.ReadAll(resp.Body)
require.NoError(err)
assert.Equal([]byte{}, data)
if event, err := conf.TestGetOutputEvent(100 * time.Millisecond); assert.NoError(err) {
assert.Equal(map[string]interface{}{"foo": "bar"}, event.Extra)
}
}
func Test_input_httpslisten2_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
input:
- type: httplisten
address: "127.0.0.1:8999"
path: "/tls2/"
cert: "./server.pem"
key: "./server.key"
ca: "./root.pem"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
time.Sleep(500 * time.Millisecond)
rootPEM, err := ioutil.ReadFile("./root.pem")
require.NoError(err)
roots := x509.NewCertPool()
assert.NotNil(roots)
ok := roots.AppendCertsFromPEM(rootPEM)
assert.Equal(ok, true)
clientCert, err := tls.LoadX509KeyPair("./client.pem", "./client.key")
require.NoError(err)
// case 1: without client cert
tlsConfig := tls.Config{
RootCAs: roots,
}
transport := http.Transport{
TLSClientConfig: &tlsConfig,
}
client := &http.Client{Transport: &transport}
_, err = client.Post("https://127.0.0.1:8999/tls2/", "application/json", bytes.NewReader([]byte("{\"foo2\":\"bar2\"}")))
assert.NotNil(err)
// case 2: with correct client cert
tlsConfig.Certificates = []tls.Certificate{clientCert}
resp, err := client.Post("https://127.0.0.1:8999/tls2/", "application/json", bytes.NewReader([]byte("{\"foo2\":\"bar2\"}")))
require.NoError(err)
defer resp.Body.Close()
assert.Equal(http.StatusOK, resp.StatusCode)
data, err := ioutil.ReadAll(resp.Body)
require.NoError(err)
assert.Equal([]byte{}, data)
if event, err := conf.TestGetOutputEvent(100 * time.Millisecond); assert.NoError(err) {
assert.Equal(map[string]interface{}{"foo2": "bar2"}, event.Extra)
}
}