@@ -2,6 +2,7 @@ package middleware
22
33import (
44 "context"
5+ "crypto/tls"
56 "errors"
67 "net/http"
78 "net/http/httptest"
@@ -91,3 +92,128 @@ func (s *MiddlewareTestSuite) TestRateLimitMiddlewareStoreFailure() {
9192 s .router .ServeHTTP (w , req )
9293 s .Equal (http .StatusInternalServerError , w .Code )
9394}
95+
96+ func (s * MiddlewareTestSuite ) TestSecurityHeadersAddsHSTS () {
97+ cfg := & config.Config {
98+ Server : config.ServerConfig {
99+ HostName : "example.com" ,
100+ Port : 443 ,
101+ },
102+ }
103+
104+ s .router .Use (SecurityHeaders (cfg ))
105+ s .router .GET ("/" , func (c * gin.Context ) {
106+ c .String (http .StatusOK , "OK" )
107+ })
108+
109+ w := httptest .NewRecorder ()
110+ req , _ := http .NewRequest ("GET" , "/" , nil )
111+ req .Header .Set ("X-Forwarded-Proto" , "https" )
112+ s .router .ServeHTTP (w , req )
113+ s .Equal (http .StatusOK , w .Code )
114+ s .Equal ("max-age=31536000; includeSubDomains; preload" , w .Header ().Get ("Strict-Transport-Security" ))
115+ }
116+
117+ func (s * MiddlewareTestSuite ) TestSecurityHeadersNoHSTSForPlainHTTP () {
118+ cfg := & config.Config {
119+ Server : config.ServerConfig {
120+ HostName : "example.com" ,
121+ Port : 443 ,
122+ },
123+ }
124+
125+ s .router .Use (SecurityHeaders (cfg ))
126+ s .router .GET ("/" , func (c * gin.Context ) {
127+ c .String (http .StatusOK , "OK" )
128+ })
129+
130+ w := httptest .NewRecorder ()
131+ req , _ := http .NewRequest ("GET" , "/" , nil )
132+ s .router .ServeHTTP (w , req )
133+ s .Equal (http .StatusMovedPermanently , w .Code )
134+ s .Empty (w .Header ().Get ("Strict-Transport-Security" ))
135+ }
136+
137+ func (s * MiddlewareTestSuite ) TestSecurityHeadersHSTSWithDirectTLS () {
138+ cfg := & config.Config {
139+ Server : config.ServerConfig {
140+ HostName : "example.com" ,
141+ Port : 443 ,
142+ },
143+ }
144+
145+ s .router .Use (SecurityHeaders (cfg ))
146+ s .router .GET ("/" , func (c * gin.Context ) {
147+ c .String (http .StatusOK , "OK" )
148+ })
149+
150+ w := httptest .NewRecorder ()
151+ req , _ := http .NewRequest ("GET" , "/" , nil )
152+ req .TLS = & tls.ConnectionState {}
153+ s .router .ServeHTTP (w , req )
154+ s .Equal (http .StatusOK , w .Code )
155+ s .Equal ("max-age=31536000; includeSubDomains; preload" , w .Header ().Get ("Strict-Transport-Security" ))
156+ }
157+
158+ func (s * MiddlewareTestSuite ) TestSecurityHeadersRedirectsHTTP () {
159+ cfg := & config.Config {
160+ Server : config.ServerConfig {
161+ HostName : "example.com" ,
162+ Port : 443 ,
163+ },
164+ }
165+
166+ s .router .Use (SecurityHeaders (cfg ))
167+ s .router .GET ("/path" , func (c * gin.Context ) {
168+ c .String (http .StatusOK , "OK" )
169+ })
170+
171+ w := httptest .NewRecorder ()
172+ req , _ := http .NewRequest ("GET" , "/path?q=1" , nil )
173+ req .Header .Set ("X-Forwarded-Proto" , "http" )
174+ s .router .ServeHTTP (w , req )
175+ s .Equal (http .StatusMovedPermanently , w .Code )
176+ s .Equal ("https://example.com/path?q=1" , w .Header ().Get ("Location" ))
177+ }
178+
179+ func (s * MiddlewareTestSuite ) TestSecurityHeadersRedirectsHTTPNonStandardPort () {
180+ cfg := & config.Config {
181+ Server : config.ServerConfig {
182+ HostName : "example.com" ,
183+ Port : 8443 ,
184+ },
185+ }
186+
187+ s .router .Use (SecurityHeaders (cfg ))
188+ s .router .GET ("/" , func (c * gin.Context ) {
189+ c .String (http .StatusOK , "OK" )
190+ })
191+
192+ w := httptest .NewRecorder ()
193+ req , _ := http .NewRequest ("GET" , "/" , nil )
194+ req .Header .Set ("X-Forwarded-Proto" , "http" )
195+ s .router .ServeHTTP (w , req )
196+ s .Equal (http .StatusMovedPermanently , w .Code )
197+ s .Equal ("https://example.com:8443/" , w .Header ().Get ("Location" ))
198+ }
199+
200+ func (s * MiddlewareTestSuite ) TestSecurityHeadersNoRedirectForHTTPS () {
201+ cfg := & config.Config {
202+ Server : config.ServerConfig {
203+ HostName : "example.com" ,
204+ Port : 443 ,
205+ },
206+ }
207+
208+ s .router .Use (SecurityHeaders (cfg ))
209+ s .router .GET ("/" , func (c * gin.Context ) {
210+ c .String (http .StatusOK , "OK" )
211+ })
212+
213+ w := httptest .NewRecorder ()
214+ req , _ := http .NewRequest ("GET" , "/" , nil )
215+ req .Header .Set ("X-Forwarded-Proto" , "https" )
216+ s .router .ServeHTTP (w , req )
217+ s .Equal (http .StatusOK , w .Code )
218+ s .Equal ("max-age=31536000; includeSubDomains; preload" , w .Header ().Get ("Strict-Transport-Security" ))
219+ }
0 commit comments