Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
Signed-off-by: Antonije Ivanovic <[email protected]>
  • Loading branch information
anivanovic committed Oct 20, 2023
1 parent d8ac42f commit 534a193
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions transport/http/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"html"
"log"
"math/rand"
"net"
"net/http"
"os"
"testing"
"time"

"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"golang.org/x/net/http2"
)

func init() {
Expand Down Expand Up @@ -201,6 +203,45 @@ func TestRunServer_plain(t *testing.T) {
}
}

func TestRunServer_h2c(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

port := newPort()

done := make(chan error)
go func() {
done <- RunServer(
ctx,
config.ServiceConfig{
Port: port,
ExtraConfig: config.ExtraConfig{
ginNamespace: serverOptions{UseH2C: true},
},
},
http.HandlerFunc(dummyHandler),
)
}()

<-time.After(100 * time.Millisecond)

client := h2cClient()
resp, err := client.Get(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
t.Error(err)
return
}
if resp.StatusCode != 200 {
t.Errorf("unexpected status code: %d", resp.StatusCode)
return
}
cancel()

if err = <-done; err != nil {
t.Error(err)
}
}

func TestRunServer_disabledTLS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -412,6 +453,18 @@ func mtlsClient(certPath, keyPath string) (*http.Client, error) {
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConf}}, nil
}

// h2cClient initializes client which executes cleartext http2 requests
func h2cClient() *http.Client {
return &http.Client{
Transport: &http2.Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
AllowHTTP: true,
},
}
}

// newPort returns random port numbers to avoid port collisions during the tests
func newPort() int {
return 16666 + rand.Intn(40000)
Expand Down

0 comments on commit 534a193

Please sign in to comment.