forked from gojek/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller_test.go
44 lines (34 loc) · 1.01 KB
/
caller_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
package fiber_test
import (
"context"
"net/http"
"testing"
"github.com/gojek/fiber"
testutils "github.com/gojek/fiber/internal/testutils/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockDispatcher struct {
mock.Mock
}
func (h *MockDispatcher) Do(req fiber.Request) fiber.Response {
args := h.Called(req)
if resp := args.Get(0); resp != nil {
return resp.(fiber.Response)
}
return nil
}
func TestCaller_Dispatch(t *testing.T) {
payload := "**BODY**"
expectedResponse := testutils.MockResp(http.StatusOK, payload, nil, nil)
dispatcher := new(MockDispatcher)
dispatcher.On("Do", mock.Anything).Return(expectedResponse)
caller, _ := fiber.NewCaller("", dispatcher)
req := testutils.MockReq("GET", "http://:8080/test", "")
resp := <-caller.Dispatch(context.Background(), req).Iter()
assert.NotNil(t, resp)
assert.Empty(t, resp.BackendName())
assert.True(t, resp.IsSuccess())
assert.Equal(t, []byte(payload), resp.Payload())
dispatcher.AssertExpectations(t)
}