-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
47 lines (38 loc) · 1.19 KB
/
api_test.go
File metadata and controls
47 lines (38 loc) · 1.19 KB
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
package mockhttp
import (
"net/http"
"testing"
"github.com/le-yams/gotestingmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_api_mock_should(t *testing.T) {
t.Parallel()
t.Run("return_underlying_server_url", func(t *testing.T) {
t.Parallel()
// Arrange & Act
mockedAPI := API(testingmock.New(t))
t.Cleanup(mockedAPI.Close)
// Assert
assert.Equal(t, mockedAPI.testServer.URL, mockedAPI.GetURL().String())
assert.Equal(t, mockedAPI.GetURL().Host, mockedAPI.GetHost())
})
t.Run("register a cleanup to close underlying server", func(t *testing.T) {
t.Parallel()
// Arrange
mockedT := testingmock.New(t)
mockedAPI := API(mockedT)
mockedAPI.Stub(http.MethodGet, "/endpoint").WithStatusCode(http.StatusOK)
endpointURL := mockedAPI.GetURL().JoinPath("endpoint").String()
response, err := http.Get(endpointURL)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
// Act
cleanups := mockedT.GetCleanups()
require.Len(t, cleanups, 1)
cleanups[0]() // Execute cleanup like a testing.T would do
// Assert
_, err = http.Get(endpointURL) // Should fail because server is closed
assert.Error(t, err)
})
}