Skip to content

Commit

Permalink
Fix LoadHTML* tests (#1559)
Browse files Browse the repository at this point in the history
Digging into the test code base I've found out that some of the tests for `LoadHTML*` methods are not reliable and efficient. They use timeouts to be sure that goroutine with the server has started. And even more, in old implementation, the server started only once – all the new instances silently failed due to the occupied network port.

Here is a short overview of the proposed changes: 
- it's not necessary to rely on timeouts, the server starts listening synchronously and returns control when it is ready
- once the server is run, it's stopped after a test passes
- dry out http server setup
- magic with empty closure return is eliminated 
- preserve router.RunTLS coverage with integration tests
  • Loading branch information
sponomarev authored and appleboy committed Oct 16, 2018
1 parent 524757b commit cfa092f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 114 deletions.
26 changes: 25 additions & 1 deletion gin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gin

import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
Expand All @@ -20,7 +21,14 @@ import (
)

func testRequest(t *testing.T, url string) {
resp, err := http.Get(url)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}

resp, err := client.Get(url)
assert.NoError(t, err)
defer resp.Body.Close()

Expand All @@ -45,6 +53,22 @@ func TestRunEmpty(t *testing.T) {
testRequest(t, "http://localhost:8080/example")
}

func TestRunTLS(t *testing.T) {
router := New()
go func() {
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })

assert.NoError(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
}()

// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)

assert.Error(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
testRequest(t, "https://localhost:8443/example")
}

func TestRunEmptyWithEnv(t *testing.T) {
os.Setenv("PORT", "3123")
router := New()
Expand Down
Loading

0 comments on commit cfa092f

Please sign in to comment.