Skip to content

Commit fd6025a

Browse files
committedAug 9, 2023
test(server_test.go): add test for RegisterHeaders
1 parent 1787bdd commit fd6025a

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed
 

‎internal/server/config_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package server
17+
package server_test
1818

1919
import (
2020
"encoding/json"
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/orca-group/spirit/internal/config"
2727
"github.com/orca-group/spirit/internal/database"
28+
"github.com/orca-group/spirit/internal/server"
2829
"github.com/stretchr/testify/require"
2930
)
3031

@@ -49,7 +50,7 @@ var mockConfig = config.Cfg{
4950
// then executes the request by calling ServeHTTP in the router
5051
// after which the handler writes the response to the response recorder
5152
// which we can then inspect.
52-
func executeRequest(req *http.Request, s *Server) *httptest.ResponseRecorder {
53+
func executeRequest(req *http.Request, s *server.Server) *httptest.ResponseRecorder {
5354
rr := httptest.NewRecorder()
5455
s.Router.ServeHTTP(rr, req)
5556

@@ -67,7 +68,7 @@ func checkResponseCode(t *testing.T, expected, actual int) {
6768
func TestConfig(t *testing.T) {
6869
mockDB := database.NewMockDatabase(t)
6970

70-
s := NewServer(&mockConfig, mockDB)
71+
s := server.NewServer(&mockConfig, mockDB)
7172
s.MountHandlers()
7273

7374
req, _ := http.NewRequest("GET", "/config", nil)

‎internal/server/create_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package server
17+
package server_test
1818

1919
import "testing"
2020

‎internal/server/fetch_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package server
17+
package server_test
1818

1919
import (
2020
"encoding/json"
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/orca-group/spirit/internal/database"
27+
"github.com/orca-group/spirit/internal/server"
2728
"github.com/stretchr/testify/mock"
2829
"github.com/stretchr/testify/require"
2930
)
@@ -36,7 +37,7 @@ type DocumentResponse struct {
3637
func TestFetch(t *testing.T) {
3738
mockDB := database.NewMockDatabase(t)
3839

39-
s := NewServer(&mockConfig, mockDB)
40+
s := server.NewServer(&mockConfig, mockDB)
4041
s.MountHandlers()
4142

4243
mockDB.EXPECT().GetDocument(mock.Anything, "12345678").Return(database.Document{

‎internal/server/server_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020-2023 Luke Whritenour
3+
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package server_test
18+
19+
import (
20+
"net/http"
21+
"testing"
22+
23+
"github.com/orca-group/spirit/internal/database"
24+
"github.com/orca-group/spirit/internal/server"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestRegisterHeaders(t *testing.T) {
29+
s := server.NewServer(&mockConfig, &database.MockDatabase{})
30+
31+
s.RegisterHeaders()
32+
s.Router.Get("/", func(w http.ResponseWriter, r *http.Request) {
33+
w.Header().Set("Content-Type", "text/plain")
34+
w.WriteHeader(http.StatusOK)
35+
w.Write([]byte("."))
36+
})
37+
38+
req, _ := http.NewRequest(http.MethodGet, "/", nil)
39+
res := executeRequest(req, s)
40+
41+
// Ensure 200
42+
checkResponseCode(t, http.StatusOK, res.Result().StatusCode)
43+
44+
require.Equal(t, "noopen", res.Result().Header.Get("X-Download-Options"))
45+
require.Equal(t, "off", res.Result().Header.Get("X-DNS-Prefetch-Control"))
46+
require.Equal(t, "SAMEORIGIN", res.Result().Header.Get("X-Frame-Options"))
47+
require.Equal(t, "1; mode=block", res.Result().Header.Get("X-XSS-Protection"))
48+
require.Equal(t, "nosniff", res.Result().Header.Get("X-Content-Type-Options"))
49+
require.Equal(t, "no-referrer-when-downgrade", res.Result().Header.Get("Referrer-Policy"))
50+
require.Equal(t, "max-age=31536000; includeSubDomains; preload", res.Result().Header.Get("Strict-Transport-Security"))
51+
require.Equal(t, "default-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; script-src 'self' 'unsafe-inline';", res.Result().Header.Get("Content-Security-Policy"))
52+
}

0 commit comments

Comments
 (0)