Skip to content

Commit

Permalink
Fix LoadHTML* tests
Browse files Browse the repository at this point in the history
- it's not necessary to rely on timeout, server starts listening synchronously and returns control when it is ready
- once the server is run, it's stopped after test passes
- dry out http server setup
- preserve router.RunTLS coverage with integration tests
  • Loading branch information
sponomarev committed Sep 20, 2018
1 parent b27b702 commit 2609878
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
Expand Up @@ -6,6 +6,7 @@ package gin

import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
Expand All @@ -19,7 +20,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 @@ -44,6 +52,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

0 comments on commit 2609878

Please sign in to comment.