-
Notifications
You must be signed in to change notification settings - Fork 324
/
http_test.go
60 lines (52 loc) · 1.73 KB
/
http_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
package api
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/iotexproject/iotex-core/v2/test/mock/mock_web3server"
"github.com/iotexproject/iotex-core/v2/testutil"
)
func TestServeHTTP(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
handler := mock_web3server.NewMockWeb3Handler(ctrl)
handler.EXPECT().HandlePOSTReq(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
svr := newHTTPHandler(handler)
t.Run("WrongHTTPMethod", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "http://url.com", nil)
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
svr.ServeHTTP(resp, req)
require.Equal(http.StatusOK, resp.Result().StatusCode)
bytes, _ := io.ReadAll(resp.Result().Body)
require.Equal("IoTeX RPC endpoint is ready.", string(bytes))
})
t.Run("Success", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "http://url.com", strings.NewReader(`{}`))
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
svr.ServeHTTP(resp, req)
require.Equal(http.StatusOK, resp.Result().StatusCode)
})
}
func TestServerStartStop(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
handler := mock_web3server.NewMockWeb3Handler(ctrl)
svr := NewHTTPServer("", testutil.RandomPort(), newHTTPHandler(handler))
err := svr.Start(context.Background())
require.NoError(err)
err = testutil.WaitUntil(100*time.Millisecond, 3*time.Second, func() (bool, error) {
err = svr.Stop(context.Background())
return err == nil, err
})
require.NoError(err)
}