-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_servers.go
143 lines (123 loc) · 4.64 KB
/
test_servers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2019 National Library of Norway.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testutil
import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"time"
)
// HttpServers contains test servers for HTTP and HTTPS
type HttpServers struct {
httpsMux *http.ServeMux
httpMux *http.ServeMux
SrvHttp *httptest.Server
SrvHttps *httptest.Server
SrvHttpsBadCert *httptest.Server
}
func replaceIpWithHostname(server *httptest.Server) *httptest.Server {
u, _ := url.Parse(server.URL)
server.URL = strings.Replace(server.URL, u.Hostname(), "localhost", 1)
return server
}
func NewHttpServer(mux *http.ServeMux) (server *httptest.Server) {
server = httptest.NewUnstartedServer(mux)
var err error
server.Listener, err = net.Listen("tcp4", ":0")
if err != nil {
panic(err)
}
server.Start()
server = replaceIpWithHostname(server)
return
}
func NewHttpsServer(mux *http.ServeMux) (server *httptest.Server) {
server = httptest.NewUnstartedServer(mux)
var err error
server.Listener, err = net.Listen("tcp4", ":0")
if err != nil {
panic(err)
}
server.StartTLS()
server = replaceIpWithHostname(server)
return
}
type ConstantHandler string
func (h ConstantHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, string(h))
}
type ConstantSlowHandler struct {
string
waitTime time.Duration
}
func NewConstantSlowHandler(response string, waitMillis int) *ConstantSlowHandler {
return &ConstantSlowHandler{string: response, waitTime: time.Millisecond * time.Duration(waitMillis)}
}
func (h ConstantSlowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(h.waitTime)
_, _ = io.WriteString(w, string(h.string))
}
type ConstantCacheHandler string
func (h ConstantCacheHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Cache", "HIT from veidemann_cache:1234")
w.Header().Add("X-Cache", "MISS")
w.Header().Set("X-Cache-Lookup", "HIT from veidemann_cache:1234")
_, _ = io.WriteString(w, string(h))
}
func NewHttpServers() *HttpServers {
httpMux := http.NewServeMux()
httpsMux := http.NewServeMux()
httpMux.Handle("/a", ConstantHandler("content from http server"))
httpsMux.Handle("/b", ConstantHandler("content from https server"))
httpMux.Handle("/replace", ConstantHandler("should be replaced"))
httpsMux.Handle("/replace", ConstantHandler("should be replaced"))
httpMux.Handle("/slow", NewConstantSlowHandler("content from http server", 600))
httpsMux.Handle("/slow", NewConstantSlowHandler("content from https server", 600))
httpMux.Handle("/extraslow", NewConstantSlowHandler("content from http server", 1000))
httpsMux.Handle("/extraslow", NewConstantSlowHandler("content from https server", 1000))
httpMux.Handle("/cancel", NewConstantSlowHandler("content from http server", 600))
httpsMux.Handle("/cancel", NewConstantSlowHandler("content from https server", 600))
httpMux.Handle("/blocked", NewConstantSlowHandler("content from http server", 600))
httpsMux.Handle("/blocked", NewConstantSlowHandler("content from https server", 600))
httpMux.Handle("/bccerr", ConstantHandler("content from http server"))
httpsMux.Handle("/bccerr", ConstantHandler("content from https server"))
httpMux.Handle("/cwerr", ConstantHandler("content from http server"))
httpsMux.Handle("/cwerr", ConstantHandler("content from https server"))
httpMux.Handle("/cached", ConstantCacheHandler("content from http server"))
httpsMux.Handle("/cached", ConstantCacheHandler("content from https server"))
s := &HttpServers{
SrvHttp: NewHttpServer(httpMux),
SrvHttps: NewHttpsServer(httpsMux),
SrvHttpsBadCert: NewHttpsServer(httpsMux),
}
s.SrvHttp.Config.WriteTimeout = 800 * time.Millisecond
s.SrvHttps.Config.WriteTimeout = 800 * time.Millisecond
s.SrvHttpsBadCert.TLS.Certificates = []tls.Certificate{{}}
fmt.Printf("HTTP server url: %v\n", s.SrvHttp.URL)
fmt.Printf("HTTPS server url: %v\n", s.SrvHttps.URL)
fmt.Printf("HTTPS bad cert server url: %v\n", s.SrvHttpsBadCert.URL)
return s
}
func (s *HttpServers) Close() {
s.SrvHttp.Close()
s.SrvHttps.Close()
s.SrvHttpsBadCert.Close()
}