-
Notifications
You must be signed in to change notification settings - Fork 6
/
https_redirect_test.go
172 lines (153 loc) · 4.63 KB
/
https_redirect_test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2016, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
)
func TestTLSListener(t *testing.T) {
ln, err := net.Listen("tcp", "")
if err != nil {
t.Error(err)
}
cert, err := tls.X509KeyPair([]byte(`
-----BEGIN CERTIFICATE-----
MIICKjCCAZOgAwIBAgIJAIMSNhoBKZFaMA0GCSqGSIb3DQEBCwUAMC4xCzAJBgNV
BAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UECgwJR29waGVyUGl0MB4XDTE2MTAy
NjE4NTg1MVoXDTI2MTAyNDE4NTg1MVowLjELMAkGA1UEBhMCVVMxCzAJBgNVBAgM
AkNBMRIwEAYDVQQKDAlHb3BoZXJQaXQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
AoGBAMGJE+nHSLxikzKG5ZniuFGed/uZwWA9EEOUE5MDmkZuUSnOCZ5v1v5rDRha
qW8rqTavbtW8bkhKKdMx5GnG3+6TTElgHYGYMDtbEBbTswx0+i9wOJXB11T7AQeu
dusElI0Gv0c5ss73emMNXUUU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b23RRrDftToDbsf2GB6dd/ItVXF
dkt05UV4U0aWHHpmz4MCQQCoexlXW7ce+6hLlQafgsiY18WFw/uXoGxHzSlpLUby
viBngkOY/zwTS9mYvM8ixsj16b2WWzajtjhBtihs+tur
-----END RSA PRIVATE KEY-----`))
if err != nil {
t.Error(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
}
ln = &TLSListener{
TCPListener: ln.(*net.TCPListener),
TLSConfig: tlsConfig,
}
var tlsRequest *bool
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tlsRequest = new(bool)
if r.TLS != nil {
*tlsRequest = !*tlsRequest
}
}),
TLSConfig: tlsConfig,
}
go func() {
if err := server.Serve(ln); err != nil {
t.Error(err)
}
}()
port := ln.Addr().(*net.TCPAddr).Port
r, err := http.Get(fmt.Sprintf("http://localhost:%d/", port))
if err != nil {
t.Error(err)
}
if r.StatusCode != http.StatusOK {
t.Errorf("expected %d response, got %d", http.StatusOK, r.StatusCode)
}
if tlsRequest == nil {
t.Error("no request has been served")
} else {
if *tlsRequest {
t.Error("tls connection detected, expected no-tls")
}
}
tlsRequest = nil
client := http.DefaultClient
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
r, err = client.Get(fmt.Sprintf("https://localhost:%d/", port))
if err != nil {
t.Error(err)
}
if r.StatusCode != http.StatusOK {
t.Errorf("expected %d response, got %d", http.StatusOK, r.StatusCode)
}
if tlsRequest == nil {
t.Error("no request has been served")
} else {
if !*tlsRequest {
t.Error("non-tls connection detected, expected tls")
}
}
}
func TestHTTPToHTTPSRedirectHandler(t *testing.T) {
for _, tc := range []struct {
name string
url string
loc string
}{
{
name: "Basic",
url: "/",
loc: "https://example.com/",
},
{
name: "Simple",
url: "http://localhost/test",
loc: "https://localhost/test",
},
{
name: "Port",
url: "http://localhost:8080/test",
loc: "https://localhost:8080/test",
},
{
name: "Query",
url: "http://localhost/test?q1=test&q2",
loc: "https://localhost/test?q1=test&q2",
},
{
name: "QueryAndFragment",
url: "http://localhost/test?q1=test&q2#fragment",
loc: "https://localhost/test?q1=test&q2#fragment",
},
} {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest("", tc.url, nil)
w := httptest.NewRecorder()
HTTPToHTTPSRedirectHandler(w, r)
if w.Result().StatusCode != http.StatusMovedPermanently {
t.Errorf("expected status code %d, got %d", http.StatusMovedPermanently, w.Result().StatusCode)
}
if w.Result().Header.Get("Location") != tc.loc {
t.Errorf("expected Location header %q, got %q", tc.loc, w.Result().Header.Get("Location"))
}
})
}
}