Skip to content

Commit

Permalink
Merge branch 'master' into multiple-error_handler-labels-2
Browse files Browse the repository at this point in the history
  • Loading branch information
johakoch authored Apr 1, 2022
2 parents 8787b4c + 72609bc commit 60ab20b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
53 changes: 41 additions & 12 deletions internal/test/test_backend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"crypto/tls"
"encoding/json"
"io"
"net"
Expand All @@ -12,6 +13,8 @@ import (
"strconv"
"sync"
"time"

"github.com/avenga/couper/server"
)

type Backend struct {
Expand All @@ -26,18 +29,7 @@ func NewBackend() *Backend {

b.srv = httptest.NewServer(b)

// test handler
b.mux.HandleFunc("/anything", createAnythingHandler(http.StatusOK))
b.mux.HandleFunc("/", createAnythingHandler(http.StatusNotFound))
b.mux.HandleFunc("/ws", echo)
b.mux.HandleFunc("/pdf", pdf)
b.mux.HandleFunc("/small", small)
b.mux.HandleFunc("/jwks.json", jwks)
b.mux.HandleFunc("/.well-known/openid-configuration", oidc)
b.mux.HandleFunc("/error", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
b.mux.HandleFunc("/reflect", reflect)
registerHTTPHandler(b)

return b
}
Expand All @@ -54,6 +46,43 @@ func (b *Backend) Addr() string {
return b.srv.URL
}

func NewExpiredBackend() (*Backend, *server.SelfSignedCertificate) {
b := &Backend{
mux: http.NewServeMux(),
}

b.srv = httptest.NewUnstartedServer(b.mux)

selfSigned, err := server.NewCertificate(time.Microsecond, nil, nil)
if err != nil {
panic(err)
}

b.srv.TLS = &tls.Config{
Certificates: []tls.Certificate{*selfSigned.Server},
}

registerHTTPHandler(b)

b.srv.StartTLS()
return b, selfSigned
}

func registerHTTPHandler(b *Backend) {
// test handler
b.mux.HandleFunc("/anything", createAnythingHandler(http.StatusOK))
b.mux.HandleFunc("/", createAnythingHandler(http.StatusNotFound))
b.mux.HandleFunc("/ws", echo)
b.mux.HandleFunc("/pdf", pdf)
b.mux.HandleFunc("/small", small)
b.mux.HandleFunc("/jwks.json", jwks)
b.mux.HandleFunc("/.well-known/openid-configuration", oidc)
b.mux.HandleFunc("/error", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
b.mux.HandleFunc("/reflect", reflect)
}

func createAnythingHandler(status int) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
type anything struct {
Expand Down
17 changes: 16 additions & 1 deletion server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2950,7 +2950,22 @@ func TestConfigBodyContent(t *testing.T) {
helper := test.New(t)
client := newClient()

shutdown, _ := newCouper("testdata/integration/config/01_couper.hcl", test.New(t))
expiredOrigin, selfSigned := test.NewExpiredBackend()
defer expiredOrigin.Close()

expiredCert, err := os.CreateTemp(os.TempDir(), "expired.pem")
helper.Must(err)

_, err = expiredCert.Write(selfSigned.CA)
helper.Must(err)
helper.Must(expiredCert.Close())

defer os.RemoveAll(expiredCert.Name())

shutdown, _ := newCouperWithTemplate("testdata/integration/config/01_couper.hcl", helper, map[string]interface{}{
"expiredOrigin": expiredOrigin.Addr(),
"caFile": expiredCert.Name(),
})
defer shutdown()

// default port changed in config
Expand Down
6 changes: 4 additions & 2 deletions server/testdata/integration/config/01_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ server "hcl" {
endpoint "/expired" {
proxy {
backend "b" {
origin = "https://expired.badssl.com"
path = "/"
origin = "{{ .expiredOrigin }}"
path = "/anything"
}
}
}
Expand All @@ -24,6 +24,7 @@ definitions {

backend "b" {
origin = "http://1.2.3.4"
path = "/"
disable_certificate_validation = true
}

Expand All @@ -33,4 +34,5 @@ definitions {
settings {
default_port = 8090
no_proxy_from_env = true
ca_file = "{{ .caFile }}"
}

0 comments on commit 60ab20b

Please sign in to comment.