-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
36 lines (29 loc) · 934 Bytes
/
struct_test.go
File metadata and controls
36 lines (29 loc) · 934 Bytes
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
package example
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/hungdv136/rio"
"github.com/stretchr/testify/require"
)
func TestStructCallAPI(t *testing.T) {
t.Parallel()
ctx := context.Background()
server := rio.NewLocalServerWithReporter(t)
animalName := uuid.NewString()
returnedBody := map[string]interface{}{"id": uuid.NewString()}
require.NoError(t, rio.NewStub().
// Verify method and path
For("POST", rio.EndWith("/animal")).
// Verify if the request body is composed correctly
WithRequestBody(rio.BodyJSONPath("$.name", rio.EqualTo(animalName))).
// Response with 200 (default) and JSON
WillReturn(rio.JSONResponse(returnedBody)).
// Submit stub to mock server
Send(ctx, server))
input := Request{Name: animalName}
api := &API{RootURL: server.GetURL(ctx)}
resData, err := api.Call(ctx, input)
require.NoError(t, err)
require.Equal(t, returnedBody["id"], resData.ID)
}