(t *testing.T)
| 78 | } |
| 79 | |
| 80 | func TestStartConfig_Start(t *testing.T) { |
| 81 | e := New() |
| 82 | e.GET("/ok", func(c *Context) error { |
| 83 | return c.String(http.StatusOK, "OK") |
| 84 | }) |
| 85 | |
| 86 | addrChan := make(chan string) |
| 87 | errCh := make(chan error) |
| 88 | |
| 89 | ctx, shutdown := stdContext.WithTimeout(stdContext.Background(), 200*time.Millisecond) |
| 90 | defer shutdown() |
| 91 | go func() { |
| 92 | errCh <- (&StartConfig{ |
| 93 | Address: ":0", |
| 94 | ListenerAddrFunc: func(addr net.Addr) { |
| 95 | addrChan <- addr.String() |
| 96 | }, |
| 97 | }).Start(ctx, e) |
| 98 | }() |
| 99 | |
| 100 | addr, err := waitForServerStart(addrChan, errCh) |
| 101 | assert.NoError(t, err) |
| 102 | |
| 103 | // check if server is actually up |
| 104 | code, body, err := doGet(fmt.Sprintf("http://%v/ok", addr)) |
| 105 | if err != nil { |
| 106 | assert.NoError(t, err) |
| 107 | return |
| 108 | } |
| 109 | assert.Equal(t, http.StatusOK, code) |
| 110 | assert.Equal(t, "OK", body) |
| 111 | |
| 112 | shutdown() |
| 113 | |
| 114 | <-errCh // we will be blocking here until server returns from http.Serve |
| 115 | |
| 116 | // check if server was stopped |
| 117 | code, body, err = doGet(fmt.Sprintf("http://%v/ok", addr)) |
| 118 | assert.Equal(t, 0, code) |
| 119 | assert.Equal(t, "", body) |
| 120 | |
| 121 | if err == nil { |
| 122 | t.Errorf("missing error") |
| 123 | return |
| 124 | } |
| 125 | expectContains := "connect: connection refused" |
| 126 | if runtime.GOOS == "windows" { |
| 127 | expectContains = "No connection could be made" |
| 128 | } |
| 129 | assert.True(t, strings.Contains(err.Error(), expectContains)) |
| 130 | } |
| 131 | |
| 132 | func TestStartConfig_GracefulShutdown(t *testing.T) { |
| 133 | var testCases = []struct { |
nothing calls this directly
no test coverage detected
searching dependent graphs…