Skip to content

Commit

Permalink
Create a mock http2 server
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu committed Feb 12, 2019
1 parent 7fa73a3 commit 74229db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
33 changes: 31 additions & 2 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
Expand Down Expand Up @@ -670,8 +671,18 @@ func TestHTTP2(t *testing.T) {
certKeyPath := filepath.Join(dir, "server.key")
ioutil.WriteFile(certKeyPath, serverPrivPem, 0666)

http2Server, err := startMockHttp2Server(certFilePath, certKeyPath)
if err != nil {
t.Fatal(err)
}

defer func() {
http2Server.Shutdown(context.Background())
}()

// Configuration
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
globalConf.HttpServerOptions.EnableHttp2 = true
globalConf.HttpServerOptions.Certificates = []config.CertData{{
Name: "localhost",
Expand All @@ -688,12 +699,30 @@ func TestHTTP2(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.UseKeylessAccess = true
spec.Proxy.TargetURL = "https://http2.golang.org" // HTTP/2 Upstream
spec.Proxy.TargetURL = "https://localhost:16501" // HTTP/2 Upstream
})

// Client
http2Client := getTLSClient(&clientCert, serverCertPem, true)

ts.Run(t, test.TestCase{Client: http2Client, Path: "", Code: 200, BodyMatch: "<h1>Go + HTTP/2</h1>"})
ts.Run(t, test.TestCase{Client: http2Client, Path: "", Code: 200, BodyMatch: "Hello, I am an Http2 Server"})
}

func startMockHttp2Server(certFilePath string, keyFilePath string) (*http.Server, error) {
s := &http.Server{
Addr: ":16501",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, I am an Http2 Server")
}),
}
err := http2.ConfigureServer(s, nil)
if err != nil {
return nil, err
}

go func() {
s.ListenAndServeTLS(certFilePath, keyFilePath)
}()

return s, nil
}
7 changes: 3 additions & 4 deletions test/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
var (
muDefaultResolver sync.RWMutex
DomainsToAddresses = map[string][]string{
"host1.local.": {"127.0.0.1"},
"host2.local.": {"127.0.0.1"},
"host3.local.": {"127.0.0.1"},
"http2.golang.org.": {"130.211.116.44"}, // HTTP/2 server
"host1.local.": {"127.0.0.1"},
"host2.local.": {"127.0.0.1"},
"host3.local.": {"127.0.0.1"},
}
)

Expand Down

0 comments on commit 74229db

Please sign in to comment.