forked from hungdv136/rio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
87 lines (73 loc) · 2.21 KB
/
request_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
package rio
import (
"context"
"net/http"
"testing"
"github.com/google/uuid"
"github.com/manabie-com/rio/internal/types"
"github.com/stretchr/testify/require"
"moul.io/http2curl/v2"
)
func TestIncomingRequest_Replay(t *testing.T) {
t.Parallel()
ctx := context.Background()
server := NewLocalServerWithReporter(t)
t.Run("replay_get", func(t *testing.T) {
t.Parallel()
requestID := uuid.NewString()
stubRes := NewResponse().WithBody(MustToJSON(types.Map{"id": 1}))
err := NewStub().For("GET", Contains("animal/get")).
WithHeader("X-REQUEST-ID", EqualTo(requestID)).
WithQuery("id", EqualTo(123)).
WillReturn(stubRes).
Send(ctx, server)
require.NoError(t, err)
incomeRequest := &IncomingRequest{
ID: 1,
Namespace: "dop",
URL: "https://api.server.com/echo/animal/get?id=123",
Method: "GET",
Header: types.Map{
"X-REQUEST-ID": requestID,
},
}
res, err := incomeRequest.Replay(ctx, server)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()
})
t.Run("replay_post", func(t *testing.T) {
t.Parallel()
requestBody := types.Map{"id": 345}
requestID := uuid.NewString()
stubRes := NewResponse().WithBody(MustToJSON(types.Map{"id": 1}))
err := NewStub().For("POST", Contains("animal/create")).
WithHeader("X-REQUEST-ID", EqualTo(requestID)).
WithRequestBody(BodyJSONPath("id", EqualTo(345))).
WillReturn(stubRes).
Send(ctx, server)
require.NoError(t, err)
incomeRequest := &IncomingRequest{
ID: 1,
Namespace: "dop",
URL: "https://api.server.com/echo/animal/create",
Method: "POST",
Header: types.Map{
HeaderContentType: ContentTypeJSON,
"X-REQUEST-ID": requestID,
},
Body: []byte(requestBody.ForceJSON()),
}
res, err := incomeRequest.Replay(ctx, server)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()
})
}
func TestRemoveBodyFromCurl(t *testing.T) {
t.Parallel()
curl := http2curl.CurlCommand([]string{"-H", "H1=val1", "-d", "data", "http://rio.com"})
expectedCurl := http2curl.CurlCommand([]string{"-H", "H1=val1", "http://rio.com"})
got := removeBodyFromCurl(&curl)
require.Equal(t, &expectedCurl, got)
}