-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_test.go
49 lines (38 loc) · 1.08 KB
/
adapter_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
package adapter
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)
var hello = "Hello World!"
type HelloHandler struct{}
func (h *HelloHandler) Handle(ctx *CustomHTTPContext) {
if _, err := io.WriteString(ctx.ResponseWriter, hello); err != nil {
ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError)
}
}
func TestAdapter(t *testing.T) {
adaptee := &HelloHandler{}
adapted := HandlerAdapter(adaptee)
AssertHTTPRequestReturnsHello(t, adapted)
}
func TestNetHttpAdapter(t *testing.T) {
adaptee := func(ctx *CustomHTTPContext) error {
if _, err := io.WriteString(ctx.ResponseWriter, hello); err != nil {
ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError)
}
return nil
}
adapted := HandlerFuncAdapter(adaptee)
AssertHTTPRequestReturnsHello(t, adapted)
}
func AssertHTTPRequestReturnsHello(t *testing.T, adapted http.Handler) {
ts := httptest.NewServer(adapted)
defer ts.Close()
res, err := http.Get(ts.URL)
assert.NoError(t, err)
message, err := io.ReadAll(res.Body)
assert.Equal(t, hello, string(message))
}