-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathoption_test.go
More file actions
48 lines (37 loc) · 1.06 KB
/
option_test.go
File metadata and controls
48 lines (37 loc) · 1.06 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
48
package timeout
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestOptions(t *testing.T) {
t.Parallel()
timeout := &Timeout{}
customTimeout := 5 * time.Second
customResponse := func(c *gin.Context) {
c.String(http.StatusGatewayTimeout, "test response")
}
// Apply options
WithTimeout(customTimeout)(timeout)
WithResponse(customResponse)(timeout)
// Assertions
assert.Equal(t, customTimeout, timeout.timeout)
assert.NotNil(t, timeout.response)
// To fully verify the handler, we can execute it and check the response.
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
timeout.response(c)
assert.Equal(t, http.StatusGatewayTimeout, w.Code)
assert.Equal(t, "test response", w.Body.String())
}
func TestDefaultResponse(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
defaultResponse(c)
assert.Equal(t, http.StatusRequestTimeout, w.Code)
assert.Equal(t, http.StatusText(http.StatusRequestTimeout), w.Body.String())
}