summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/hooks_test.go
blob: 37c529510dff1d35ac2732dcf5ad999f64858011 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package rpcplugin

import (
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/plugin"
	"github.com/mattermost/mattermost-server/plugin/plugintest"
)

func testHooksRPC(hooks interface{}, f func(*RemoteHooks)) error {
	r1, w1 := io.Pipe()
	r2, w2 := io.Pipe()

	c1 := NewMuxer(NewReadWriteCloser(r1, w2), false)
	defer c1.Close()

	c2 := NewMuxer(NewReadWriteCloser(r2, w1), true)
	defer c2.Close()

	id, server := c1.Serve()
	go ServeHooks(hooks, server, c1)

	remote, err := ConnectHooks(c2.Connect(id), c2)
	if err != nil {
		return err
	}
	defer remote.Close()

	f(remote)
	return nil
}

func TestHooks(t *testing.T) {
	var api plugintest.API
	var hooks plugintest.Hooks
	defer hooks.AssertExpectations(t)

	assert.NoError(t, testHooksRPC(&hooks, func(remote *RemoteHooks) {
		hooks.On("OnActivate", mock.AnythingOfType("*rpcplugin.RemoteAPI")).Return(nil)
		assert.NoError(t, remote.OnActivate(&api))

		hooks.On("OnDeactivate").Return(nil)
		assert.NoError(t, remote.OnDeactivate())

		hooks.On("OnConfigurationChange").Return(nil)
		assert.NoError(t, remote.OnConfigurationChange())

		hooks.On("ServeHTTP", mock.AnythingOfType("*rpcplugin.RemoteHTTPResponseWriter"), mock.AnythingOfType("*http.Request")).Run(func(args mock.Arguments) {
			w := args.Get(0).(http.ResponseWriter)
			r := args.Get(1).(*http.Request)
			assert.Equal(t, "/foo", r.URL.Path)
			assert.Equal(t, "POST", r.Method)
			body, err := ioutil.ReadAll(r.Body)
			assert.NoError(t, err)
			assert.Equal(t, "asdf", string(body))
			assert.Equal(t, "header", r.Header.Get("Test-Header"))
			w.Write([]byte("bar"))
		})

		w := httptest.NewRecorder()
		r, err := http.NewRequest("POST", "/foo", strings.NewReader("asdf"))
		r.Header.Set("Test-Header", "header")
		assert.NoError(t, err)
		remote.ServeHTTP(w, r)

		resp := w.Result()
		defer resp.Body.Close()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		body, err := ioutil.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, "bar", string(body))
	}))
}

func TestHooks_Concurrency(t *testing.T) {
	var hooks plugintest.Hooks
	defer hooks.AssertExpectations(t)

	assert.NoError(t, testHooksRPC(&hooks, func(remote *RemoteHooks) {
		ch := make(chan bool)

		hooks.On("ServeHTTP", mock.AnythingOfType("*rpcplugin.RemoteHTTPResponseWriter"), mock.AnythingOfType("*http.Request")).Run(func(args mock.Arguments) {
			r := args.Get(1).(*http.Request)
			if r.URL.Path == "/1" {
				<-ch
			} else {
				ch <- true
			}
		})

		rec := httptest.NewRecorder()

		wg := sync.WaitGroup{}
		wg.Add(2)

		go func() {
			req, err := http.NewRequest("GET", "/1", nil)
			require.NoError(t, err)
			remote.ServeHTTP(rec, req)
			wg.Done()
		}()

		go func() {
			req, err := http.NewRequest("GET", "/2", nil)
			require.NoError(t, err)
			remote.ServeHTTP(rec, req)
			wg.Done()
		}()

		wg.Wait()
	}))
}

type testHooks struct {
	mock.Mock
}

func (h *testHooks) OnActivate(api plugin.API) error {
	return h.Called(api).Error(0)
}

func TestHooks_PartiallyImplemented(t *testing.T) {
	var api plugintest.API
	var hooks testHooks
	defer hooks.AssertExpectations(t)

	assert.NoError(t, testHooksRPC(&hooks, func(remote *RemoteHooks) {
		implemented, err := remote.Implemented()
		assert.NoError(t, err)
		assert.Equal(t, []string{"OnActivate"}, implemented)

		hooks.On("OnActivate", mock.AnythingOfType("*rpcplugin.RemoteAPI")).Return(nil)
		assert.NoError(t, remote.OnActivate(&api))

		assert.NoError(t, remote.OnDeactivate())
	}))
}

type benchmarkHooks struct{}

func (*benchmarkHooks) OnDeactivate() error { return nil }

func (*benchmarkHooks) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ioutil.ReadAll(r.Body)
	w.Header().Set("Foo-Header", "foo")
	http.Error(w, "foo", http.StatusBadRequest)
}

func BenchmarkHooks_OnDeactivate(b *testing.B) {
	var hooks benchmarkHooks

	if err := testHooksRPC(&hooks, func(remote *RemoteHooks) {
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			remote.OnDeactivate()
		}
		b.StopTimer()
	}); err != nil {
		b.Fatal(err.Error())
	}
}

func BenchmarkHooks_ServeHTTP(b *testing.B) {
	var hooks benchmarkHooks

	if err := testHooksRPC(&hooks, func(remote *RemoteHooks) {
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			w := httptest.NewRecorder()
			r, _ := http.NewRequest("POST", "/foo", strings.NewReader("12345678901234567890"))
			remote.ServeHTTP(w, r)
		}
		b.StopTimer()
	}); err != nil {
		b.Fatal(err.Error())
	}
}

func BenchmarkHooks_Unimplemented(b *testing.B) {
	var hooks testHooks

	if err := testHooksRPC(&hooks, func(remote *RemoteHooks) {
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			remote.OnDeactivate()
		}
		b.StopTimer()
	}); err != nil {
		b.Fatal(err.Error())
	}
}