-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_handler_test.go
97 lines (91 loc) · 2.76 KB
/
forward_handler_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
package aggregadantur_test
import (
"bytes"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/orange-cloudfoundry/aggregadantur"
"github.com/orange-cloudfoundry/aggregadantur/models"
"github.com/orange-cloudfoundry/aggregadantur/testhelper"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"net/http/httptest"
"os"
)
var _ = Describe("ForwardHandler", func() {
var pack *ServerPack
var respRecorder *httptest.ResponseRecorder
var aggrRoute *models.AggregateRoute
var fwdHandler *aggregadantur.ForwardHandler
BeforeEach(func() {
log.StandardLogger().Out = io.Discard
var err error
respRecorder = httptest.NewRecorder()
pack = NewAggregateEndpointPack("test")
aggrRoute, err = models.NewAggregateRoute(
"test",
"test",
models.NewUpstreamFromUrl(pack.HttpServer().URL),
models.AggregateEndpoints{
{
Url: "http://localhost",
Identifier: "test",
},
},
models.Auth{},
models.PathMatchers{models.NewPathMatcher("/**")},
models.PathMatchers{models.NewPathMatcher("/metrics")},
)
Expect(err).NotTo(HaveOccurred())
fwdHandler, err = aggregadantur.NewForwardHandler(aggrRoute)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
log.StandardLogger().Out = os.Stdout
pack.HttpServer().Close()
})
Context("ServeHTTP", func() {
It("should forward to with retry by default", func() {
req := testhelper.NewRequest(http.MethodGet, "http://localhost", bytes.NewBufferString("empty."))
nbTry := 0
pack.Handler().SetFn(func(w http.ResponseWriter, req *http.Request) bool {
nbTry += 1
if nbTry == 2 {
w.WriteHeader(http.StatusCreated)
w.Write([]byte("good"))
return true
}
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("bad"))
return true
})
fwdHandler.ServeHTTP(respRecorder, req)
Expect(nbTry).To(Equal(2))
Expect(respRecorder.Code).To(Equal(http.StatusCreated))
Expect(respRecorder.Body.String()).To(Equal("good"))
})
When("no buffer is set on route", func() {
It("Should forward without retry and full stream", func() {
aggrRoute.NoBuffer = true
fwdHandler, _ = aggregadantur.NewForwardHandler(aggrRoute)
req := testhelper.NewRequest(http.MethodGet, "http://localhost", bytes.NewBufferString("empty."))
nbTry := 0
pack.Handler().SetFn(func(w http.ResponseWriter, req *http.Request) bool {
nbTry += 1
if nbTry == 2 {
w.WriteHeader(http.StatusCreated)
w.Write([]byte("good"))
return true
}
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("bad"))
return true
})
fwdHandler.ServeHTTP(respRecorder, req)
Expect(nbTry).To(Equal(1))
Expect(respRecorder.Code).To(Equal(http.StatusBadGateway))
Expect(respRecorder.Body.String()).To(Equal("bad"))
})
})
})
})